Skip to content

Commit bb44657

Browse files
authored
feat: add type guard isTruthy
1 parent fe0cb80 commit bb44657

File tree

2 files changed

+57
-21
lines changed

2 files changed

+57
-21
lines changed

src/typeGuards.test.ts

+36-10
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ import {
55
isNonEmptyString,
66
isNotNullish,
77
isString,
8+
isTruthy,
89
} from './typeGuards';
910

1011
describe('typeGuards', () => {
11-
describe('isString', () => {
12-
assertTrue(isString, ['foo', '1', 'true', '']);
13-
assertFalse(isString, [/foo/, 1, true, null, undefined]);
12+
describe('isArrayOfStrings', () => {
13+
expect(isArrayOfStrings(['foo', 'bar'])).toBe(true);
14+
expect(isArrayOfStrings([])).toBe(true);
15+
assertFalse(isArrayOfStrings, [null, undefined, ['', null]]);
16+
});
17+
18+
describe('isEmptyObject', () => {
19+
assertTrue(isEmptyObject, [{}]);
20+
assertFalse(isEmptyObject, [{ foo: 'bar' }, [], null, undefined, '']);
1421
});
1522

1623
describe('isNonEmptyString', () => {
@@ -23,14 +30,33 @@ describe('typeGuards', () => {
2330
assertFalse(isNotNullish, [null, undefined]);
2431
});
2532

26-
describe('isArrayOfStrings', () => {
27-
expect(isArrayOfStrings(['foo', 'bar'])).toBe(true);
28-
expect(isArrayOfStrings([])).toBe(true);
29-
assertFalse(isArrayOfStrings, [null, undefined, ['', null]]);
33+
describe('isString', () => {
34+
assertTrue(isString, ['foo', '1', 'true', '']);
35+
assertFalse(isString, [/foo/, 1, true, null, undefined]);
3036
});
3137

32-
describe('emptyObject', () => {
33-
assertTrue(isEmptyObject, [{}]);
34-
assertFalse(isEmptyObject, [{ foo: 'bar' }, [], null, undefined, '']);
38+
describe('isTruthy', () => {
39+
assertTrue(isTruthy, [
40+
true,
41+
1,
42+
-1,
43+
Infinity,
44+
' ',
45+
'0',
46+
'false',
47+
[],
48+
{},
49+
document,
50+
]);
51+
assertFalse(isTruthy, [
52+
null,
53+
undefined,
54+
false,
55+
0,
56+
-0,
57+
NaN,
58+
'',
59+
document.all,
60+
]);
3561
});
3662
});

src/typeGuards.ts

+21-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
export function isString(value: unknown): value is string {
2-
return typeof value === 'string';
1+
export function isArrayOfStrings(value: unknown): value is Array<string> {
2+
return value instanceof Array && value.every(isString);
3+
}
4+
5+
export function isEmptyObject(value: unknown): value is Record<string, never> {
6+
return Boolean(
7+
value &&
8+
typeof value === 'object' &&
9+
!(value instanceof Array) &&
10+
Object.keys(value).length === 0,
11+
);
312
}
413

514
export function isNonEmptyString(value: unknown): value is Exclude<string, ''> {
@@ -10,15 +19,16 @@ export function isNotNullish<T>(value: T): value is NonNullable<T> {
1019
return value != null;
1120
}
1221

13-
export function isArrayOfStrings(value: unknown): value is Array<string> {
14-
return value instanceof Array && value.every(isString);
22+
export function isString(value: unknown): value is string {
23+
return typeof value === 'string';
1524
}
1625

17-
export function isEmptyObject(value: unknown): value is Record<string, never> {
18-
return Boolean(
19-
value &&
20-
typeof value === 'object' &&
21-
!(value instanceof Array) &&
22-
Object.keys(value).length === 0,
23-
);
26+
/** https://developer.mozilla.org/en-US/docs/Glossary/Falsy */
27+
export function isTruthy<T>(
28+
value: T,
29+
): value is Exclude<
30+
T,
31+
null | undefined | false | 0 | 0n | '' | typeof document.all
32+
> {
33+
return Boolean(value);
2434
}

0 commit comments

Comments
 (0)