From 2070fd3be9437f4b26b820e20c0039e42de452b1 Mon Sep 17 00:00:00 2001 From: Travis CI Date: Sun, 31 Dec 2017 17:34:18 +0000 Subject: [PATCH] Travis build: 748 [ci skip] --- README.md | 32 ++++++++++++++++++++++++++++++++ docs/index.html | 11 ++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5092802d3..91e2025e8 100644 --- a/README.md +++ b/README.md @@ -250,6 +250,7 @@ * [`isFunction`](#isfunction) * [`isNull`](#isnull) * [`isNumber`](#isnumber) +* [`isPrimitive`](#isprimitive) * [`isPromiseLike`](#ispromiselike) * [`isString`](#isstring) * [`isSymbol`](#issymbol) @@ -4204,6 +4205,37 @@ isNumber(1); // true
[⬆ Back to top](#table-of-contents) +### 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; +``` + +
+Examples + +```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 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### isPromiseLike Returns `true` if an object looks like a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), `false` otherwise. diff --git a/docs/index.html b/docs/index.html index f87468ab4..1e956217b 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+    }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -874,6 +874,15 @@ isNull('null'); // false
 

isNumber

Checks if the given argument is a number.

Use typeof to check if a value is classified as a number primitive.

const isNumber = val => typeof val === 'number';
 
isNumber('1'); // false
 isNumber(1); // true
+

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.

const isPrimitive = val => !['object', 'function'].includes(typeof val) || val === null;
+
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
 

isPromiseLike

Returns true if an object looks like a Promise, false otherwise.

Check if the object is not null, its typeof matches either object or function and if it has a .then property, which is also a function.

const isPromiseLike = obj =>
   obj !== null &&
   (typeof obj === 'object' || typeof obj === 'function') &&