◐ Shell
clean mode source ↗

lib: add validateInteger() validator · nodejs/node@a0cfb0c

@@ -13,7 +13,21 @@ function isUint32(value) {

1313

return value === (value >>> 0);

1414

}

151516-

function validateInt32(value, name) {

16+

function validateInteger(value, name) {

17+

let err;

18+19+

if (typeof value !== 'number')

20+

err = new ERR_INVALID_ARG_TYPE(name, 'number', value);

21+

else if (!Number.isSafeInteger(value))

22+

err = new ERR_OUT_OF_RANGE(name, 'an integer', value);

23+24+

if (err) {

25+

Error.captureStackTrace(err, validateInteger);

26+

throw err;

27+

}

28+

}

29+30+

function validateInt32(value, name, min = -2147483648, max = 2147483647) {

1731

if (!isInt32(value)) {

1832

let err;

1933

if (typeof value !== 'number') {

@@ -53,6 +67,7 @@ function validateUint32(value, name, positive) {

5367

module.exports = {

5468

isInt32,

5569

isUint32,

70+

validateInteger,

5671

validateInt32,

5772

validateUint32

5873

};