Skip to content

Commit 98480ae

Browse files
committed
lib: refactor to use validateNumber
1 parent fef2128 commit 98480ae

File tree

4 files changed

+13
-15
lines changed

4 files changed

+13
-15
lines changed

lib/buffer.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ const {
101101
} = require('internal/errors');
102102
const {
103103
validateBuffer,
104+
validateNumber,
104105
validateInteger,
105106
validateString
106107
} = require('internal/validators');
@@ -345,9 +346,7 @@ ObjectSetPrototypeOf(Buffer, Uint8Array);
345346
// occurs. This is done simply to keep the internal details of the
346347
// implementation from bleeding out to users.
347348
const assertSize = hideStackFrames((size) => {
348-
if (typeof size !== 'number') {
349-
throw new ERR_INVALID_ARG_TYPE('size', 'number', size);
350-
}
349+
validateNumber(size, 'size');
351350
if (!(size >= 0 && size <= kMaxLength)) {
352351
throw new ERR_INVALID_ARG_VALUE.RangeError('size', size);
353352
}

lib/internal/crypto/util.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const {
4242

4343
const {
4444
validateArray,
45+
validateNumber,
4546
validateString
4647
} = require('internal/validators');
4748

@@ -104,8 +105,8 @@ const getCurves = cachedResult(() => filterDuplicateStrings(_getCurves()));
104105

105106
function setEngine(id, flags) {
106107
validateString(id, 'id');
107-
if (flags && typeof flags !== 'number')
108-
throw new ERR_INVALID_ARG_TYPE('flags', 'number', flags);
108+
if (flags) {
109+
validateNumber(flags, 'flags');
109110
flags = flags >>> 0;
110111

111112
// Use provided engine for everything by default
@@ -244,8 +245,7 @@ function hasAnyNotIn(set, ...check) {
244245

245246
function validateBitLength(length, name, required = false) {
246247
if (length !== undefined || required) {
247-
if (typeof length !== 'number')
248-
throw new ERR_INVALID_ARG_TYPE(name, 'number', length);
248+
validateNumber(length, name);
249249
if (length < 0)
250250
throw new ERR_OUT_OF_RANGE(name, '> 0');
251251
if (length % 8) {

lib/internal/histogram.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ const {
77
const { format } = require('util');
88
const { NumberIsNaN, SafeMap, Symbol } = primordials;
99

10-
const {
11-
ERR_INVALID_ARG_TYPE,
12-
ERR_INVALID_ARG_VALUE,
13-
} = require('internal/errors').codes;
10+
const { ERR_INVALID_ARG_VALUE } = require('internal/errors').codes;
1411

1512
const kDestroy = Symbol('kDestroy');
1613
const kHandle = Symbol('kHandle');
1714

15+
const { validateNumber } = require('internal/validators');
16+
1817
// Histograms are created internally by Node.js and used to
1918
// record various metrics. This Histogram class provides a
2019
// generally read-only view of the internal histogram.
@@ -58,8 +57,7 @@ class Histogram {
5857
}
5958

6059
percentile(percentile) {
61-
if (typeof percentile !== 'number')
62-
throw new ERR_INVALID_ARG_TYPE('percentile', 'number', percentile);
60+
validateNumber(percentile, 'percentile');
6361

6462
if (NumberIsNaN(percentile) || percentile <= 0 || percentile > 100)
6563
throw new ERR_INVALID_ARG_VALUE.RangeError('percentile', percentile);

lib/net.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ const { isUint8Array } = require('internal/util/types');
111111
const {
112112
validateAbortSignal,
113113
validateInt32,
114+
validateNumber,
114115
validatePort,
115116
validateString
116117
} = require('internal/validators');
@@ -987,8 +988,8 @@ function lookupAndConnect(self, options) {
987988
throw new ERR_INVALID_IP_ADDRESS(localAddress);
988989
}
989990

990-
if (localPort && typeof localPort !== 'number') {
991-
throw new ERR_INVALID_ARG_TYPE('options.localPort', 'number', localPort);
991+
if (localPort) {
992+
validateNumber(localPort, 'options.localPort');
992993
}
993994

994995
if (typeof port !== 'undefined') {

0 commit comments

Comments
 (0)