Add isPrimitive utility snippet
This commit is contained in:
22
snippets/isPrimitive.md
Normal file
22
snippets/isPrimitive.md
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
### isPrimitive
|
||||||
|
|
||||||
|
Returns a boolean determining if the supplied value is primitive or not.
|
||||||
|
|
||||||
|
Use `Array.includes()` on an array of type strings, supplying the type using `typeof`.
|
||||||
|
Since `typeof null` evaluates to `'object'`, it needs to be directly compared.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const isPrimitive = value =>
|
||||||
|
['string', 'number', 'symbol', 'boolean', 'undefined'].includes(typeof value) || value === null;
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
isPrimitive(window.someNonExistentProperty); // true
|
||||||
|
isPrimitive(null); // true
|
||||||
|
isPrimitive(50); // true
|
||||||
|
isPrimitive('Hello!'); // true
|
||||||
|
isPrimitive(false); // true
|
||||||
|
isPrimitive(Symbol()); // true
|
||||||
|
isPrimitive([]); // false
|
||||||
|
isPrimitive(new String('Hello!')); // false
|
||||||
|
```
|
||||||
@ -71,6 +71,7 @@ isEven:math
|
|||||||
isFunction:utility
|
isFunction:utility
|
||||||
isNumber:utility
|
isNumber:utility
|
||||||
isPrime:math
|
isPrime:math
|
||||||
|
isPrimitive:utility
|
||||||
isString:utility
|
isString:utility
|
||||||
isSymbol:utility
|
isSymbol:utility
|
||||||
JSONToDate:date
|
JSONToDate:date
|
||||||
|
|||||||
Reference in New Issue
Block a user