Last active
January 14, 2025 16:53
-
-
Save papalardo/a7030b286dbeb3ddfcf8f1c0aa342d7a to your computer and use it in GitHub Desktop.
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 const blank = <T>( | |
value: T, | |
): value is Extract<T, null | undefined | '' | [] | Record<string, never>> => { | |
if (value === undefined || value === null) { | |
return true | |
} | |
if (typeof value === 'boolean') { | |
return value === false | |
} | |
if (typeof value === 'number') { | |
return value === 0 | |
} | |
if (typeof value === 'string') { | |
return value.trim() === '' | |
} | |
if (Array.isArray(value)) { | |
return value.length === 0 | |
} | |
if (typeof value === 'object' && value !== null) { | |
return Object.keys(value).length === 0 | |
} | |
return false | |
} | |
export const filled = <T>(value: T): value is NonNullable<T> => !blank(value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment