Fix type guard for isWhitespaceString and isEmptyStringOrWhitespace (#207)
This commit is contained in:
parent
8cbcaee674
commit
25a376875d
3 changed files with 14 additions and 4 deletions
|
|
@ -10,6 +10,7 @@ import type {
|
||||||
Primitive,
|
Primitive,
|
||||||
TypedArray,
|
TypedArray,
|
||||||
WeakRef,
|
WeakRef,
|
||||||
|
Whitespace,
|
||||||
} from './types.js';
|
} from './types.js';
|
||||||
|
|
||||||
const typedArrayTypeNames = [
|
const typedArrayTypeNames = [
|
||||||
|
|
@ -435,7 +436,7 @@ export function isEmptyString(value: unknown): value is '' {
|
||||||
return isString(value) && value.length === 0;
|
return isString(value) && value.length === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isEmptyStringOrWhitespace(value: unknown): value is string {
|
export function isEmptyStringOrWhitespace(value: unknown): value is '' | Whitespace {
|
||||||
return isEmptyString(value) || isWhitespaceString(value);
|
return isEmptyString(value) || isWhitespaceString(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -777,7 +778,7 @@ export function isWeakSet(value: unknown): value is WeakSet<object> {
|
||||||
return getObjectType(value) === 'WeakSet';
|
return getObjectType(value) === 'WeakSet';
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isWhitespaceString(value: unknown): value is string {
|
export function isWhitespaceString(value: unknown): value is Whitespace {
|
||||||
return isString(value) && /^\s+$/.test(value);
|
return isString(value) && /^\s+$/.test(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -893,7 +894,7 @@ type Assert = {
|
||||||
emptyArray: (value: unknown, message?: string) => asserts value is never[];
|
emptyArray: (value: unknown, message?: string) => asserts value is never[];
|
||||||
nonEmptyArray: <T = unknown, Item = unknown>(value: T | Item[], message?: string) => asserts value is [Item, ...Item[]];
|
nonEmptyArray: <T = unknown, Item = unknown>(value: T | Item[], message?: string) => asserts value is [Item, ...Item[]];
|
||||||
emptyString: (value: unknown, message?: string) => asserts value is '';
|
emptyString: (value: unknown, message?: string) => asserts value is '';
|
||||||
emptyStringOrWhitespace: (value: unknown, message?: string) => asserts value is string;
|
emptyStringOrWhitespace: (value: unknown, message?: string) => asserts value is '' | Whitespace;
|
||||||
nonEmptyString: (value: unknown, message?: string) => asserts value is string;
|
nonEmptyString: (value: unknown, message?: string) => asserts value is string;
|
||||||
nonEmptyStringAndNotWhitespace: (value: unknown, message?: string) => asserts value is string;
|
nonEmptyStringAndNotWhitespace: (value: unknown, message?: string) => asserts value is string;
|
||||||
emptyObject: <Key extends keyof any = string>(value: unknown, message?: string) => asserts value is Record<Key, never>;
|
emptyObject: <Key extends keyof any = string>(value: unknown, message?: string) => asserts value is Record<Key, never>;
|
||||||
|
|
@ -1277,7 +1278,7 @@ export function assertEmptyString(value: unknown, message?: string): asserts val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function assertEmptyStringOrWhitespace(value: unknown, message?: string): asserts value is string {
|
export function assertEmptyStringOrWhitespace(value: unknown, message?: string): asserts value is '' | Whitespace {
|
||||||
if (!isEmptyStringOrWhitespace(value)) {
|
if (!isEmptyStringOrWhitespace(value)) {
|
||||||
throw new TypeError(message ?? typeErrorMessage('empty string or whitespace', value));
|
throw new TypeError(message ?? typeErrorMessage('empty string or whitespace', value));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -73,3 +73,5 @@ export type NodeStream = {
|
||||||
export type Predicate = (value: unknown) => boolean;
|
export type Predicate = (value: unknown) => boolean;
|
||||||
|
|
||||||
export type NonEmptyString = string & {0: string};
|
export type NonEmptyString = string & {0: string};
|
||||||
|
|
||||||
|
export type Whitespace = ' ';
|
||||||
|
|
|
||||||
|
|
@ -1793,6 +1793,13 @@ test('is.emptyStringOrWhitespace', t => {
|
||||||
t.throws(() => {
|
t.throws(() => {
|
||||||
assert.emptyStringOrWhitespace('unicorn');
|
assert.emptyStringOrWhitespace('unicorn');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let value = 'test'; // eslint-disable-line prefer-const -- can't use `const` here because then it will be inferred as `never` in the `if` block
|
||||||
|
if (is.emptyStringOrWhitespace(value)) {
|
||||||
|
value.charAt(0); // Should be inferred as `'' | Whitespace` and not `never`
|
||||||
|
} else {
|
||||||
|
value.charAt(0); // Should be inferred as `string` and not `never`
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is.nonEmptyString', t => {
|
test('is.nonEmptyString', t => {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue