You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
6.4 KiB

2 years ago
  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. > Returns true if the value is a finite number.
  3. Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
  4. ## Install
  5. Install with [npm](https://www.npmjs.com/):
  6. ```sh
  7. $ npm install --save is-number
  8. ```
  9. ## Why is this needed?
  10. 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:
  11. ```js
  12. console.log(+[]); //=> 0
  13. console.log(+''); //=> 0
  14. console.log(+' '); //=> 0
  15. console.log(typeof NaN); //=> 'number'
  16. ```
  17. This library offers a performant way to smooth out edge cases like these.
  18. ## Usage
  19. ```js
  20. const isNumber = require('is-number');
  21. ```
  22. See the [tests](./test.js) for more examples.
  23. ### true
  24. ```js
  25. isNumber(5e3); // true
  26. isNumber(0xff); // true
  27. isNumber(-1.1); // true
  28. isNumber(0); // true
  29. isNumber(1); // true
  30. isNumber(1.1); // true
  31. isNumber(10); // true
  32. isNumber(10.10); // true
  33. isNumber(100); // true
  34. isNumber('-1.1'); // true
  35. isNumber('0'); // true
  36. isNumber('012'); // true
  37. isNumber('0xff'); // true
  38. isNumber('1'); // true
  39. isNumber('1.1'); // true
  40. isNumber('10'); // true
  41. isNumber('10.10'); // true
  42. isNumber('100'); // true
  43. isNumber('5e3'); // true
  44. isNumber(parseInt('012')); // true
  45. isNumber(parseFloat('012')); // true
  46. ```
  47. ### False
  48. Everything else is false, as you would expect:
  49. ```js
  50. isNumber(Infinity); // false
  51. isNumber(NaN); // false
  52. isNumber(null); // false
  53. isNumber(undefined); // false
  54. isNumber(''); // false
  55. isNumber(' '); // false
  56. isNumber('foo'); // false
  57. isNumber([1]); // false
  58. isNumber([]); // false
  59. isNumber(function () {}); // false
  60. isNumber({}); // false
  61. ```
  62. ## Release history
  63. ### 7.0.0
  64. * Refactor. Now uses `.isFinite` if it exists.
  65. * 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.
  66. ### 6.0.0
  67. * Optimizations, thanks to @benaadams.
  68. ### 5.0.0
  69. **Breaking changes**
  70. * removed support for `instanceof Number` and `instanceof String`
  71. ## Benchmarks
  72. As with all benchmarks, take these with a grain of salt. See the [benchmarks](./benchmark/index.js) for more detail.
  73. ```
  74. # all
  75. v7.0 x 413,222 ops/sec ±2.02% (86 runs sampled)
  76. v6.0 x 111,061 ops/sec ±1.29% (85 runs sampled)
  77. parseFloat x 317,596 ops/sec ±1.36% (86 runs sampled)
  78. fastest is 'v7.0'
  79. # string
  80. v7.0 x 3,054,496 ops/sec ±1.05% (89 runs sampled)
  81. v6.0 x 2,957,781 ops/sec ±0.98% (88 runs sampled)
  82. parseFloat x 3,071,060 ops/sec ±1.13% (88 runs sampled)
  83. fastest is 'parseFloat,v7.0'
  84. # number
  85. v7.0 x 3,146,895 ops/sec ±0.89% (89 runs sampled)
  86. v6.0 x 3,214,038 ops/sec ±1.07% (89 runs sampled)
  87. parseFloat x 3,077,588 ops/sec ±1.07% (87 runs sampled)
  88. fastest is 'v6.0'
  89. ```
  90. ## About
  91. <details>
  92. <summary><strong>Contributing</strong></summary>
  93. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  94. </details>
  95. <details>
  96. <summary><strong>Running Tests</strong></summary>
  97. 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:
  98. ```sh
  99. $ npm install && npm test
  100. ```
  101. </details>
  102. <details>
  103. <summary><strong>Building docs</strong></summary>
  104. _(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.)_
  105. To generate the readme, run the following command:
  106. ```sh
  107. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  108. ```
  109. </details>
  110. ### Related projects
  111. You might also be interested in these projects:
  112. * [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.")
  113. * [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. ")
  114. * [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.")
  115. * [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.")
  116. ### Contributors
  117. | **Commits** | **Contributor** |
  118. | --- | --- |
  119. | 49 | [jonschlinkert](https://github.com/jonschlinkert) |
  120. | 5 | [charlike-old](https://github.com/charlike-old) |
  121. | 1 | [benaadams](https://github.com/benaadams) |
  122. | 1 | [realityking](https://github.com/realityking) |
  123. ### Author
  124. **Jon Schlinkert**
  125. * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
  126. * [GitHub Profile](https://github.com/jonschlinkert)
  127. * [Twitter Profile](https://twitter.com/jonschlinkert)
  128. ### License
  129. Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
  130. Released under the [MIT License](LICENSE).
  131. ***
  132. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on June 15, 2018._