Last active
April 22, 2025 20:57
-
-
Save jrobinsonc/6fa4727e4079928d748c1252a350be05 to your computer and use it in GitHub Desktop.
Type Guard Functions for Various Data Types
This file contains hidden or 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
export default function isDefined<T>(arg: T | undefined | null): arg is T { | |
return arg !== undefined && arg !== null; | |
} |
This file contains hidden or 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. | |
*/ | |
export default 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 hidden or 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 arg - The value to check. | |
* @returns `true` if the value is null or undefined; otherwise, return `false`. | |
*/ | |
export default function isNil(arg: unknown): arg is null | undefined { | |
return arg === null || arg === undefined; | |
} |
This file contains hidden or 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 `unknown`. | |
* @param arg - The argument that needs to be checked. | |
* @returns Returns `true` if the argument is a plain object, `false` otherwise. | |
*/ | |
export default function isPlainObject<T>(arg: T): arg is T { | |
return ( | |
typeof arg === 'object' && | |
arg !== null && | |
!Array.isArray(arg) && | |
Object.getPrototypeOf(arg) === Object.prototype | |
); | |
} |
This file contains hidden or 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
export default 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