Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
splitting out validateEncoder() and validateDecoder()
  • Loading branch information
timotew committed Jan 29, 2018
commit 8f4a276fcae72fdf7a46313b7908a5880a048c78
30 changes: 17 additions & 13 deletions lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ function lazyBuffer() {
return Buffer;
}

function validateEnDecoder(obj, type) {
const prop = type === 'TextDecoder' ? kDecoder : kEncoder;
if (obj == null || obj[prop] !== true)
throw new errors.TypeError('ERR_INVALID_THIS', type);
function validateEncoder(obj) {
if (obj == null || obj[kEncoder] !== true)
throw new errors.TypeError('ERR_INVALID_THIS', 'TextEncoder');
}

function validateDecoder(obj) {
if (obj == null || obj[kDecoder] !== true)
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
}

function validateArgument(prop, expected, propName) {
Expand Down Expand Up @@ -299,17 +303,17 @@ class TextEncoder {
}

get encoding() {
validateEnDecoder(this, 'TextEncoder');
validateEncoder(this);
return 'utf-8';
}

encode(input = '') {
validateEnDecoder(this, 'TextEncoder');
validateEncoder(this);
return encodeUtf8String(`${input}`);
}

[inspect](depth, opts) {
validateEnDecoder(this, 'TextEncoder');
validateEncoder(this);
if (typeof depth === 'number' && depth < 0)
return opts.stylize('[Object]', 'special');
var ctor = getConstructorOf(this);
Expand Down Expand Up @@ -375,7 +379,7 @@ function makeTextDecoderICU() {


decode(input = empty, options = {}) {
validateEnDecoder(this, 'TextDecoder');
validateDecoder(this);
if (isArrayBuffer(input)) {
input = lazyBuffer().from(input);
} else if (!isArrayBufferView(input)) {
Expand Down Expand Up @@ -442,7 +446,7 @@ function makeTextDecoderJS() {
}

decode(input = empty, options = {}) {
validateEnDecoder(this, 'TextDecoder');
validateDecoder(this);
if (isArrayBuffer(input)) {
input = lazyBuffer().from(input);
} else if (isArrayBufferView(input)) {
Expand Down Expand Up @@ -495,23 +499,23 @@ function makeTextDecoderJS() {
TextDecoder.prototype,
Object.getOwnPropertyDescriptors({
get encoding() {
validateEnDecoder(this, 'TextDecoder');
validateDecoder(this);
return this[kEncoding];
},

get fatal() {
validateEnDecoder(this, 'TextDecoder');
validateDecoder(this);
return (this[kFlags] & CONVERTER_FLAGS_FATAL) === CONVERTER_FLAGS_FATAL;
},

get ignoreBOM() {
validateEnDecoder(this, 'TextDecoder');
validateDecoder(this);
return (this[kFlags] & CONVERTER_FLAGS_IGNORE_BOM) ===
CONVERTER_FLAGS_IGNORE_BOM;
},

[inspect](depth, opts) {
validateEnDecoder(this, 'TextDecoder');
validateDecoder(this);
if (typeof depth === 'number' && depth < 0)
return opts.stylize('[Object]', 'special');
var ctor = getConstructorOf(this);
Expand Down