diff options
Diffstat (limited to 'std/node/_utils.ts')
-rw-r--r-- | std/node/_utils.ts | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/std/node/_utils.ts b/std/node/_utils.ts index 0e8b26fb5..5bd4d1075 100644 --- a/std/node/_utils.ts +++ b/std/node/_utils.ts @@ -111,3 +111,21 @@ function slowCases(enc: string): string | undefined { if (enc === "") return "utf8"; } } + +export function validateIntegerRange( + value: number, + name: string, + min = -2147483648, + max = 2147483647, +): void { + // The defaults for min and max correspond to the limits of 32-bit integers. + if (!Number.isInteger(value)) { + throw new Error(`${name} must be 'an integer' but was ${value}`); + } + + if (value < min || value > max) { + throw new Error( + `${name} must be >= ${min} && <= ${max}. Value was ${value}`, + ); + } +} |