-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a file to centralize argument validators that are used in multiple internal modules. Move validateInt32 and validateUint32 to this file. PR-URL: #21149 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com>
- Loading branch information
1 parent
7c0c61b
commit f2c9e5a
Showing
5 changed files
with
76 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict'; | ||
|
||
const { | ||
ERR_INVALID_ARG_TYPE, | ||
ERR_OUT_OF_RANGE | ||
} = require('internal/errors').codes; | ||
|
||
function isInt32(value) { | ||
return value === (value | 0); | ||
} | ||
|
||
function isUint32(value) { | ||
return value === (value >>> 0); | ||
} | ||
|
||
function validateInt32(value, name) { | ||
if (!isInt32(value)) { | ||
let err; | ||
if (typeof value !== 'number') { | ||
err = new ERR_INVALID_ARG_TYPE(name, 'number', value); | ||
} else if (!Number.isInteger(value)) { | ||
err = new ERR_OUT_OF_RANGE(name, 'an integer', value); | ||
} else { | ||
// 2 ** 31 === 2147483648 | ||
err = new ERR_OUT_OF_RANGE(name, '> -2147483649 && < 2147483648', value); | ||
} | ||
Error.captureStackTrace(err, validateInt32); | ||
throw err; | ||
} | ||
} | ||
|
||
function validateUint32(value, name, positive) { | ||
if (!isUint32(value)) { | ||
let err; | ||
if (typeof value !== 'number') { | ||
err = new ERR_INVALID_ARG_TYPE(name, 'number', value); | ||
} else if (!Number.isInteger(value)) { | ||
err = new ERR_OUT_OF_RANGE(name, 'an integer', value); | ||
} else { | ||
const min = positive ? 1 : 0; | ||
// 2 ** 32 === 4294967296 | ||
err = new ERR_OUT_OF_RANGE(name, `>= ${min} && < 4294967296`, value); | ||
} | ||
Error.captureStackTrace(err, validateUint32); | ||
throw err; | ||
} else if (positive && value === 0) { | ||
const err = new ERR_OUT_OF_RANGE(name, '>= 1 && < 4294967296', value); | ||
Error.captureStackTrace(err, validateUint32); | ||
throw err; | ||
} | ||
} | ||
|
||
module.exports = { | ||
isInt32, | ||
isUint32, | ||
validateInt32, | ||
validateUint32 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters