diff --git a/index.js b/index.js index f5d76ce..5c5e656 100644 --- a/index.js +++ b/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) => { diff --git a/readme.md b/readme.md index c1eae3f..51b18bf 100644 --- a/readme.md +++ b/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) diff --git a/test.js b/test.js index 31e6055..f35eb08 100644 --- a/test.js +++ b/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'); });