Add is.empty() (#16)
This commit is contained in:
parent
ee8f5d16f8
commit
46e886d10b
3 changed files with 40 additions and 0 deletions
6
index.js
6
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;
|
||||
|
|
|
|||
|
|
@ -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?
|
||||
|
|
|
|||
29
test.js
29
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));
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue