Skip to content

Commit 007abb0

Browse files
authored
lib: use validateObject
Used the validateObject() validator to keep consistency instead of a custom validator which was only used to validate objects. Refs: nodejs#39595
1 parent 8be3b91 commit 007abb0

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

lib/internal/encoding.js

+20-11
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ const {
3939
isUint8Array
4040
} = require('internal/util/types');
4141

42-
const { validateString } = require('internal/validators');
42+
const {
43+
validateString,
44+
validateObject,
45+
} = require('internal/validators');
4346

4447
const {
4548
encodeInto,
@@ -63,12 +66,6 @@ function validateDecoder(obj) {
6366
throw new ERR_INVALID_THIS('TextDecoder');
6467
}
6568

66-
function validateArgument(prop, expected, propName, expectedName) {
67-
// eslint-disable-next-line valid-typeof
68-
if (typeof prop !== expected)
69-
throw new ERR_INVALID_ARG_TYPE(propName, expectedName, prop);
70-
}
71-
7269
const CONVERTER_FLAGS_FLUSH = 0x1;
7370
const CONVERTER_FLAGS_FATAL = 0x2;
7471
const CONVERTER_FLAGS_IGNORE_BOM = 0x4;
@@ -381,7 +378,10 @@ function makeTextDecoderICU() {
381378
class TextDecoder {
382379
constructor(encoding = 'utf-8', options = {}) {
383380
encoding = `${encoding}`;
384-
validateArgument(options, 'object', 'options', 'Object');
381+
validateObject(options, 'options', {
382+
nullable: true,
383+
allowArray: true
384+
});
385385

386386
const enc = getEncodingFromLabel(encoding);
387387
if (enc === undefined)
@@ -413,7 +413,10 @@ function makeTextDecoderICU() {
413413
['ArrayBuffer', 'ArrayBufferView'],
414414
input);
415415
}
416-
validateArgument(options, 'object', 'options', 'Object');
416+
validateObject(options, 'options', {
417+
nullable: true,
418+
allowArray: true
419+
});
417420

418421
let flags = 0;
419422
if (options !== null)
@@ -447,7 +450,10 @@ function makeTextDecoderJS() {
447450
class TextDecoder {
448451
constructor(encoding = 'utf-8', options = {}) {
449452
encoding = `${encoding}`;
450-
validateArgument(options, 'object', 'options', 'Object');
453+
validateObject(options, 'options', {
454+
nullable: true,
455+
allowArray: true
456+
});
451457

452458
const enc = getEncodingFromLabel(encoding);
453459
if (enc === undefined || !hasConverter(enc))
@@ -481,7 +487,10 @@ function makeTextDecoderJS() {
481487
['ArrayBuffer', 'ArrayBufferView'],
482488
input);
483489
}
484-
validateArgument(options, 'object', 'options', 'Object');
490+
validateObject(options, 'options', {
491+
nullable: true,
492+
allowArray: true
493+
});
485494

486495
if (this[kFlags] & CONVERTER_FLAGS_FLUSH) {
487496
this[kBOMSeen] = false;

0 commit comments

Comments
 (0)