npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@toolbuilder/isnumber

v0.1.7

Published

Well tested 'is number' checks, that can optionally include number-like strings, and non-finite values.

Downloads

504

Readme

IsNumber

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 |

Table of Contents

Installation

npm install --save @toolbuilder/isnumber

Getting Started

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

API documentation follows.

isNumber

Test if n is a number.

Includes Infinities and NaN, does not include strings that look like numbers

Parameters:

  • n any value to test
isNumber(1.23) // true
isNumber(Infinity) // true
isNumber(NaN) // true
isNumber('1.23') // false

Returns boolean true if is number, false otherwise

isNumeric

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 test
isNumeric(1.23) // true
isNumeric('Infinity') // true
isNumeric(NaN) // true
isNumeric('1.23') // true
isNumeric('hi') // false

Returns boolean true if is numeric, false otherwise

isFiniteNumber

Test if n is a finite number.

Does not include infinities, NaN, or strings that look like numbers.

Parameters:

  • n any value to test
isFiniteNumber(1.23) // true
isFiniteNumber(Infinity) // false
isFiniteNumber(NaN) // false
isFiniteNumber('1.23') // false

Returns boolean true if is a finite number, false otherwise

isFiniteNumeric

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 test
isFiniteNumeric(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

Credits

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!

Contributing

Contributions are welcome. Please create a pull request.

  • I use pnpm instead of npm.
  • Run the unit tests with pnpm test
  • Package verification requires pnpm to be installed globally.
    • npm install -g pnpm
    • pnpm install
    • pnpm build to build cjs, docs, and *.d.ts
    • pnpm run check:packfile to test against ES and CommonJS projects, as well as Electron
    • pnpm run check to validate the package is ready for commit

Issues

This project uses Github issues.

License

MIT