Add .nonEmptyStringAndNotWhitespace() (#161)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
This commit is contained in:
parent
04ccf21dba
commit
23cf074a73
3 changed files with 38 additions and 0 deletions
11
readme.md
11
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.
|
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)
|
##### .emptyStringOrWhitespace(value)
|
||||||
|
|
||||||
Returns `true` if `is.emptyString(value)` or if it's a `string` that is all whitespace.
|
Returns `true` if `is.emptyString(value)` or if it's a `string` that is all whitespace.
|
||||||
|
|
|
||||||
|
|
@ -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);
|
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);
|
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 = <Key extends keyof any = string>(value: unknown): value is Record<Key, never> => is.object(value) && !is.map(value) && !is.set(value) && Object.keys(value).length === 0;
|
is.emptyObject = <Key extends keyof any = string>(value: unknown): value is Record<Key, never> => 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:
|
// TODO: Use `not` operator here to remove `Map` and `Set` from type guard:
|
||||||
|
|
@ -433,6 +436,7 @@ export const enum AssertionTypeDescription {
|
||||||
emptyString = 'empty string',
|
emptyString = 'empty string',
|
||||||
nonEmptyString = 'non-empty string',
|
nonEmptyString = 'non-empty string',
|
||||||
emptyStringOrWhitespace = 'empty string or whitespace',
|
emptyStringOrWhitespace = 'empty string or whitespace',
|
||||||
|
nonEmptyStringAndNotWhitespace = 'non-empty string and not whitespace',
|
||||||
emptyObject = 'empty object',
|
emptyObject = 'empty object',
|
||||||
nonEmptyObject = 'non-empty object',
|
nonEmptyObject = 'non-empty object',
|
||||||
emptySet = 'empty set',
|
emptySet = 'empty set',
|
||||||
|
|
@ -522,6 +526,7 @@ interface Assert {
|
||||||
emptyString: (value: unknown) => asserts value is '';
|
emptyString: (value: unknown) => asserts value is '';
|
||||||
nonEmptyString: (value: unknown) => asserts value is string;
|
nonEmptyString: (value: unknown) => asserts value is string;
|
||||||
emptyStringOrWhitespace: (value: unknown) => asserts value is string;
|
emptyStringOrWhitespace: (value: unknown) => asserts value is string;
|
||||||
|
nonEmptyStringAndNotWhitespace: (value: unknown) => asserts value is string;
|
||||||
emptyObject: <Key extends keyof any = string>(value: unknown) => asserts value is Record<Key, never>;
|
emptyObject: <Key extends keyof any = string>(value: unknown) => asserts value is Record<Key, never>;
|
||||||
nonEmptyObject: <Key extends keyof any = string, Value = unknown>(value: unknown) => asserts value is Record<Key, Value>;
|
nonEmptyObject: <Key extends keyof any = string, Value = unknown>(value: unknown) => asserts value is Record<Key, Value>;
|
||||||
emptySet: (value: unknown) => asserts value is Set<never>;
|
emptySet: (value: unknown) => asserts value is Set<never>;
|
||||||
|
|
@ -623,6 +628,7 @@ export const assert: Assert = {
|
||||||
emptyString: (value: unknown): asserts value is '' => assertType(is.emptyString(value), AssertionTypeDescription.emptyString, value),
|
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),
|
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),
|
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: <Key extends keyof any = string>(value: unknown): asserts value is Record<Key, never> => assertType(is.emptyObject(value), AssertionTypeDescription.emptyObject, value),
|
emptyObject: <Key extends keyof any = string>(value: unknown): asserts value is Record<Key, never> => assertType(is.emptyObject(value), AssertionTypeDescription.emptyObject, value),
|
||||||
nonEmptyObject: <Key extends keyof any = string, Value = unknown>(value: unknown): asserts value is Record<Key, Value> => assertType(is.nonEmptyObject(value), AssertionTypeDescription.nonEmptyObject, value),
|
nonEmptyObject: <Key extends keyof any = string, Value = unknown>(value: unknown): asserts value is Record<Key, Value> => assertType(is.nonEmptyObject(value), AssertionTypeDescription.nonEmptyObject, value),
|
||||||
emptySet: (value: unknown): asserts value is Set<never> => assertType(is.emptySet(value), AssertionTypeDescription.emptySet, value),
|
emptySet: (value: unknown): asserts value is Set<never> => assertType(is.emptySet(value), AssertionTypeDescription.emptySet, value),
|
||||||
|
|
|
||||||
21
test/test.ts
21
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 => {
|
test('is.emptyObject', t => {
|
||||||
t.true(is.emptyObject({}));
|
t.true(is.emptyObject({}));
|
||||||
t.true(is.emptyObject(new Object())); // eslint-disable-line no-new-object
|
t.true(is.emptyObject(new Object())); // eslint-disable-line no-new-object
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue