Add is.empty()

This commit is contained in:
Kodie Grantham 2017-10-05 10:33:17 -05:00
parent ee8f5d16f8
commit af2ad0c819
3 changed files with 36 additions and 0 deletions

View file

@ -163,4 +163,12 @@ is.inRange = (x, range) => {
is.infinite = x => x === Infinity || x === -Infinity;
is.empty = value => {
return (
((is.string(value) || is.array(value)) && !value.length) ||
(!is.map(value) && !is.set(value) && is.object(value) && !Object.keys(value).length) ||
((is.map(value) || is.set(value)) && !value.size)
);
};
module.exports = is;

View file

@ -150,6 +150,11 @@ is.inRange(3, 10);
Check if `value` is `Infinity` or `-Infinity`.
##### .empty(value)
Returns `true` if `value` is an empty string, array, object, map, or set.
## FAQ
### Why yet another type checking module?

23
test.js
View file

@ -373,3 +373,26 @@ test('is.inRange', t => {
test('is.infinite', t => {
testType(t, 'infinite', ['number']);
});
test('is.empty', t => {
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));
});