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.

readme.md 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. # is [![Build Status](https://travis-ci.org/sindresorhus/is.svg?branch=master)](https://travis-ci.org/sindresorhus/is)
  2. > Type check values: `is.string('🦄') //=> true`
  3. <img src="header.gif" width="182" align="right">
  4. ## Install
  5. ```
  6. $ npm install @sindresorhus/is
  7. ```
  8. ## Usage
  9. ```js
  10. const is = require('@sindresorhus/is');
  11. is('🦄');
  12. //=> 'string'
  13. is(new Map());
  14. //=> 'Map'
  15. is.number(6);
  16. //=> true
  17. ```
  18. ## API
  19. ### is(value)
  20. Returns the type of `value`.
  21. Primitives are lowercase and object types are camelcase.
  22. Example:
  23. - `'undefined'`
  24. - `'null'`
  25. - `'string'`
  26. - `'symbol'`
  27. - `'Array'`
  28. - `'Function'`
  29. - `'Object'`
  30. Note: It will throw if you try to feed it object-wrapped primitives, as that's a bad practice. For example `new String('foo')`.
  31. ### is.{method}
  32. All the below methods accept a value and returns a boolean for whether the value is of the desired type.
  33. #### Primitives
  34. ##### .undefined(value)
  35. ##### .null(value)
  36. ##### .string(value)
  37. ##### .number(value)
  38. ##### .boolean(value)
  39. ##### .symbol(value)
  40. #### Built-in types
  41. ##### .array(value)
  42. ##### .function(value)
  43. ##### .buffer(value)
  44. ##### .object(value)
  45. Keep in mind that [functions are objects too](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions).
  46. ##### .regExp(value)
  47. ##### .date(value)
  48. ##### .error(value)
  49. ##### .nativePromise(value)
  50. ##### .promise(value)
  51. Returns `true` for any object with a `.then()` and `.catch()` method. Prefer this one over `.nativePromise()` as you usually want to allow userland promise implementations too.
  52. ##### .generator(value)
  53. Returns `true` for any object that implements its own `.next()` and `.throw()` methods and has a function definition for `Symbol.iterator`.
  54. ##### .generatorFunction(value)
  55. ##### .asyncFunction(value)
  56. Returns `true` for any `async` function that can be called with the `await` operator.
  57. ```js
  58. is.asyncFunction(async () => {});
  59. // => true
  60. is.asyncFunction(() => {});
  61. // => false
  62. ```
  63. ##### .boundFunction(value)
  64. Returns `true` for any `bound` function.
  65. ```js
  66. is.boundFunction(() => {});
  67. // => true
  68. is.boundFunction(function () {}.bind(null));
  69. // => true
  70. is.boundFunction(function () {});
  71. // => false
  72. ```
  73. ##### .map(value)
  74. ##### .set(value)
  75. ##### .weakMap(value)
  76. ##### .weakSet(value)
  77. #### Typed arrays
  78. ##### .int8Array(value)
  79. ##### .uint8Array(value)
  80. ##### .uint8ClampedArray(value)
  81. ##### .int16Array(value)
  82. ##### .uint16Array(value)
  83. ##### .int32Array(value)
  84. ##### .uint32Array(value)
  85. ##### .float32Array(value)
  86. ##### .float64Array(value)
  87. #### Structured data
  88. ##### .arrayBuffer(value)
  89. ##### .sharedArrayBuffer(value)
  90. ##### .dataView(value)
  91. #### Miscellaneous
  92. ##### .directInstanceOf(value, class)
  93. Returns `true` if `value` is a direct instance of `class`.
  94. ```js
  95. is.directInstanceOf(new Error(), Error);
  96. //=> true
  97. class UnicornError extends Error {};
  98. is.directInstanceOf(new UnicornError(), Error);
  99. //=> false
  100. ```
  101. ##### .truthy(value)
  102. Returns `true` for all values that evaluate to true in a boolean context:
  103. ```js
  104. is.truthy('🦄');
  105. //=> true
  106. is.truthy(undefined);
  107. //=> false
  108. ```
  109. ##### .falsy(value)
  110. Returns `true` if `value` is one of: `false`, `0`, `''`, `null`, `undefined`, `NaN`.
  111. ##### .nan(value)
  112. ##### .nullOrUndefined(value)
  113. ##### .primitive(value)
  114. JavaScript primitives are as follows: `null`, `undefined`, `string`, `number`, `boolean`, `symbol`.
  115. ##### .integer(value)
  116. ##### .safeInteger(value)
  117. Returns `true` if `value` is a [safe integer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger).
  118. ##### .plainObject(value)
  119. An object is plain if it's created by either `{}`, `new Object()`, or `Object.create(null)`.
  120. ##### .iterable(value)
  121. ##### .class(value)
  122. Returns `true` for instances created by a ES2015 class.
  123. ##### .typedArray(value)
  124. ##### .arrayLike(value)
  125. A `value` is array-like if it is not a function and has a `value.length` that is a safe integer greater than or equal to 0.
  126. ```js
  127. is.arrayLike(document.forms);
  128. //=> true
  129. function () {
  130. is.arrayLike(arguments);
  131. //=> true
  132. }
  133. ```
  134. ##### .inRange(value, range)
  135. Check if `value` (number) is in the given `range`. The range is an array of two values, lower bound and upper bound, in no specific order.
  136. ```js
  137. is.inRange(3, [0, 5]);
  138. is.inRange(3, [5, 0]);
  139. is.inRange(0, [-2, 2]);
  140. ```
  141. ##### .inRange(value, upperBound)
  142. Check if `value` (number) is in the range of `0` to `upperBound`.
  143. ```js
  144. is.inRange(3, 10);
  145. ```
  146. ##### .domElement(value)
  147. Returns `true` if `value` is a DOM Element.
  148. ##### .nodeStream(value)
  149. Returns `true` if `value` is a Node.js [stream](https://nodejs.org/api/stream.html).
  150. ```js
  151. const fs = require('fs');
  152. is.nodeStream(fs.createReadStream('unicorn.png'));
  153. //=> true
  154. ```
  155. ##### .infinite(value)
  156. Check if `value` is `Infinity` or `-Infinity`.
  157. ##### .even(value)
  158. Returns `true` if `value` is an even integer.
  159. ##### .odd(value)
  160. Returns `true` if `value` is an odd integer.
  161. ##### .empty(value)
  162. Returns `true` if `value` is falsy or an empty string, array, object, map, or set.
  163. ##### .emptyOrWhitespace(value)
  164. Returns `true` if `is.empty(value)` or a string that is all whitespace.
  165. ##### .any(predicate, ...values)
  166. Returns `true` if **any** of the input `values` returns true in the `predicate`:
  167. ```js
  168. is.any(is.string, {}, true, '🦄');
  169. //=> true
  170. is.any(is.boolean, 'unicorns', [], new Map());
  171. //=> false
  172. ```
  173. ##### .all(predicate, ...values)
  174. Returns `true` if **all** of the input `values` returns true in the `predicate`:
  175. ```js
  176. is.all(is.object, {}, new Map(), new Set());
  177. //=> true
  178. is.all(is.string, '🦄', [], 'unicorns');
  179. //=> false
  180. ```
  181. ## FAQ
  182. ### Why yet another type checking module?
  183. There are hundreds of type checking modules on npm, unfortunately, I couldn't find any that fit my needs:
  184. - Includes both type methods and ability to get the type
  185. - Types of primitives returned as lowercase and object types as camelcase
  186. - Covers all built-ins
  187. - Unsurprising behavior
  188. - Well-maintained
  189. - Comprehensive test suite
  190. For the ones I found, pick 3 of these.
  191. The most common mistakes I noticed in these modules was using `instanceof` for type checking, forgetting that functions are objects, and omitting `symbol` as a primitive.
  192. ## Related
  193. - [is-stream](https://github.com/sindresorhus/is-stream) - Check if something is a Node.js stream
  194. - [is-observable](https://github.com/sindresorhus/is-observable) - Check if a value is an Observable
  195. - [file-type](https://github.com/sindresorhus/file-type) - Detect the file type of a Buffer/Uint8Array
  196. - [is-ip](https://github.com/sindresorhus/is-ip) - Check if a string is an IP address
  197. - [is-array-sorted](https://github.com/sindresorhus/is-array-sorted) - Check if an Array is sorted
  198. - [is-error-constructor](https://github.com/sindresorhus/is-error-constructor) - Check if a value is an error constructor
  199. - [is-empty-iterable](https://github.com/sindresorhus/is-empty-iterable) - Check if an Iterable is empty
  200. - [is-blob](https://github.com/sindresorhus/is-blob) - Check if a value is a Blob - File-like object of immutable, raw data
  201. - [has-emoji](https://github.com/sindresorhus/has-emoji) - Check whether a string has any emoji
  202. ## Created by
  203. - [Sindre Sorhus](https://github.com/sindresorhus)
  204. - [Giora Guttsait](https://github.com/gioragutt)
  205. - [Brandon Smith](https://github.com/brandon93s)
  206. ## License
  207. MIT