Cleaned up code for is.empty() function
This commit is contained in:
parent
af2ad0c819
commit
788891cd81
3 changed files with 12 additions and 8 deletions
12
index.js
12
index.js
|
|
@ -163,12 +163,10 @@ 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)
|
||||
);
|
||||
};
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -152,7 +152,7 @@ Check if `value` is `Infinity` or `-Infinity`.
|
|||
|
||||
##### .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
|
||||
|
|
|
|||
6
test.js
6
test.js
|
|
@ -375,6 +375,12 @@ test('is.infinite', 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.false(m.empty('🦄'));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue