Skip to content

Commit

Permalink
feat: add isNonEmptyString
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Apr 22, 2024
1 parent 0cf7cde commit 406c08f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
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": "[email protected].0"
"packageManager": "[email protected].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

0 comments on commit 406c08f

Please sign in to comment.