Last active
August 3, 2024 17:06
-
-
Save jrobinsonc/6fa4727e4079928d748c1252a350be05 to your computer and use it in GitHub Desktop.
Type Guard Functions for Various Data Types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isDefined<T>(arg: T | undefined | null): arg is T { | |
return arg !== undefined && arg !== null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Checks if the given argument is empty. | |
* | |
* 🚨 The values listed below are not considered empty (they are falsy, not empty): | |
* - The boolean `false` | |
* - The number zero `0` | |
* | |
* @param arg - The argument to check. It can be of any type. | |
* | |
* @returns `true` if the argument is empty, `false` otherwise. | |
*/ | |
function isEmpty(arg: unknown): boolean { | |
if (typeof arg === 'boolean') { | |
return false; | |
} | |
if (arg === undefined || arg === null) { | |
return true; | |
} | |
if (typeof arg === 'string') { | |
return arg.trim() === ''; | |
} | |
if (typeof arg === 'number') { | |
/** | |
* `isNaN` checks whether the value is not a number or cannot be converted | |
* into a number. `Number.isNaN` only checks if the value is equal to NaN. | |
*/ | |
return isNaN(arg); | |
} | |
if (Array.isArray(arg)) { | |
return arg.length === 0; | |
} | |
if (arg instanceof Map || arg instanceof Set) { | |
return arg.size === 0; | |
} | |
if (typeof arg === 'object') { | |
return Object.keys(arg).length === 0; | |
} | |
throw new Error(`The given argument could not be parsed: ${String(arg)}`); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Checks if a value is null or undefined. | |
* | |
* @param value - The value to check. | |
* @returns `true` if the value is null or undefined; otherwise, return `false`. | |
*/ | |
function isNil(arg: unknown): arg is null | undefined { | |
return arg === null || arg === undefined; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This function checks if the given argument is a plain object or not. | |
* | |
* @template T - This is the type parameter, which defaults to `Record<string, unknown>`. | |
* @param arg - The argument that needs to be checked. | |
* @returns `true` if the argument is a plain object, `false` otherwise. | |
*/ | |
function isPlainObject<T = Record<string, unknown>>(arg: unknown): arg is T { | |
return typeof arg === 'object' && arg !== null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isStringifiable(arg: unknown): arg is string | number | boolean | null | { toString(): string } { | |
return ( | |
typeof arg === 'string' || | |
typeof arg === 'number' || | |
typeof arg === 'boolean' || | |
arg === null || | |
(typeof arg === 'object' && arg !== null && typeof arg.toString === 'function') | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment