Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add isNonEmptyString #40

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@
"ts-node": "^9.1.1",
"typescript": "^5.3.3"
},
"packageManager": "yarn@4.1.0"
"packageManager": "yarn@4.1.1"
}
18 changes: 17 additions & 1 deletion src/typeGuards.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import { assertFalse, assertTrue } from './predicates.test';
import { isArrayOfStrings, isEmptyObject, isNotNullish } from './typeGuards';
import {
isArrayOfStrings,
isEmptyObject,
isNonEmptyString,
isNotNullish,
isString,
} from './typeGuards';

describe('typeGuards', () => {
describe('isString', () => {
assertTrue(isString, ['foo', '1', 'true', '']);
assertFalse(isString, [/foo/, 1, true, null, undefined]);
});

describe('isNonEmptyString', () => {
assertTrue(isNonEmptyString, ['foo', '1', 'true']);
assertFalse(isNonEmptyString, ['', /foo/, 1, true, null, undefined]);
});

describe('isNotNullish', () => {
assertTrue(isNotNullish, ['', [], {}, false]);
assertFalse(isNotNullish, [null, undefined]);
Expand Down
9 changes: 5 additions & 4 deletions src/typeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ export function isString(value: unknown): value is string {
return typeof value === 'string';
}

export function isNonEmptyString(value: unknown): value is Exclude<string, ''> {
return isString(value) && value !== '';
}

export function isNotNullish<T>(value: T): value is NonNullable<T> {
return value != null;
}

export function isArrayOfStrings(value: unknown): value is Array<string> {
return (
value instanceof Array &&
value.every((record) => typeof record === 'string')
);
return value instanceof Array && value.every(isString);
}

export function isEmptyObject(value: unknown): value is Record<string, never> {
Expand Down
Loading