Add is.truthy() and is.falsy() (#25)
This commit is contained in:
parent
cdd4829edf
commit
dfcdfc3a83
3 changed files with 37 additions and 1 deletions
5
index.js
5
index.js
|
|
@ -111,6 +111,9 @@ is.float64Array = isObjectOfType('Float64Array');
|
|||
is.arrayBuffer = isObjectOfType('ArrayBuffer');
|
||||
is.sharedArrayBuffer = isObjectOfType('SharedArrayBuffer');
|
||||
|
||||
is.truthy = x => !!x; // eslint-disable-line no-implicit-coercion
|
||||
is.falsy = x => !x;
|
||||
|
||||
is.nan = Number.isNaN;
|
||||
is.nullOrUndefined = x => is.null(x) || is.undefined(x);
|
||||
|
||||
|
|
@ -188,7 +191,7 @@ const isEmptyStringOrArray = x => (is.string(x) || is.array(x)) && x.length ===
|
|||
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);
|
||||
is.empty = x => is.falsy(x) || isEmptyStringOrArray(x) || isEmptyObject(x) || isEmptyMapOrSet(x);
|
||||
is.emptyOrWhitespace = x => is.empty(x) || isWhiteSpaceString(x);
|
||||
|
||||
const predicateOnArray = (method, predicate, values) => {
|
||||
|
|
|
|||
16
readme.md
16
readme.md
|
|
@ -121,6 +121,22 @@ is.asyncFunction(() => {});
|
|||
|
||||
#### Miscellaneous
|
||||
|
||||
##### .truthy(value)
|
||||
|
||||
Returns `true` for all values that evaluate to true in a boolean context:
|
||||
|
||||
```js
|
||||
is.truthy('🦄');
|
||||
//=> true
|
||||
|
||||
is.truthy(undefined);
|
||||
//=> false
|
||||
```
|
||||
|
||||
##### .falsy(value)
|
||||
|
||||
Returns `true` if `value` is one of: `false`, `0`, `''`, `null`, `undefined`, `NaN`.
|
||||
|
||||
##### .nan(value)
|
||||
##### .nullOrUndefined(value)
|
||||
##### .primitive(value)
|
||||
|
|
|
|||
17
test.js
17
test.js
|
|
@ -289,6 +289,23 @@ test('is.dataView', t => {
|
|||
testType(t, 'arrayBuffer');
|
||||
});
|
||||
|
||||
test('is.truthy', t => {
|
||||
t.true(m.truthy('unicorn'));
|
||||
t.true(m.truthy('🦄'));
|
||||
t.true(m.truthy(new Set()));
|
||||
t.true(m.truthy(Symbol('🦄')));
|
||||
t.true(m.truthy(true));
|
||||
});
|
||||
|
||||
test('is.falsy', t => {
|
||||
t.true(m.falsy(false));
|
||||
t.true(m.falsy(0));
|
||||
t.true(m.falsy(''));
|
||||
t.true(m.falsy(null));
|
||||
t.true(m.falsy(undefined));
|
||||
t.true(m.falsy(NaN));
|
||||
});
|
||||
|
||||
test('is.nan', t => {
|
||||
testType(t, 'nan');
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue