Skip to content

Commit

Permalink
lib: use validateObject
Browse files Browse the repository at this point in the history
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 <tniessen@tnie.de>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
VoltrexKeyva authored and danielleadams committed Aug 16, 2021
1 parent 37dda19 commit 486d51a
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions lib/internal/encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ const {
isUint8Array
} = require('internal/util/types');

const { validateString } = require('internal/validators');
const {
validateString,
validateObject,
} = require('internal/validators');

const {
encodeInto,
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 486d51a

Please sign in to comment.