From 5b1104291d2c9d9af63bbf9039aa64b7ee78aede Mon Sep 17 00:00:00 2001 From: Voltrex Date: Sat, 31 Jul 2021 15:20:19 +0430 Subject: [PATCH] lib: use `validateObject` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Used the validateObject() validator to keep consistency instead of a custom validator which was only used to validate objects. Refs: #39595 PR-URL: https://github.com/nodejs/node/pull/39605 Refs: https://github.com/nodejs/node/pull/39595 Reviewed-By: Tobias Nießen Reviewed-By: Darshan Sen Reviewed-By: James M Snell --- lib/internal/encoding.js | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/internal/encoding.js b/lib/internal/encoding.js index f6e52238a1270c..2cdd453b15ba91 100644 --- a/lib/internal/encoding.js +++ b/lib/internal/encoding.js @@ -39,7 +39,10 @@ const { isUint8Array } = require('internal/util/types'); -const { validateString } = require('internal/validators'); +const { + validateString, + validateObject, +} = require('internal/validators'); const { encodeInto, @@ -63,12 +66,6 @@ function validateDecoder(obj) { throw new ERR_INVALID_THIS('TextDecoder'); } -function validateArgument(prop, expected, propName, expectedName) { - // eslint-disable-next-line valid-typeof - if (typeof prop !== expected) - throw new ERR_INVALID_ARG_TYPE(propName, expectedName, prop); -} - const CONVERTER_FLAGS_FLUSH = 0x1; const CONVERTER_FLAGS_FATAL = 0x2; const CONVERTER_FLAGS_IGNORE_BOM = 0x4; @@ -381,7 +378,11 @@ function makeTextDecoderICU() { class TextDecoder { constructor(encoding = 'utf-8', options = {}) { encoding = `${encoding}`; - validateArgument(options, 'object', 'options', 'Object'); + validateObject(options, 'options', { + nullable: true, + allowArray: true, + allowFunction: true, + }); const enc = getEncodingFromLabel(encoding); if (enc === undefined) @@ -413,7 +414,11 @@ function makeTextDecoderICU() { ['ArrayBuffer', 'ArrayBufferView'], input); } - validateArgument(options, 'object', 'options', 'Object'); + validateObject(options, 'options', { + nullable: true, + allowArray: true, + allowFunction: true, + }); let flags = 0; if (options !== null) @@ -447,7 +452,11 @@ function makeTextDecoderJS() { class TextDecoder { constructor(encoding = 'utf-8', options = {}) { encoding = `${encoding}`; - validateArgument(options, 'object', 'options', 'Object'); + validateObject(options, 'options', { + nullable: true, + allowArray: true, + allowFunction: true, + }); const enc = getEncodingFromLabel(encoding); if (enc === undefined || !hasConverter(enc)) @@ -481,7 +490,11 @@ function makeTextDecoderJS() { ['ArrayBuffer', 'ArrayBufferView'], input); } - validateArgument(options, 'object', 'options', 'Object'); + validateObject(options, 'options', { + nullable: true, + allowArray: true, + allowFunction: true, + }); if (this[kFlags] & CONVERTER_FLAGS_FLUSH) { this[kBOMSeen] = false;