is-number
Advanced tools
Comparing version 6.0.0 to 7.0.0
25
index.js
/*! | ||
* is-number <https://github.com/jonschlinkert/is-number> | ||
* | ||
* Copyright (c) 2014-2018, Jon Schlinkert. | ||
* Copyright (c) 2014-present, Jon Schlinkert. | ||
* Released under the MIT License. | ||
@@ -10,23 +10,10 @@ */ | ||
module.exports = function isNumber(num) { | ||
var number = +num; | ||
if ((number - number) !== 0) { | ||
// Discard Infinity and NaN | ||
return false; | ||
module.exports = function(num) { | ||
if (typeof num === 'number') { | ||
return num - num === 0; | ||
} | ||
if (number === num) { | ||
return true; | ||
if (typeof num === 'string' && num.trim() !== '') { | ||
return Number.isFinite ? Number.isFinite(+num) : isFinite(+num); | ||
} | ||
if (typeof num === 'string') { | ||
// String parsed, both a non-empty whitespace string and an empty string | ||
// will have been coerced to 0. If 0 trim the string and see if its empty. | ||
if (number === 0 && num.trim() === '') { | ||
return false; | ||
} | ||
return true; | ||
} | ||
return false; | ||
}; |
{ | ||
"name": "is-number", | ||
"description": "Returns true if the value is a number. comprehensive tests.", | ||
"version": "6.0.0", | ||
"description": "Returns true if a number or string value is a finite number. Useful for regex matches, parsing, user input, etc.", | ||
"version": "7.0.0", | ||
"homepage": "https://github.com/jonschlinkert/is-number", | ||
@@ -22,3 +22,3 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"engines": { | ||
"node": ">=0.10.0" | ||
"node": ">=0.12.0" | ||
}, | ||
@@ -29,16 +29,21 @@ "scripts": { | ||
"devDependencies": { | ||
"benchmarked": "^2.0.0", | ||
"chalk": "^2.1.0", | ||
"ansi": "^0.3.1", | ||
"benchmark": "^2.1.4", | ||
"gulp-format-md": "^1.0.0", | ||
"mocha": "^3.0.1" | ||
"mocha": "^3.5.3" | ||
}, | ||
"keywords": [ | ||
"cast", | ||
"check", | ||
"coerce", | ||
"coercion", | ||
"finite", | ||
"integer", | ||
"is", | ||
"isnan", | ||
"is-nan", | ||
"is-num", | ||
"is-number", | ||
"isnumber", | ||
"isfinite", | ||
"istype", | ||
@@ -51,2 +56,4 @@ "kind", | ||
"numeric", | ||
"parseFloat", | ||
"parseInt", | ||
"test", | ||
@@ -65,5 +72,5 @@ "type", | ||
"list": [ | ||
"isobject", | ||
"is-plain-object", | ||
"is-primitive", | ||
"isobject", | ||
"kind-of" | ||
@@ -70,0 +77,0 @@ ] |
# 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) | ||
> Returns true if the value is a number. comprehensive tests. | ||
> Returns true if the value is a finite number. | ||
@@ -17,10 +17,15 @@ Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. | ||
This library provides a fast, simple way of checking whether a value is a number, whether defined as a string or number by the user. | ||
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: | ||
It's easy to check for a number in JavaScript, all we need to do is `typeof value === 'number'`. But sometimes that's not enough. For example, `typeof NaN` returns `number`, and there are many use cases where it's possible or necessary for a numerical value to be defined as a string, like in parsers or regex matches where it's not possible for the value to be defined as a number. | ||
```js | ||
console.log(+[]); //=> 0 | ||
console.log(+''); //=> 0 | ||
console.log(+' '); //=> 0 | ||
console.log(typeof NaN); //=> 'number' | ||
``` | ||
This library offers a performant way to smooth out edge cases like these. | ||
## Usage | ||
To understand some of the rationale behind the decisions made in this library (and to learn about some oddities of number evaluation in JavaScript), [see this gist](https://gist.github.com/jonschlinkert/e30c70c713da325d0e81). | ||
```js | ||
@@ -30,6 +35,6 @@ const isNumber = require('is-number'); | ||
See the [tests](./test.js) for more examples. | ||
### true | ||
See the [tests](./test.js) for more examples. | ||
```js | ||
@@ -61,5 +66,11 @@ isNumber(5e3); // true | ||
See the [tests](./test.js) for more examples. | ||
Everything else is false, as you would expect: | ||
```js | ||
isNumber(Infinity); // false | ||
isNumber(NaN); // false | ||
isNumber(null); // false | ||
isNumber(undefined); // false | ||
isNumber(''); // false | ||
isNumber(' '); // false | ||
isNumber('foo'); // false | ||
@@ -69,8 +80,3 @@ isNumber([1]); // false | ||
isNumber(function () {}); // false | ||
isNumber(Infinity); // false | ||
isNumber(NaN); // false | ||
isNumber(new Buffer('abc')); // false | ||
isNumber(null); // false | ||
isNumber(undefined); // false | ||
isNumber({abc: 'abc'}); // false | ||
isNumber({}); // false | ||
``` | ||
@@ -80,5 +86,10 @@ | ||
### 7.0.0 | ||
* Refactor. Now uses `.isFinite` if it exists. | ||
* 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. | ||
### 6.0.0 | ||
* optimizations, thanks to @benaadams. | ||
* Optimizations, thanks to @benaadams. | ||
@@ -91,2 +102,26 @@ ### 5.0.0 | ||
## Benchmarks | ||
As with all benchmarks, take these with a grain of salt. See the [benchmarks](./benchmark/index.js) for more detail. | ||
``` | ||
# all | ||
v7.0 x 413,222 ops/sec ±2.02% (86 runs sampled) | ||
v6.0 x 111,061 ops/sec ±1.29% (85 runs sampled) | ||
parseFloat x 317,596 ops/sec ±1.36% (86 runs sampled) | ||
fastest is 'v7.0' | ||
# string | ||
v7.0 x 3,054,496 ops/sec ±1.05% (89 runs sampled) | ||
v6.0 x 2,957,781 ops/sec ±0.98% (88 runs sampled) | ||
parseFloat x 3,071,060 ops/sec ±1.13% (88 runs sampled) | ||
fastest is 'parseFloat,v7.0' | ||
# number | ||
v7.0 x 3,146,895 ops/sec ±0.89% (89 runs sampled) | ||
v6.0 x 3,214,038 ops/sec ±1.07% (89 runs sampled) | ||
parseFloat x 3,077,588 ops/sec ±1.07% (87 runs sampled) | ||
fastest is 'v6.0' | ||
``` | ||
## About | ||
@@ -138,3 +173,3 @@ | ||
| --- | --- | | ||
| 47 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 49 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 5 | [charlike-old](https://github.com/charlike-old) | | ||
@@ -159,2 +194,2 @@ | 1 | [benaadams](https://github.com/benaadams) | | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on March 31, 2018._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on June 15, 2018._ |
Sorry, the diff of this file is not supported yet
9615
187
16