
Product
Rust Support Now in Beta
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
@toolbuilder/isnumber
Advanced tools
Well tested 'is number' checks, that can optionally include number-like strings, and non-finite values.
Well tested 'is number' checks, that can accept number-like strings, and non-finite values as desired.
There are a lot of is number
tests out there. Many of them don't work quite right. Others use a different
definition of is number
than you might want. So after spending way too much time for such a simple thing,
here are four checks:
Function | number literals | number-like strings | Infinity and NaN | Infinity and NaN strings | other strings |
---|---|---|---|---|---|
isNumeric | true | true | true | true | false |
isNumber | true | false | true | false | false |
isFiniteNumeric | true | true | false | false | false |
isFiniteNumber | true | false | false | false | false |
npm install --save @toolbuilder/isnumber
This is a dual package, so named exports are available for both CommonJS and ES modules.
import { isNumeric, isFiniteNumeric, isNumber, isFiniteNumber } from '@toolbuilder/isnumber'
console.log(isNumeric('1.234')) // true because it looks like a number
console.log(isFiniteNumeric(42.54)) // true
console.log(isNumber('1.234')) // false because it is a string
console.log(isFiniteNumber(-Infinity)) // false
API documentation follows.
Test if n is a number.
Includes Infinities and NaN, does not include strings that look like numbers
Parameters:
n
any value to testisNumber(1.23) // true
isNumber(Infinity) // true
isNumber(NaN) // true
isNumber('1.23') // false
Returns boolean true if is number, false otherwise
Test if n is a number, or string that parses to a number. Includes infinities and NaN.
Non-finite strings are: 'Infinity', '-Infinity', and 'NaN'.
Parameters:
n
any value to testisNumeric(1.23) // true
isNumeric('Infinity') // true
isNumeric(NaN) // true
isNumeric('1.23') // true
isNumeric('hi') // false
Returns boolean true if is numeric, false otherwise
Test if n is a finite number.
Does not include infinities, NaN, or strings that look like numbers.
Parameters:
n
any value to testisFiniteNumber(1.23) // true
isFiniteNumber(Infinity) // false
isFiniteNumber(NaN) // false
isFiniteNumber('1.23') // false
Returns boolean true if is a finite number, false otherwise
Test if n is a finite number, or string that parses to a finite number.
Does not include infinities, NaN
Parameters:
n
any value to testisFiniteNumeric(1.23) // true
isFiniteNumeric('Infinity') // false
isFiniteNumeric(NaN) // false
isFiniteNumeric('1.23') // true
isFiniteNumeric('hi') // false
Returns boolean true if is a finite number, false otherwise
The isFiniteNumeric
implementation, and the most of the tests were copied from this
site. I found these tests on Stack Overflow
in an answer by 'CMS'. Thanks!
Contributions are welcome. Please create a pull request.
pnpm test
npm install -g pnpm
pnpm install
pnpm build
to build cjs, docs, and *.d.tspnpm run check:packfile
to test against ES and CommonJS projects, as well as Electronpnpm run check
to validate the package is ready for commitThis project uses Github issues.
MIT
FAQs
Well tested 'is number' checks, that can optionally include number-like strings, and non-finite values.
We found that @toolbuilder/isnumber demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.