Skip to content

Commit

Permalink
feat: add type guard isTruthy
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia authored Jul 16, 2024
1 parent fe0cb80 commit bb44657
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
46 changes: 36 additions & 10 deletions src/typeGuards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import {
isNonEmptyString,
isNotNullish,
isString,
isTruthy,
} from './typeGuards';

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

describe('isEmptyObject', () => {
assertTrue(isEmptyObject, [{}]);
assertFalse(isEmptyObject, [{ foo: 'bar' }, [], null, undefined, '']);
});

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

describe('isArrayOfStrings', () => {
expect(isArrayOfStrings(['foo', 'bar'])).toBe(true);
expect(isArrayOfStrings([])).toBe(true);
assertFalse(isArrayOfStrings, [null, undefined, ['', null]]);
describe('isString', () => {
assertTrue(isString, ['foo', '1', 'true', '']);
assertFalse(isString, [/foo/, 1, true, null, undefined]);
});

describe('emptyObject', () => {
assertTrue(isEmptyObject, [{}]);
assertFalse(isEmptyObject, [{ foo: 'bar' }, [], null, undefined, '']);
describe('isTruthy', () => {
assertTrue(isTruthy, [
true,
1,
-1,
Infinity,
' ',
'0',
'false',
[],
{},
document,
]);
assertFalse(isTruthy, [
null,
undefined,
false,
0,
-0,
NaN,
'',
document.all,
]);
});
});
32 changes: 21 additions & 11 deletions src/typeGuards.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
export function isString(value: unknown): value is string {
return typeof value === 'string';
export function isArrayOfStrings(value: unknown): value is Array<string> {
return value instanceof Array && value.every(isString);
}

export function isEmptyObject(value: unknown): value is Record<string, never> {
return Boolean(
value &&
typeof value === 'object' &&
!(value instanceof Array) &&
Object.keys(value).length === 0,
);
}

export function isNonEmptyString(value: unknown): value is Exclude<string, ''> {
Expand All @@ -10,15 +19,16 @@ 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(isString);
export function isString(value: unknown): value is string {
return typeof value === 'string';
}

export function isEmptyObject(value: unknown): value is Record<string, never> {
return Boolean(
value &&
typeof value === 'object' &&
!(value instanceof Array) &&
Object.keys(value).length === 0,
);
/** https://developer.mozilla.org/en-US/docs/Glossary/Falsy */
export function isTruthy<T>(
value: T,
): value is Exclude<
T,
null | undefined | false | 0 | 0n | '' | typeof document.all
> {
return Boolean(value);
}

0 comments on commit bb44657

Please sign in to comment.