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.

114 lines
4.3 KiB

4 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 downloads](https://img.shields.io/npm/dm/is-number.svg?style=flat)](https://npmjs.org/package/is-number) [![Build Status](https://img.shields.io/travis/jonschlinkert/is-number.svg?style=flat)](https://travis-ci.org/jonschlinkert/is-number)
  2. > Returns true if the value is a number. comprehensive tests.
  3. ## Install
  4. Install with [npm](https://www.npmjs.com/):
  5. ```sh
  6. $ npm install --save is-number
  7. ```
  8. ## Usage
  9. 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).
  10. ```js
  11. var isNumber = require('is-number');
  12. ```
  13. ### true
  14. See the [tests](./test.js) for more examples.
  15. ```js
  16. isNumber(5e3) //=> 'true'
  17. isNumber(0xff) //=> 'true'
  18. isNumber(-1.1) //=> 'true'
  19. isNumber(0) //=> 'true'
  20. isNumber(1) //=> 'true'
  21. isNumber(1.1) //=> 'true'
  22. isNumber(10) //=> 'true'
  23. isNumber(10.10) //=> 'true'
  24. isNumber(100) //=> 'true'
  25. isNumber('-1.1') //=> 'true'
  26. isNumber('0') //=> 'true'
  27. isNumber('012') //=> 'true'
  28. isNumber('0xff') //=> '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('5e3') //=> 'true'
  35. isNumber(parseInt('012')) //=> 'true'
  36. isNumber(parseFloat('012')) //=> 'true'
  37. ```
  38. ### False
  39. See the [tests](./test.js) for more examples.
  40. ```js
  41. isNumber('foo') //=> 'false'
  42. isNumber([1]) //=> 'false'
  43. isNumber([]) //=> 'false'
  44. isNumber(function () {}) //=> 'false'
  45. isNumber(Infinity) //=> 'false'
  46. isNumber(NaN) //=> 'false'
  47. isNumber(new Array('abc')) //=> 'false'
  48. isNumber(new Array(2)) //=> 'false'
  49. isNumber(new Buffer('abc')) //=> 'false'
  50. isNumber(null) //=> 'false'
  51. isNumber(undefined) //=> 'false'
  52. isNumber({abc: 'abc'}) //=> 'false'
  53. ```
  54. ## About
  55. ### Related projects
  56. * [even](https://www.npmjs.com/package/even): Get the even numbered items from an array. | [homepage](https://github.com/jonschlinkert/even "Get the even numbered items from an array.")
  57. * [is-even](https://www.npmjs.com/package/is-even): Return true if the given number is even. | [homepage](https://github.com/jonschlinkert/is-even "Return true if the given number is even.")
  58. * [is-odd](https://www.npmjs.com/package/is-odd): Returns true if the given number is odd. | [homepage](https://github.com/jonschlinkert/is-odd "Returns true if the given number is odd.")
  59. * [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. ")
  60. * [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.")
  61. * [odd](https://www.npmjs.com/package/odd): Get the odd numbered items from an array. | [homepage](https://github.com/jonschlinkert/odd "Get the odd numbered items from an array.")
  62. ### Contributing
  63. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  64. ### Building docs
  65. _(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_
  66. To generate the readme and API documentation with [verb](https://github.com/verbose/verb):
  67. ```sh
  68. $ npm install -g verb verb-generate-readme && verb
  69. ```
  70. ### Running tests
  71. Install dev dependencies:
  72. ```sh
  73. $ npm install -d && npm test
  74. ```
  75. ### Author
  76. **Jon Schlinkert**
  77. * [github/jonschlinkert](https://github.com/jonschlinkert)
  78. * [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
  79. ### License
  80. Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert).
  81. Released under the [MIT license](https://github.com/jonschlinkert/is-number/blob/master/LICENSE).
  82. ***
  83. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.1.30, on September 10, 2016._