From 23cf074a73a9c4bb0d70fe0c8df2e07bae1e21d9 Mon Sep 17 00:00:00 2001 From: Ivan Katliarchuk <5395690+ivankatliarchuk@users.noreply.github.com> Date: Fri, 25 Feb 2022 09:10:57 +0000 Subject: [PATCH] Add `.nonEmptyStringAndNotWhitespace()` (#161) Co-authored-by: Sindre Sorhus --- readme.md | 11 +++++++++++ source/index.ts | 6 ++++++ test/test.ts | 21 +++++++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/readme.md b/readme.md index 1450f83..e4e1924 100644 --- a/readme.md +++ b/readme.md @@ -245,6 +245,17 @@ Returns `true` if the value is a `string` and the `.length` is 0. Returns `true` if the value is a `string` and the `.length` is more than 0. +##### .nonEmptyStringAndNotWhitespace(value) + +Returns `true` if the value is a `string` that is not empty and not whitespace. + +```js +const values = ['property1', '', null, 'property2', ' ', undefined]; + +values.filter(is.nonEmptyStringAndNotWhitespace); +//=> ['property1', 'property2'] +``` + ##### .emptyStringOrWhitespace(value) Returns `true` if `is.emptyString(value)` or if it's a `string` that is all whitespace. diff --git a/source/index.ts b/source/index.ts index fcd66a0..6836b18 100644 --- a/source/index.ts +++ b/source/index.ts @@ -350,6 +350,9 @@ is.nonEmptyString = (value: unknown): value is string => is.string(value) && val const isWhiteSpaceString = (value: unknown): value is string => is.string(value) && !/\S/.test(value); is.emptyStringOrWhitespace = (value: unknown): value is string => is.emptyString(value) || isWhiteSpaceString(value); +// TODO: Use `not ''` when the `not` operator is available. +is.nonEmptyStringAndNotWhitespace = (value: unknown): value is string => is.string(value) && !is.emptyStringOrWhitespace(value); + is.emptyObject = (value: unknown): value is Record => is.object(value) && !is.map(value) && !is.set(value) && Object.keys(value).length === 0; // TODO: Use `not` operator here to remove `Map` and `Set` from type guard: @@ -433,6 +436,7 @@ export const enum AssertionTypeDescription { emptyString = 'empty string', nonEmptyString = 'non-empty string', emptyStringOrWhitespace = 'empty string or whitespace', + nonEmptyStringAndNotWhitespace = 'non-empty string and not whitespace', emptyObject = 'empty object', nonEmptyObject = 'non-empty object', emptySet = 'empty set', @@ -522,6 +526,7 @@ interface Assert { emptyString: (value: unknown) => asserts value is ''; nonEmptyString: (value: unknown) => asserts value is string; emptyStringOrWhitespace: (value: unknown) => asserts value is string; + nonEmptyStringAndNotWhitespace: (value: unknown) => asserts value is string; emptyObject: (value: unknown) => asserts value is Record; nonEmptyObject: (value: unknown) => asserts value is Record; emptySet: (value: unknown) => asserts value is Set; @@ -623,6 +628,7 @@ export const assert: Assert = { emptyString: (value: unknown): asserts value is '' => assertType(is.emptyString(value), AssertionTypeDescription.emptyString, value), nonEmptyString: (value: unknown): asserts value is string => assertType(is.nonEmptyString(value), AssertionTypeDescription.nonEmptyString, value), emptyStringOrWhitespace: (value: unknown): asserts value is string => assertType(is.emptyStringOrWhitespace(value), AssertionTypeDescription.emptyStringOrWhitespace, value), + nonEmptyStringAndNotWhitespace: (value: unknown): asserts value is string => assertType(is.nonEmptyStringAndNotWhitespace(value), AssertionTypeDescription.nonEmptyStringAndNotWhitespace, value), emptyObject: (value: unknown): asserts value is Record => assertType(is.emptyObject(value), AssertionTypeDescription.emptyObject, value), nonEmptyObject: (value: unknown): asserts value is Record => assertType(is.nonEmptyObject(value), AssertionTypeDescription.nonEmptyObject, value), emptySet: (value: unknown): asserts value is Set => assertType(is.emptySet(value), AssertionTypeDescription.emptySet, value), diff --git a/test/test.ts b/test/test.ts index 734fd27..7fb01f8 100644 --- a/test/test.ts +++ b/test/test.ts @@ -1436,6 +1436,27 @@ test('is.emptyStringOrWhitespace', t => { }); }); +test('is.nonEmptyStringAndNotWhitespace', t => { + t.false(is.nonEmptyStringAndNotWhitespace(' ')); + t.true(is.nonEmptyStringAndNotWhitespace('🦄')); + + for (const value of [null, undefined, 5, NaN, {}, []]) { + t.false(is.nonEmptyStringAndNotWhitespace(value)); + + t.throws(() => { + assert.nonEmptyStringAndNotWhitespace(value); + }); + } + + t.throws(() => { + assert.nonEmptyStringAndNotWhitespace(''); + }); + + t.notThrows(() => { + assert.nonEmptyStringAndNotWhitespace('🦄'); + }); +}); + test('is.emptyObject', t => { t.true(is.emptyObject({})); t.true(is.emptyObject(new Object())); // eslint-disable-line no-new-object