Skip to content

Commit bb4f8c8

Browse files
PoojaDurgaddanielleadams
authored andcommitted
lib: use more primordials in shared validators
PR-URL: #36552 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 64bf2f2 commit bb4f8c8

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

lib/internal/validators.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
const {
44
ArrayIsArray,
5+
ArrayPrototypeIncludes,
6+
ArrayPrototypeJoin,
7+
ArrayPrototypeMap,
58
NumberIsInteger,
69
NumberMAX_SAFE_INTEGER,
710
NumberMIN_SAFE_INTEGER,
811
NumberParseInt,
12+
RegExpPrototypeTest,
913
String,
14+
StringPrototypeToUpperCase,
15+
StringPrototypeTrim,
1016
} = primordials;
1117

1218
const {
@@ -63,7 +69,7 @@ function parseFileMode(value, name, def) {
6369
}
6470

6571
if (typeof value === 'string') {
66-
if (!octalReg.test(value)) {
72+
if (!RegExpPrototypeTest(octalReg, value)) {
6773
throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc);
6874
}
6975
return NumberParseInt(value, 8);
@@ -129,10 +135,11 @@ function validateNumber(value, name) {
129135
}
130136

131137
const validateOneOf = hideStackFrames((value, name, oneOf) => {
132-
if (!oneOf.includes(value)) {
133-
const allowed = oneOf
134-
.map((v) => (typeof v === 'string' ? `'${v}'` : String(v)))
135-
.join(', ');
138+
if (!ArrayPrototypeIncludes(oneOf, value)) {
139+
const allowed = ArrayPrototypeJoin(
140+
ArrayPrototypeMap(oneOf, (v) =>
141+
(typeof v === 'string' ? `'${v}'` : String(v))),
142+
', ');
136143
const reason = 'must be one of: ' + allowed;
137144
throw new ERR_INVALID_ARG_VALUE(name, value, reason);
138145
}
@@ -167,7 +174,7 @@ function validateSignalName(signal, name = 'signal') {
167174
throw new ERR_INVALID_ARG_TYPE(name, 'string', signal);
168175

169176
if (signals[signal] === undefined) {
170-
if (signals[signal.toUpperCase()] !== undefined) {
177+
if (signals[StringPrototypeToUpperCase(signal)] !== undefined) {
171178
throw new ERR_UNKNOWN_SIGNAL(signal +
172179
' (signals must use all capital letters)');
173180
}
@@ -198,7 +205,7 @@ function validateEncoding(data, encoding) {
198205
// is an integer and that it falls within the legal range of port numbers.
199206
function validatePort(port, name = 'Port', { allowZero = true } = {}) {
200207
if ((typeof port !== 'number' && typeof port !== 'string') ||
201-
(typeof port === 'string' && port.trim().length === 0) ||
208+
(typeof port === 'string' && StringPrototypeTrim(port).length === 0) ||
202209
+port !== (+port >>> 0) ||
203210
port > 0xFFFF ||
204211
(port === 0 && !allowZero)) {

0 commit comments

Comments
 (0)