Merge pull request #431 from atomiks/isPrimitive

[FEATURE] isPrimitive utility snippet
This commit is contained in:
Angelos Chalaris
2017-12-31 19:30:07 +02:00
committed by GitHub
2 changed files with 23 additions and 0 deletions

22
snippets/isPrimitive.md Normal file
View 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 which are not primitive,
supplying the type using `typeof`.
Since `typeof null` evaluates to `'object'`, it needs to be directly compared.
```js
const isPrimitive = val => !['object', 'function'].includes(typeof val) || val === 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
```

View File

@ -75,6 +75,7 @@ isFunction:utility
isNull:utility
isNumber:utility
isPrime:math
isPrimitive:utility
isPromiseLike:utility
isString:utility
isSymbol:utility