Skip to content

Commit

Permalink
lib: refactor to use validateNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
Lxxyx committed Jan 20, 2021
1 parent fef2128 commit b6ceabb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
5 changes: 2 additions & 3 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const {
} = require('internal/errors');
const {
validateBuffer,
validateNumber,
validateInteger,
validateString
} = require('internal/validators');
Expand Down Expand Up @@ -345,9 +346,7 @@ ObjectSetPrototypeOf(Buffer, Uint8Array);
// occurs. This is done simply to keep the internal details of the
// implementation from bleeding out to users.
const assertSize = hideStackFrames((size) => {
if (typeof size !== 'number') {
throw new ERR_INVALID_ARG_TYPE('size', 'number', size);
}
validateNumber(size, 'size');
if (!(size >= 0 && size <= kMaxLength)) {
throw new ERR_INVALID_ARG_VALUE.RangeError('size', size);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const {

const {
validateArray,
validateNumber,
validateString
} = require('internal/validators');

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

function setEngine(id, flags) {
validateString(id, 'id');
if (flags && typeof flags !== 'number')
throw new ERR_INVALID_ARG_TYPE('flags', 'number', flags);
if (flags)
validateNumber(flags, 'flags');
flags = flags >>> 0;

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

function validateBitLength(length, name, required = false) {
if (length !== undefined || required) {
if (typeof length !== 'number')
throw new ERR_INVALID_ARG_TYPE(name, 'number', length);
validateNumber(length, name);
if (length < 0)
throw new ERR_OUT_OF_RANGE(name, '> 0');
if (length % 8) {
Expand Down
9 changes: 3 additions & 6 deletions lib/internal/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ const {
const { format } = require('util');
const { NumberIsNaN, SafeMap, Symbol } = primordials;

const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
} = require('internal/errors').codes;
const { ERR_INVALID_ARG_VALUE } = require('internal/errors').codes;
const { validateNumber } = require('internal/validators');

const kDestroy = Symbol('kDestroy');
const kHandle = Symbol('kHandle');
Expand Down Expand Up @@ -58,8 +56,7 @@ class Histogram {
}

percentile(percentile) {
if (typeof percentile !== 'number')
throw new ERR_INVALID_ARG_TYPE('percentile', 'number', percentile);
validateNumber(percentile, 'percentile');

if (NumberIsNaN(percentile) || percentile <= 0 || percentile > 100)
throw new ERR_INVALID_ARG_VALUE.RangeError('percentile', percentile);
Expand Down
5 changes: 3 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const { isUint8Array } = require('internal/util/types');
const {
validateAbortSignal,
validateInt32,
validateNumber,
validatePort,
validateString
} = require('internal/validators');
Expand Down Expand Up @@ -987,8 +988,8 @@ function lookupAndConnect(self, options) {
throw new ERR_INVALID_IP_ADDRESS(localAddress);
}

if (localPort && typeof localPort !== 'number') {
throw new ERR_INVALID_ARG_TYPE('options.localPort', 'number', localPort);
if (localPort) {
validateNumber(localPort, 'options.localPort');
}

if (typeof port !== 'undefined') {
Expand Down

0 comments on commit b6ceabb

Please sign in to comment.