UNPKG

6.51 kBMarkdownView Raw
1# is-number [![NPM version](https://img.shields.io/npm/v/is-number.svg?style=flat)](https://www.npmjs.com/package/is-number) [![NPM monthly downloads](https://img.shields.io/npm/dm/is-number.svg?style=flat)](https://npmjs.org/package/is-number) [![NPM total downloads](https://img.shields.io/npm/dt/is-number.svg?style=flat)](https://npmjs.org/package/is-number) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/is-number.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/is-number)
2
3> Returns true if the value is a finite number.
4
5Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
6
7## Install
8
9Install with [npm](https://www.npmjs.com/):
10
11```sh
12$ npm install --save is-number
13```
14
15## Why is this needed?
16
17In JavaScript, it's not always as straightforward as it should be to reliably check if a value is a number. It's common for devs to use `+`, `-`, or `Number()` to cast a string value to a number (for example, when values are returned from user input, regex matches, parsers, etc). But there are many non-intuitive edge cases that yield unexpected results:
18
19```js
20console.log(+[]); //=> 0
21console.log(+''); //=> 0
22console.log(+' '); //=> 0
23console.log(typeof NaN); //=> 'number'
24```
25
26This library offers a performant way to smooth out edge cases like these.
27
28## Usage
29
30```js
31const isNumber = require('is-number');
32```
33
34See the [tests](./test.js) for more examples.
35
36### true
37
38```js
39isNumber(5e3); // true
40isNumber(0xff); // true
41isNumber(-1.1); // true
42isNumber(0); // true
43isNumber(1); // true
44isNumber(1.1); // true
45isNumber(10); // true
46isNumber(10.10); // true
47isNumber(100); // true
48isNumber('-1.1'); // true
49isNumber('0'); // true
50isNumber('012'); // true
51isNumber('0xff'); // true
52isNumber('1'); // true
53isNumber('1.1'); // true
54isNumber('10'); // true
55isNumber('10.10'); // true
56isNumber('100'); // true
57isNumber('5e3'); // true
58isNumber(parseInt('012')); // true
59isNumber(parseFloat('012')); // true
60```
61
62### False
63
64Everything else is false, as you would expect:
65
66```js
67isNumber(Infinity); // false
68isNumber(NaN); // false
69isNumber(null); // false
70isNumber(undefined); // false
71isNumber(''); // false
72isNumber(' '); // false
73isNumber('foo'); // false
74isNumber([1]); // false
75isNumber([]); // false
76isNumber(function () {}); // false
77isNumber({}); // false
78```
79
80## Release history
81
82### 7.0.0
83
84* Refactor. Now uses `.isFinite` if it exists.
85* Performance is about the same as v6.0 when the value is a string or number. But it's now 3x-4x faster when the value is not a string or number.
86
87### 6.0.0
88
89* Optimizations, thanks to @benaadams.
90
91### 5.0.0
92
93**Breaking changes**
94
95* removed support for `instanceof Number` and `instanceof String`
96
97## Benchmarks
98
99As with all benchmarks, take these with a grain of salt. See the [benchmarks](./benchmark/index.js) for more detail.
100
101```
102# all
103v7.0 x 413,222 ops/sec ±2.02% (86 runs sampled)
104v6.0 x 111,061 ops/sec ±1.29% (85 runs sampled)
105parseFloat x 317,596 ops/sec ±1.36% (86 runs sampled)
106fastest is 'v7.0'
107
108# string
109v7.0 x 3,054,496 ops/sec ±1.05% (89 runs sampled)
110v6.0 x 2,957,781 ops/sec ±0.98% (88 runs sampled)
111parseFloat x 3,071,060 ops/sec ±1.13% (88 runs sampled)
112fastest is 'parseFloat,v7.0'
113
114# number
115v7.0 x 3,146,895 ops/sec ±0.89% (89 runs sampled)
116v6.0 x 3,214,038 ops/sec ±1.07% (89 runs sampled)
117parseFloat x 3,077,588 ops/sec ±1.07% (87 runs sampled)
118fastest is 'v6.0'
119```
120
121## About
122
123<details>
124<summary><strong>Contributing</strong></summary>
125
126Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
127
128</details>
129
130<details>
131<summary><strong>Running Tests</strong></summary>
132
133Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
134
135```sh
136$ npm install && npm test
137```
138
139</details>
140
141<details>
142<summary><strong>Building docs</strong></summary>
143
144_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
145
146To generate the readme, run the following command:
147
148```sh
149$ npm install -g verbose/verb#dev verb-generate-readme && verb
150```
151
152</details>
153
154### Related projects
155
156You might also be interested in these projects:
157
158* [is-plain-object](https://www.npmjs.com/package/is-plain-object): Returns true if an object was created by the `Object` constructor. | [homepage](https://github.com/jonschlinkert/is-plain-object "Returns true if an object was created by the `Object` constructor.")
159* [is-primitive](https://www.npmjs.com/package/is-primitive): Returns `true` if the value is a primitive. | [homepage](https://github.com/jonschlinkert/is-primitive "Returns `true` if the value is a primitive. ")
160* [isobject](https://www.npmjs.com/package/isobject): Returns true if the value is an object and not an array or null. | [homepage](https://github.com/jonschlinkert/isobject "Returns true if the value is an object and not an array or null.")
161* [kind-of](https://www.npmjs.com/package/kind-of): Get the native type of a value. | [homepage](https://github.com/jonschlinkert/kind-of "Get the native type of a value.")
162
163### Contributors
164
165| **Commits** | **Contributor** |
166| --- | --- |
167| 49 | [jonschlinkert](https://github.com/jonschlinkert) |
168| 5 | [charlike-old](https://github.com/charlike-old) |
169| 1 | [benaadams](https://github.com/benaadams) |
170| 1 | [realityking](https://github.com/realityking) |
171
172### Author
173
174**Jon Schlinkert**
175
176* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
177* [GitHub Profile](https://github.com/jonschlinkert)
178* [Twitter Profile](https://twitter.com/jonschlinkert)
179
180### License
181
182Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
183Released under the [MIT License](LICENSE).
184
185***
186
187_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on June 15, 2018._
\No newline at end of file