Cleaned up code for is.empty() function

This commit is contained in:
Kodie Grantham 2017-10-05 11:55:49 -05:00
parent af2ad0c819
commit 788891cd81
3 changed files with 12 additions and 8 deletions

View file

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

View file

@ -152,7 +152,7 @@ Check if `value` is `Infinity` or `-Infinity`.
##### .empty(value) ##### .empty(value)
Returns `true` if `value` is an empty string, array, object, map, or set. Returns `true` if `value` is falsy or an empty string, array, object, map, or set.
## FAQ ## FAQ

View file

@ -375,6 +375,12 @@ test('is.infinite', t => {
}); });
test('is.empty', t => { 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.true(m.empty(''));
t.false(m.empty('🦄')); t.false(m.empty('🦄'));