lib: use more primordials in shared validators

PR-URL: https://github.com/nodejs/node/pull/36552
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Pooja D P authored and Danielle Adams committed 2021-01-12 07:10:20 -05:00
1 parent 64bf2f229e
commit bb4f8c8732
1 file changed
+14 -7
+14 -7
View File
@@ -2,11 +2,17 @@
const {
ArrayIsArray,
ArrayPrototypeIncludes,
ArrayPrototypeJoin,
ArrayPrototypeMap,
NumberIsInteger,
NumberMAX_SAFE_INTEGER,
NumberMIN_SAFE_INTEGER,
NumberParseInt,
RegExpPrototypeTest,
String,
StringPrototypeToUpperCase,
StringPrototypeTrim,
} = primordials;
const {
@@ -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);
@@ -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);
}
@@ -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)');
}
@@ -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)) {