From 321b662ec751d0008bb519f39bac0d02b5bb451c Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 23 Jan 2018 19:25:17 +0200 Subject: [PATCH] Add isEmpty --- snippets/isEmpty.md | 22 ++++++++++++++++++++++ tag_database | 1 + 2 files changed, 23 insertions(+) create mode 100644 snippets/isEmpty.md diff --git a/snippets/isEmpty.md b/snippets/isEmpty.md new file mode 100644 index 000000000..f620f3727 --- /dev/null +++ b/snippets/isEmpty.md @@ -0,0 +1,22 @@ +### 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; +``` + +```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 +``` diff --git a/tag_database b/tag_database index 85c99fe29..df181e65a 100644 --- a/tag_database +++ b/tag_database @@ -93,6 +93,7 @@ isAbsoluteURL:string,utility,browser,url isArrayLike:type,array isBoolean:type isDivisible:math +isEmpty:type,array,object,string isEven:math isFunction:type,function isLowerCase:string,utility