An open API service indexing awesome lists of open source software.

https://github.com/toolbuilder/isnumber

Well tested 'is number' checks, that can accept number-like strings, and non-finite values as desired.
https://github.com/toolbuilder/isnumber

Last synced: about 1 year ago
JSON representation

Well tested 'is number' checks, that can accept number-like strings, and non-finite values as desired.

Awesome Lists containing this project

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](#installation)
* [Getting Started](#getting-started)
* [API](#api)
* [isNumber](#isnumber-1)
* [isNumeric](#isnumeric)
* [isFiniteNumber](#isfinitenumber)
* [isFiniteNumeric](#isfinitenumeric)
* [Credits](#credits)
* [Contributing](#contributing)
* [Issues](#issues)
* [License](#license)

## Installation

```bash
npm install --save @toolbuilder/isnumber
```

## Getting Started

This is a [dual package](https://nodejs.org/dist/latest-v14.x/docs/api/packages.html#packages_dual_commonjs_es_module_packages),
so named exports are available for both CommonJS and ES modules.

```javascript
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

```javascript
isNumber(1.23) // true
isNumber(Infinity) // true
isNumber(NaN) // true
isNumber('1.23') // false
```

Returns **[boolean][1]** 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

```javascript
isNumeric(1.23) // true
isNumeric('Infinity') // true
isNumeric(NaN) // true
isNumeric('1.23') // true
isNumeric('hi') // false
```

Returns **[boolean][1]** 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

```javascript
isFiniteNumber(1.23) // true
isFiniteNumber(Infinity) // false
isFiniteNumber(NaN) // false
isFiniteNumber('1.23') // false
```

Returns **[boolean][1]** 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

```javascript
isFiniteNumeric(1.23) // true
isFiniteNumeric('Infinity') // false
isFiniteNumeric(NaN) // false
isFiniteNumeric('1.23') // true
isFiniteNumeric('hi') // false
```

Returns **[boolean][1]** true if is a finite number, false otherwise

[1]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean

## Credits

The `isFiniteNumeric` implementation, and the most of the tests were copied from this
[site](http://run.plnkr.co/plunks/93FPpacuIcXqqKMecLdk/). I found these tests on Stack Overflow
in an [answer](https://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric) by 'CMS'. Thanks!

## Contributing

Contributions are welcome. Please create a pull request.

* I use [pnpm](https://pnpm.js.org/) instead of npm.
* Run the unit tests with `pnpm test`
* Package verification requires [pnpm](https://pnpm.io/) 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