Skip to content

Commit 5b11042

Browse files
VoltrexKeyvatargos
authored andcommitted
lib: use validateObject
Used the validateObject() validator to keep consistency instead of a custom validator which was only used to validate objects. Refs: #39595 PR-URL: #39605 Refs: #39595 Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 4b2eee5 commit 5b11042

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

Diff for: lib/internal/encoding.js

+24-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,11 @@ 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+
allowFunction: true,
385+
});
385386

386387
const enc = getEncodingFromLabel(encoding);
387388
if (enc === undefined)
@@ -413,7 +414,11 @@ function makeTextDecoderICU() {
413414
['ArrayBuffer', 'ArrayBufferView'],
414415
input);
415416
}
416-
validateArgument(options, 'object', 'options', 'Object');
417+
validateObject(options, 'options', {
418+
nullable: true,
419+
allowArray: true,
420+
allowFunction: true,
421+
});
417422

418423
let flags = 0;
419424
if (options !== null)
@@ -447,7 +452,11 @@ function makeTextDecoderJS() {
447452
class TextDecoder {
448453
constructor(encoding = 'utf-8', options = {}) {
449454
encoding = `${encoding}`;
450-
validateArgument(options, 'object', 'options', 'Object');
455+
validateObject(options, 'options', {
456+
nullable: true,
457+
allowArray: true,
458+
allowFunction: true,
459+
});
451460

452461
const enc = getEncodingFromLabel(encoding);
453462
if (enc === undefined || !hasConverter(enc))
@@ -481,7 +490,11 @@ function makeTextDecoderJS() {
481490
['ArrayBuffer', 'ArrayBufferView'],
482491
input);
483492
}
484-
validateArgument(options, 'object', 'options', 'Object');
493+
validateObject(options, 'options', {
494+
nullable: true,
495+
allowArray: true,
496+
allowFunction: true,
497+
});
485498

486499
if (this[kFlags] & CONVERTER_FLAGS_FLUSH) {
487500
this[kBOMSeen] = false;

0 commit comments

Comments
 (0)