From bb44657bc7f52a48b74caf138ba83aa112a95c5d Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Tue, 16 Jul 2024 10:33:56 +0200 Subject: [PATCH] feat: add type guard `isTruthy` --- src/typeGuards.test.ts | 46 +++++++++++++++++++++++++++++++++--------- src/typeGuards.ts | 32 +++++++++++++++++++---------- 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/src/typeGuards.test.ts b/src/typeGuards.test.ts index 7ddaec3..b7a1089 100644 --- a/src/typeGuards.test.ts +++ b/src/typeGuards.test.ts @@ -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', () => { @@ -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, + ]); }); }); diff --git a/src/typeGuards.ts b/src/typeGuards.ts index 9432c3a..52c7c6e 100644 --- a/src/typeGuards.ts +++ b/src/typeGuards.ts @@ -1,5 +1,14 @@ -export function isString(value: unknown): value is string { - return typeof value === 'string'; +export function isArrayOfStrings(value: unknown): value is Array { + return value instanceof Array && value.every(isString); +} + +export function isEmptyObject(value: unknown): value is Record { + return Boolean( + value && + typeof value === 'object' && + !(value instanceof Array) && + Object.keys(value).length === 0, + ); } export function isNonEmptyString(value: unknown): value is Exclude { @@ -10,15 +19,16 @@ export function isNotNullish(value: T): value is NonNullable { return value != null; } -export function isArrayOfStrings(value: unknown): value is Array { - 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 { - 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( + value: T, +): value is Exclude< + T, + null | undefined | false | 0 | 0n | '' | typeof document.all +> { + return Boolean(value); }