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 |
|
5 | Please 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 |
|
9 | Install with [npm](https://www.npmjs.com/):
|
10 |
|
11 | ```sh
|
12 | $ npm install --save is-number
|
13 | ```
|
14 |
|
15 | ## Why is this needed?
|
16 |
|
17 | In 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
|
20 | console.log(+[]); //=> 0
|
21 | console.log(+''); //=> 0
|
22 | console.log(+' '); //=> 0
|
23 | console.log(typeof NaN); //=> 'number'
|
24 | ```
|
25 |
|
26 | This library offers a performant way to smooth out edge cases like these.
|
27 |
|
28 | ## Usage
|
29 |
|
30 | ```js
|
31 | const isNumber = require('is-number');
|
32 | ```
|
33 |
|
34 | See the [tests](./test.js) for more examples.
|
35 |
|
36 | ### true
|
37 |
|
38 | ```js
|
39 | isNumber(5e3); // true
|
40 | isNumber(0xff); // true
|
41 | isNumber(-1.1); // true
|
42 | isNumber(0); // true
|
43 | isNumber(1); // true
|
44 | isNumber(1.1); // true
|
45 | isNumber(10); // true
|
46 | isNumber(10.10); // true
|
47 | isNumber(100); // true
|
48 | isNumber('-1.1'); // true
|
49 | isNumber('0'); // true
|
50 | isNumber('012'); // true
|
51 | isNumber('0xff'); // true
|
52 | isNumber('1'); // true
|
53 | isNumber('1.1'); // true
|
54 | isNumber('10'); // true
|
55 | isNumber('10.10'); // true
|
56 | isNumber('100'); // true
|
57 | isNumber('5e3'); // true
|
58 | isNumber(parseInt('012')); // true
|
59 | isNumber(parseFloat('012')); // true
|
60 | ```
|
61 |
|
62 | ### False
|
63 |
|
64 | Everything else is false, as you would expect:
|
65 |
|
66 | ```js
|
67 | isNumber(Infinity); // false
|
68 | isNumber(NaN); // false
|
69 | isNumber(null); // false
|
70 | isNumber(undefined); // false
|
71 | isNumber(''); // false
|
72 | isNumber(' '); // false
|
73 | isNumber('foo'); // false
|
74 | isNumber([1]); // false
|
75 | isNumber([]); // false
|
76 | isNumber(function () {}); // false
|
77 | isNumber({}); // 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 |
|
99 | As with all benchmarks, take these with a grain of salt. See the [benchmarks](./benchmark/index.js) for more detail.
|
100 |
|
101 | ```
|
102 | # all
|
103 | v7.0 x 413,222 ops/sec ±2.02% (86 runs sampled)
|
104 | v6.0 x 111,061 ops/sec ±1.29% (85 runs sampled)
|
105 | parseFloat x 317,596 ops/sec ±1.36% (86 runs sampled)
|
106 | fastest is 'v7.0'
|
107 |
|
108 | # string
|
109 | v7.0 x 3,054,496 ops/sec ±1.05% (89 runs sampled)
|
110 | v6.0 x 2,957,781 ops/sec ±0.98% (88 runs sampled)
|
111 | parseFloat x 3,071,060 ops/sec ±1.13% (88 runs sampled)
|
112 | fastest is 'parseFloat,v7.0'
|
113 |
|
114 | # number
|
115 | v7.0 x 3,146,895 ops/sec ±0.89% (89 runs sampled)
|
116 | v6.0 x 3,214,038 ops/sec ±1.07% (89 runs sampled)
|
117 | parseFloat x 3,077,588 ops/sec ±1.07% (87 runs sampled)
|
118 | fastest is 'v6.0'
|
119 | ```
|
120 |
|
121 | ## About
|
122 |
|
123 | <details>
|
124 | <summary><strong>Contributing</strong></summary>
|
125 |
|
126 | Pull 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 |
|
133 | Running 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 |
|
146 | To 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 |
|
156 | You 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 |
|
182 | Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
|
183 | Released 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 |