Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: restate the code to use primordials #36552

Merged
merged 1 commit into from
Dec 28, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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