Skip to content

Commit

Permalink
Change type guard for whitespace from string to ' '
Browse files Browse the repository at this point in the history
  • Loading branch information
marlun78 committed May 22, 2024
1 parent 47f4974 commit 5301915
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
9 changes: 5 additions & 4 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
Primitive,
TypedArray,
WeakRef,
Whitespace,
} from './types.js';

const typedArrayTypeNames = [
Expand Down Expand Up @@ -443,7 +444,7 @@ export function isEmptyString(value: unknown): value is '' {
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);
}

Expand Down Expand Up @@ -785,7 +786,7 @@ export function isWeakSet(value: unknown): value is WeakSet<object> {
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);
}

Expand Down Expand Up @@ -911,7 +912,7 @@ type Assert = {
emptyArray: (value: unknown, message?: string) => asserts value is never[];
nonEmptyArray: <T = unknown, Item = unknown>(value: T | Item[], message?: string) => asserts value is [Item, ...Item[]];
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;
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>;
Expand Down Expand Up @@ -1301,7 +1302,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)) {
throw new TypeError(message ?? typeErrorMessage('empty string or whitespace', value));
}
Expand Down
2 changes: 2 additions & 0 deletions source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,5 @@ export type NodeStream = {
export type Predicate = (value: unknown) => boolean;

export type NonEmptyString = string & {0: string};

export type Whitespace = ' ';
7 changes: 7 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,13 @@ test('is.emptyStringOrWhitespace', t => {
t.throws(() => {
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 => {
Expand Down

0 comments on commit 5301915

Please sign in to comment.