is-safe-number
v1.0.0
Published
Returns true if every input is a number that is safe to compute with.
Maintainers
Readme
is-safe-number
Returns true if every input is a number that is safe to compute with: a primitive number, finite, and within the safe integer range.
It validates the inputs of is-nodd and is-throdd, and through them is-neven and is-threeven.
Install
npm install is-safe-numberUsage
This package is ESM only.
import isSafeNumber, { isSafeDivisor } from 'is-safe-number';
isSafeNumber(42); // true
isSafeNumber(1.5, -3, 0); // true
isSafeNumber('42'); // false
isSafeNumber(NaN); // false
isSafeNumber(Number.MAX_SAFE_INTEGER + 1); // false
isSafeDivisor(3); // true
isSafeDivisor(0); // falseBehavior
isSafeNumber(...values) returns true only when every value passes all of these checks:
- It is a primitive
number. Numeric strings,null,undefined, booleans,bigintand boxedNumberobjects are rejected. - It is finite.
NaN,Infinityand-Infinityare rejected. - Its absolute value is at most
Number.MAX_SAFE_INTEGER(2^53 - 1). Larger values such as1e300orNumber.MAX_VALUEare rejected because arithmetic on them loses precision or overflows.
With no inputs it returns false, since there is nothing to vouch for.
Non-integers such as 1.5 are safe. 0 and -0 are safe numbers.
isSafeDivisor(...values) applies the same checks and additionally rejects 0 and -0, because dividing by zero yields Infinity or NaN.
Development
Tests use the built-in Node.js test runner. No dependencies are required.
npm testPublishing
Only index.js, package.json, README.md and LICENSE are included in the package tarball. Verify with:
npm pack --dry-run