Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. When using `is` together with TypeScript, [type guards](http://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types) are being used to infer the correct type inside if-else statements.
  19. ```ts
  20. import is from '@sindresorhus/is';
  21. const padLeft = (value: string, padding: string | number) => {
  22. if (is.number(padding)) {
  23. // `padding` is typed as `number`
  24. return Array(padding + 1).join(' ') + value;
  25. }
  26. if (is.string(padding)) {
  27. // `padding` is typed as `string`
  28. return padding + value;
  29. }
  30. throw new TypeError(`Expected 'padding' to be of type 'string' or 'number', got '${is(padding)}'.`);
  31. }
  32. padLeft('🦄', 3);
  33. //=> ' 🦄'
  34. padLeft('🦄', '🌈');
  35. //=> '🌈🦄'
  36. ```
  37. ## API
  38. ### is(value)
  39. Returns the type of `value`.
  40. Primitives are lowercase and object types are camelcase.
  41. Example:
  42. - `'undefined'`
  43. - `'null'`
  44. - `'string'`
  45. - `'symbol'`
  46. - `'Array'`
  47. - `'Function'`
  48. - `'Object'`
  49. Note: It will throw an error if you try to feed it object-wrapped primitives, as that's a bad practice. For example `new String('foo')`.
  50. ### is.{method}
  51. All the below methods accept a value and returns a boolean for whether the value is of the desired type.
  52. #### Primitives
  53. ##### .undefined(value)
  54. ##### .null(value)
  55. ##### .string(value)
  56. ##### .number(value)
  57. ##### .boolean(value)
  58. ##### .symbol(value)
  59. #### Built-in types
  60. ##### .array(value)
  61. ##### .function(value)
  62. ##### .buffer(value)
  63. ##### .object(value)
  64. Keep in mind that [functions are objects too](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions).
  65. ##### .numericString(value)
  66. Returns `true` for a string that represents a number. For example, `'42'` and `'-8'`.
  67. Note: `'NaN'` returns `false`, but `'Infinity'` and `'-Infinity'` return `true`.
  68. ##### .regExp(value)
  69. ##### .date(value)
  70. ##### .error(value)
  71. ##### .nativePromise(value)
  72. ##### .promise(value)
  73. 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.
  74. ##### .generator(value)
  75. Returns `true` for any object that implements its own `.next()` and `.throw()` methods and has a function definition for `Symbol.iterator`.
  76. ##### .generatorFunction(value)
  77. ##### .asyncFunction(value)
  78. Returns `true` for any `async` function that can be called with the `await` operator.
  79. ```js
  80. is.asyncFunction(async () => {});
  81. // => true
  82. is.asyncFunction(() => {});
  83. // => false
  84. ```
  85. ##### .boundFunction(value)
  86. Returns `true` for any `bound` function.
  87. ```js
  88. is.boundFunction(() => {});
  89. // => true
  90. is.boundFunction(function () {}.bind(null));
  91. // => true
  92. is.boundFunction(function () {});
  93. // => false
  94. ```
  95. ##### .map(value)
  96. ##### .set(value)
  97. ##### .weakMap(value)
  98. ##### .weakSet(value)
  99. #### Typed arrays
  100. ##### .int8Array(value)
  101. ##### .uint8Array(value)
  102. ##### .uint8ClampedArray(value)
  103. ##### .int16Array(value)
  104. ##### .uint16Array(value)
  105. ##### .int32Array(value)
  106. ##### .uint32Array(value)
  107. ##### .float32Array(value)
  108. ##### .float64Array(value)
  109. #### Structured data
  110. ##### .arrayBuffer(value)
  111. ##### .sharedArrayBuffer(value)
  112. ##### .dataView(value)
  113. #### Emptiness
  114. ##### .emptyString(value)
  115. Returns `true` if the value is a `string` and the `.length` is 0.
  116. ##### .nonEmptyString(value)
  117. Returns `true` if the value is a `string` and the `.length` is more than 0.
  118. ##### .emptyStringOrWhitespace(value)
  119. Returns `true` if `is.emptyString(value)` or if it's a `string` that is all whitespace.
  120. ##### .emptyArray(value)
  121. Returns `true` if the value is an `Array` and the `.length` is 0.
  122. ##### .nonEmptyArray(value)
  123. Returns `true` if the value is an `Array` and the `.length` is more than 0.
  124. ##### .emptyObject(value)
  125. Returns `true` if the value is an `Object` and `Object.keys(value).length` is 0.
  126. Please note that `Object.keys` returns only own enumerable properties. Hence something like this can happen:
  127. ```js
  128. const object1 = {};
  129. Object.defineProperty(object1, 'property1', {
  130. value: 42,
  131. writable: true,
  132. enumerable: false,
  133. configurable: true
  134. });
  135. is.emptyObject(object1);
  136. // => true
  137. ```
  138. ##### .nonEmptyObject(value)
  139. Returns `true` if the value is an `Object` and `Object.keys(value).length` is more than 0.
  140. ##### .emptySet(value)
  141. Returns `true` if the value is a `Set` and the `.size` is 0.
  142. ##### .nonEmptySet(Value)
  143. Returns `true` if the value is a `Set` and the `.size` is more than 0.
  144. ##### .emptyMap(value)
  145. Returns `true` if the value is a `Map` and the `.size` is 0.
  146. ##### .nonEmptyMap(value)
  147. Returns `true` if the value is a `Map` and the `.size` is more than 0.
  148. #### Miscellaneous
  149. ##### .directInstanceOf(value, class)
  150. Returns `true` if `value` is a direct instance of `class`.
  151. ```js
  152. is.directInstanceOf(new Error(), Error);
  153. //=> true
  154. class UnicornError extends Error {}
  155. is.directInstanceOf(new UnicornError(), Error);
  156. //=> false
  157. ```
  158. ##### .urlInstance(value)
  159. Returns `true` if `value` is an instance of the [`URL` class](https://developer.mozilla.org/en-US/docs/Web/API/URL).
  160. ```js
  161. const url = new URL('https://example.com');
  162. is.urlInstance(url);
  163. //=> true
  164. ```
  165. ### .url(value)
  166. Returns `true` if `value` is a URL string.
  167. Note: this only does basic checking using the [`URL` class](https://developer.mozilla.org/en-US/docs/Web/API/URL) constructor.
  168. ```js
  169. const url = 'https://example.com';
  170. is.url(url);
  171. //=> true
  172. is.url(new URL(url));
  173. //=> false
  174. ```
  175. ##### .truthy(value)
  176. Returns `true` for all values that evaluate to true in a boolean context:
  177. ```js
  178. is.truthy('🦄');
  179. //=> true
  180. is.truthy(undefined);
  181. //=> false
  182. ```
  183. ##### .falsy(value)
  184. Returns `true` if `value` is one of: `false`, `0`, `''`, `null`, `undefined`, `NaN`.
  185. ##### .nan(value)
  186. ##### .nullOrUndefined(value)
  187. ##### .primitive(value)
  188. JavaScript primitives are as follows: `null`, `undefined`, `string`, `number`, `boolean`, `symbol`.
  189. ##### .integer(value)
  190. ##### .safeInteger(value)
  191. Returns `true` if `value` is a [safe integer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger).
  192. ##### .plainObject(value)
  193. An object is plain if it's created by either `{}`, `new Object()`, or `Object.create(null)`.
  194. ##### .iterable(value)
  195. ##### .asyncIterable(value)
  196. ##### .class(value)
  197. Returns `true` for instances created by a class.
  198. ##### .typedArray(value)
  199. ##### .arrayLike(value)
  200. 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.
  201. ```js
  202. is.arrayLike(document.forms);
  203. //=> true
  204. function foo() {
  205. is.arrayLike(arguments);
  206. //=> true
  207. }
  208. foo();
  209. ```
  210. ##### .inRange(value, range)
  211. 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.
  212. ```js
  213. is.inRange(3, [0, 5]);
  214. is.inRange(3, [5, 0]);
  215. is.inRange(0, [-2, 2]);
  216. ```
  217. ##### .inRange(value, upperBound)
  218. Check if `value` (number) is in the range of `0` to `upperBound`.
  219. ```js
  220. is.inRange(3, 10);
  221. ```
  222. ##### .domElement(value)
  223. Returns `true` if `value` is a DOM Element.
  224. ##### .nodeStream(value)
  225. Returns `true` if `value` is a Node.js [stream](https://nodejs.org/api/stream.html).
  226. ```js
  227. const fs = require('fs');
  228. is.nodeStream(fs.createReadStream('unicorn.png'));
  229. //=> true
  230. ```
  231. ##### .observable(value)
  232. Returns `true` if `value` is an `Observable`.
  233. ```js
  234. const {Observable} = require('rxjs');
  235. is.observable(new Observable());
  236. //=> true
  237. ```
  238. ##### .infinite(value)
  239. Check if `value` is `Infinity` or `-Infinity`.
  240. ##### .even(value)
  241. Returns `true` if `value` is an even integer.
  242. ##### .odd(value)
  243. Returns `true` if `value` is an odd integer.
  244. ##### .any(predicate, ...values)
  245. Returns `true` if **any** of the input `values` returns true in the `predicate`:
  246. ```js
  247. is.any(is.string, {}, true, '🦄');
  248. //=> true
  249. is.any(is.boolean, 'unicorns', [], new Map());
  250. //=> false
  251. ```
  252. ##### .all(predicate, ...values)
  253. Returns `true` if **all** of the input `values` returns true in the `predicate`:
  254. ```js
  255. is.all(is.object, {}, new Map(), new Set());
  256. //=> true
  257. is.all(is.string, '🦄', [], 'unicorns');
  258. //=> false
  259. ```
  260. ## FAQ
  261. ### Why yet another type checking module?
  262. There are hundreds of type checking modules on npm, unfortunately, I couldn't find any that fit my needs:
  263. - Includes both type methods and ability to get the type
  264. - Types of primitives returned as lowercase and object types as camelcase
  265. - Covers all built-ins
  266. - Unsurprising behavior
  267. - Well-maintained
  268. - Comprehensive test suite
  269. For the ones I found, pick 3 of these.
  270. 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.
  271. ## Related
  272. - [ow](https://github.com/sindresorhus/ow) - Function argument validation for humans
  273. - [is-stream](https://github.com/sindresorhus/is-stream) - Check if something is a Node.js stream
  274. - [is-observable](https://github.com/sindresorhus/is-observable) - Check if a value is an Observable
  275. - [file-type](https://github.com/sindresorhus/file-type) - Detect the file type of a Buffer/Uint8Array
  276. - [is-ip](https://github.com/sindresorhus/is-ip) - Check if a string is an IP address
  277. - [is-array-sorted](https://github.com/sindresorhus/is-array-sorted) - Check if an Array is sorted
  278. - [is-error-constructor](https://github.com/sindresorhus/is-error-constructor) - Check if a value is an error constructor
  279. - [is-empty-iterable](https://github.com/sindresorhus/is-empty-iterable) - Check if an Iterable is empty
  280. - [is-blob](https://github.com/sindresorhus/is-blob) - Check if a value is a Blob - File-like object of immutable, raw data
  281. - [has-emoji](https://github.com/sindresorhus/has-emoji) - Check whether a string has any emoji
  282. ## Created by
  283. - [Sindre Sorhus](https://github.com/sindresorhus)
  284. - [Giora Guttsait](https://github.com/gioragutt)
  285. - [Brandon Smith](https://github.com/brandon93s)
  286. ## License
  287. MIT