From 46e886d10b3f4fdc4746c58f13e3c68285718651 Mon Sep 17 00:00:00 2001 From: Kodie Grantham Date: Fri, 6 Oct 2017 02:39:36 -0500 Subject: [PATCH 1/2] Add is.empty() (#16) --- index.js | 6 ++++++ readme.md | 5 +++++ test.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/index.js b/index.js index f7a16fd..37f27f6 100644 --- a/index.js +++ b/index.js @@ -163,4 +163,10 @@ is.inRange = (x, range) => { is.infinite = x => x === Infinity || x === -Infinity; +const isEmptyStringOrArray = x => (is.string(x) || is.array(x)) && x.length === 0; +const isEmptyObject = x => !is.map(x) && !is.set(x) && is.object(x) && Object.keys(x).length === 0; +const isEmptyMapOrSet = x => (is.map(x) || is.set(x)) && x.size === 0; + +is.empty = x => !x || isEmptyStringOrArray(x) || isEmptyObject(x) || isEmptyMapOrSet(x); + module.exports = is; diff --git a/readme.md b/readme.md index fab01c1..8c9a29e 100644 --- a/readme.md +++ b/readme.md @@ -150,6 +150,11 @@ is.inRange(3, 10); Check if `value` is `Infinity` or `-Infinity`. +##### .empty(value) + +Returns `true` if `value` is falsy or an empty string, array, object, map, or set. + + ## FAQ ### Why yet another type checking module? diff --git a/test.js b/test.js index a2c4337..336bc45 100644 --- a/test.js +++ b/test.js @@ -373,3 +373,32 @@ test('is.inRange', t => { test('is.infinite', t => { testType(t, 'infinite', ['number']); }); + +test('is.empty', t => { + t.true(m.empty(null)); + t.true(m.empty(undefined)); + + t.true(m.empty(false)); + t.false(m.empty(true)); + + t.true(m.empty('')); + t.false(m.empty('🦄')); + + t.true(m.empty([])); + t.false(m.empty(['🦄'])); + + t.true(m.empty({})); + t.false(m.empty({unicorn: '🦄'})); + + const tempMap = new Map(); + t.true(m.empty(tempMap)); + + tempMap.set('unicorn', '🦄'); + t.false(m.empty(tempMap)); + + const tempSet = new Set(); + t.true(m.empty(tempSet)); + + tempSet.add(1); + t.false(m.empty(tempSet)); +}); From 6268253ec6e6722e2bf4d55e05834b8e3d1e5e12 Mon Sep 17 00:00:00 2001 From: Melvin Date: Fri, 6 Oct 2017 00:48:04 -0700 Subject: [PATCH 2/2] Add link to is-blob (#17) --- readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/readme.md b/readme.md index 8c9a29e..d3dc021 100644 --- a/readme.md +++ b/readme.md @@ -182,6 +182,7 @@ The most common mistakes I noticed in these modules was using `instanceof` for t - [is-array-sorted](https://github.com/sindresorhus/is-array-sorted) - Check if an Array is sorted - [is-error-constructor](https://github.com/sindresorhus/is-error-constructor) - Check if a value is an error constructor - [is-empty-iterable](https://github.com/sindresorhus/is-empty-iterable) - Check if an Iterable is empty +- [is-blob](https://github.com/sindresorhus/is-blob) - Check if a value is a Blob - File-like object of immutable, raw data ## Created by