From cdd4829edfec08131dade41ab353b7f1d004f8ff Mon Sep 17 00:00:00 2001 From: Brandon Smith Date: Tue, 17 Oct 2017 16:46:58 -0400 Subject: [PATCH] Add is.emptyOrWhitespace() (#21) --- index.js | 2 ++ readme.md | 4 ++++ test.js | 7 +++++++ 3 files changed, 13 insertions(+) diff --git a/index.js b/index.js index d5abf64..f5d76ce 100644 --- a/index.js +++ b/index.js @@ -183,11 +183,13 @@ const isAbsoluteMod2 = value => x => is.integer(x) && Math.abs(x % 2) === value; is.even = isAbsoluteMod2(0); is.odd = isAbsoluteMod2(1); +const isWhiteSpaceString = x => is.string(x) && /\S/.test(x) === false; 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); +is.emptyOrWhitespace = x => is.empty(x) || isWhiteSpaceString(x); const predicateOnArray = (method, predicate, values) => { // `values` is the calling function's "arguments object". diff --git a/readme.md b/readme.md index 1760692..c1eae3f 100644 --- a/readme.md +++ b/readme.md @@ -182,6 +182,10 @@ Returns `true` if `value` is an odd integer. Returns `true` if `value` is falsy or an empty string, array, object, map, or set. +##### .emptyOrWhitespace(value) + +Returns `true` if `is.empty(value)` or a string that is all whitespace. + ##### .any(predicate, ...values) diff --git a/test.js b/test.js index 910f37a..31e6055 100644 --- a/test.js +++ b/test.js @@ -468,6 +468,13 @@ test('is.empty', t => { t.false(m.empty(tempSet)); }); +test('is.emptyOrWhitespace', t => { + t.true(m.emptyOrWhitespace('')); + t.true(m.emptyOrWhitespace(' ')); + t.false(m.emptyOrWhitespace('🦄')); + t.false(m.emptyOrWhitespace('unicorn')); +}); + test('is.any', t => { t.true(m.any(m.string, {}, true, '🦄')); t.true(m.any(m.object, false, {}, 'unicorns'));