Skip to content

Commit

Permalink
lib: use more primordials in shared validators
Browse files Browse the repository at this point in the history
PR-URL: #36552
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • Loading branch information
PoojaDurgad authored and aduh95 committed Dec 28, 2020
1 parent ecdff81 commit 0538749
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

const {
ArrayIsArray,
ArrayPrototypeIncludes,
ArrayPrototypeJoin,
ArrayPrototypeMap,
NumberIsInteger,
NumberMAX_SAFE_INTEGER,
NumberMIN_SAFE_INTEGER,
NumberParseInt,
RegExpPrototypeTest,
String,
StringPrototypeToUpperCase,
StringPrototypeTrim,
} = primordials;

const {
Expand Down Expand Up @@ -63,7 +69,7 @@ function parseFileMode(value, name, def) {
}

if (typeof value === 'string') {
if (!octalReg.test(value)) {
if (!RegExpPrototypeTest(octalReg, value)) {
throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
}
return NumberParseInt(value, 8);
Expand Down Expand Up @@ -129,10 +135,11 @@ function validateNumber(value, name) {
}

const validateOneOf = hideStackFrames((value, name, oneOf) => {
if (!oneOf.includes(value)) {
const allowed = oneOf
.map((v) => (typeof v === 'string' ? `'${v}'` : String(v)))
.join(', ');
if (!ArrayPrototypeIncludes(oneOf, value)) {
const allowed = ArrayPrototypeJoin(
ArrayPrototypeMap(oneOf, (v) =>
(typeof v === 'string' ? `'${v}'` : String(v))),
', ');
const reason = 'must be one of: ' + allowed;
throw new ERR_INVALID_ARG_VALUE(name, value, reason);
}
Expand Down Expand Up @@ -167,7 +174,7 @@ function validateSignalName(signal, name = 'signal') {
throw new ERR_INVALID_ARG_TYPE(name, 'string', signal);

if (signals[signal] === undefined) {
if (signals[signal.toUpperCase()] !== undefined) {
if (signals[StringPrototypeToUpperCase(signal)] !== undefined) {
throw new ERR_UNKNOWN_SIGNAL(signal +
' (signals must use all capital letters)');
}
Expand Down Expand Up @@ -198,7 +205,7 @@ function validateEncoding(data, encoding) {
// is an integer and that it falls within the legal range of port numbers.
function validatePort(port, name = 'Port', { allowZero = true } = {}) {
if ((typeof port !== 'number' && typeof port !== 'string') ||
(typeof port === 'string' && port.trim().length === 0) ||
(typeof port === 'string' && StringPrototypeTrim(port).length === 0) ||
+port !== (+port >>> 0) ||
port > 0xFFFF ||
(port === 0 && !allowZero)) {
Expand Down

0 comments on commit 0538749

Please sign in to comment.