diff --git a/README.md b/README.md index ba96ed086..08ea3212b 100644 --- a/README.md +++ b/README.md @@ -340,6 +340,7 @@ average(1, 2, 3); * [`is`](#is) * [`isArrayLike`](#isarraylike) * [`isBoolean`](#isboolean) +* [`isEmpty`](#isempty) * [`isFunction`](#isfunction) * [`isNil`](#isnil) * [`isNull`](#isnull) @@ -5629,6 +5630,37 @@ isBoolean(false); // true
[⬆ Back to top](#table-of-contents) +### isEmpty + +Returns true if the a value is an empty object, collection, map or set, has no enumerable properties or is any type that is not considered a collection. + +Check if the provided value is `null` or if its `length` is equal to `0`. + +```js +const isEmpty = val => val == null || !(Object.keys(val) || val).length; +``` + +
+Examples + +```js +isEmpty(new Map()); // true +isEmpty(new Set()); // true +isEmpty([]); // true +isEmpty({}); // true +isEmpty(''); // true +isEmpty([1, 2]); // false +isEmpty({ a: 1, b: 2 }); // false +isEmpty('text'); // false +isEmpty(123); // true - type is not considered a collection +isEmpty(true); // true - type is not considered a collection +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### isFunction Checks if the given argument is a function. diff --git a/docs/index.html b/docs/index.html index 35079591d..aaa8b4fb8 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 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);
+      }

logo 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 ]
@@ -1300,6 +1300,17 @@ Foo.prototype📋 Copy to clipboard

isBoolean

Checks if the given argument is a native boolean element.

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

const isBoolean = val => typeof val === 'boolean';
 
isBoolean(null); // false
 isBoolean(false); // true
+

isEmpty

Returns true if the a value is an empty object, collection, map or set, has no enumerable properties or is any type that is not considered a collection.

Check if the provided value is null or if its length is equal to 0.

const isEmpty = val => val == null || !(Object.keys(val) || val).length;
+
isEmpty(new Map()); // true
+isEmpty(new Set()); // true
+isEmpty([]); // true
+isEmpty({}); // true
+isEmpty(''); // true
+isEmpty([1, 2]); // false
+isEmpty({ a: 1, b: 2 }); // false
+isEmpty('text'); // false
+isEmpty(123); // true - type is not considered a collection
+isEmpty(true); // true - type is not considered a collection
 

isFunction

Checks if the given argument is a function.

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

const isFunction = val => typeof val === 'function';
 
isFunction('x'); // false
 isFunction(x => x); // true