Skip to content

Commit 650ec2d

Browse files
timotewMylesBorins
authored andcommitted
lib: extract validation functions
PR-URL: #18421 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent f6c9a2b commit 650ec2d

File tree

1 file changed

+29
-30
lines changed

1 file changed

+29
-30
lines changed

lib/internal/encoding.js

+29-30
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ function lazyBuffer() {
3232
return Buffer;
3333
}
3434

35+
function validateEncoder(obj) {
36+
if (obj == null || obj[kEncoder] !== true)
37+
throw new errors.TypeError('ERR_INVALID_THIS', 'TextEncoder');
38+
}
39+
40+
function validateDecoder(obj) {
41+
if (obj == null || obj[kDecoder] !== true)
42+
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
43+
}
44+
45+
function validateArgument(prop, expected, propName, expectedName) {
46+
if (typeof prop !== expected)
47+
throw new errors.Error('ERR_INVALID_ARG_TYPE', propName, expectedName);
48+
}
49+
3550
const CONVERTER_FLAGS_FLUSH = 0x1;
3651
const CONVERTER_FLAGS_FATAL = 0x2;
3752
const CONVERTER_FLAGS_IGNORE_BOM = 0x4;
@@ -288,20 +303,17 @@ class TextEncoder {
288303
}
289304

290305
get encoding() {
291-
if (this == null || this[kEncoder] !== true)
292-
throw new errors.TypeError('ERR_INVALID_THIS', 'TextEncoder');
306+
validateEncoder(this);
293307
return 'utf-8';
294308
}
295309

296310
encode(input = '') {
297-
if (this == null || this[kEncoder] !== true)
298-
throw new errors.TypeError('ERR_INVALID_THIS', 'TextEncoder');
311+
validateEncoder(this);
299312
return encodeUtf8String(`${input}`);
300313
}
301314

302315
[inspect](depth, opts) {
303-
if (this == null || this[kEncoder] !== true)
304-
throw new errors.TypeError('ERR_INVALID_THIS', 'TextEncoder');
316+
validateEncoder(this);
305317
if (typeof depth === 'number' && depth < 0)
306318
return opts.stylize('[Object]', 'special');
307319
var ctor = getConstructorOf(this);
@@ -329,8 +341,7 @@ const { hasConverter, TextDecoder } =
329341
makeTextDecoderJS();
330342

331343
function hasTextDecoder(encoding = 'utf-8') {
332-
if (typeof encoding !== 'string')
333-
throw new errors.Error('ERR_INVALID_ARG_TYPE', 'encoding', 'string');
344+
validateArgument(encoding, 'string', 'encoding', 'string');
334345
return hasConverter(getEncodingFromLabel(encoding));
335346
}
336347

@@ -344,8 +355,7 @@ function makeTextDecoderICU() {
344355
class TextDecoder {
345356
constructor(encoding = 'utf-8', options = {}) {
346357
encoding = `${encoding}`;
347-
if (typeof options !== 'object')
348-
throw new errors.Error('ERR_INVALID_ARG_TYPE', 'options', 'Object');
358+
validateArgument(options, 'object', 'options', 'Object');
349359

350360
const enc = getEncodingFromLabel(encoding);
351361
if (enc === undefined)
@@ -369,17 +379,14 @@ function makeTextDecoderICU() {
369379

370380

371381
decode(input = empty, options = {}) {
372-
if (this == null || this[kDecoder] !== true)
373-
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
382+
validateDecoder(this);
374383
if (isArrayBuffer(input)) {
375384
input = lazyBuffer().from(input);
376385
} else if (!isArrayBufferView(input)) {
377386
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'input',
378387
['ArrayBuffer', 'ArrayBufferView']);
379388
}
380-
if (typeof options !== 'object') {
381-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object');
382-
}
389+
validateArgument(options, 'object', 'options', 'Object');
383390

384391
var flags = 0;
385392
if (options !== null)
@@ -416,8 +423,7 @@ function makeTextDecoderJS() {
416423
class TextDecoder {
417424
constructor(encoding = 'utf-8', options = {}) {
418425
encoding = `${encoding}`;
419-
if (typeof options !== 'object')
420-
throw new errors.Error('ERR_INVALID_ARG_TYPE', 'options', 'Object');
426+
validateArgument(options, 'object', 'options', 'Object');
421427

422428
const enc = getEncodingFromLabel(encoding);
423429
if (enc === undefined || !hasConverter(enc))
@@ -440,8 +446,7 @@ function makeTextDecoderJS() {
440446
}
441447

442448
decode(input = empty, options = {}) {
443-
if (this == null || this[kDecoder] !== true)
444-
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
449+
validateDecoder(this);
445450
if (isArrayBuffer(input)) {
446451
input = lazyBuffer().from(input);
447452
} else if (isArrayBufferView(input)) {
@@ -451,9 +456,7 @@ function makeTextDecoderJS() {
451456
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'input',
452457
['ArrayBuffer', 'ArrayBufferView']);
453458
}
454-
if (typeof options !== 'object') {
455-
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object');
456-
}
459+
validateArgument(options, 'object', 'options', 'Object');
457460

458461
if (this[kFlags] & CONVERTER_FLAGS_FLUSH) {
459462
this[kBOMSeen] = false;
@@ -496,27 +499,23 @@ function makeTextDecoderJS() {
496499
TextDecoder.prototype,
497500
Object.getOwnPropertyDescriptors({
498501
get encoding() {
499-
if (this == null || this[kDecoder] !== true)
500-
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
502+
validateDecoder(this);
501503
return this[kEncoding];
502504
},
503505

504506
get fatal() {
505-
if (this == null || this[kDecoder] !== true)
506-
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
507+
validateDecoder(this);
507508
return (this[kFlags] & CONVERTER_FLAGS_FATAL) === CONVERTER_FLAGS_FATAL;
508509
},
509510

510511
get ignoreBOM() {
511-
if (this == null || this[kDecoder] !== true)
512-
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
512+
validateDecoder(this);
513513
return (this[kFlags] & CONVERTER_FLAGS_IGNORE_BOM) ===
514514
CONVERTER_FLAGS_IGNORE_BOM;
515515
},
516516

517517
[inspect](depth, opts) {
518-
if (this == null || this[kDecoder] !== true)
519-
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
518+
validateDecoder(this);
520519
if (typeof depth === 'number' && depth < 0)
521520
return opts.stylize('[Object]', 'special');
522521
var ctor = getConstructorOf(this);

0 commit comments

Comments
 (0)