feat(src/utilities): Add function that asserts that a value is not null or undefined.
This commit is contained in:
parent
3dbb1220b5
commit
ce6d719779
|
@ -104,3 +104,25 @@ export const getDepositTitle = ({
|
|||
return 'Deposit Funds'
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asserts that a value is not null or undefined.
|
||||
*
|
||||
* @param value - The value to check.
|
||||
* @param errorMessage - Optional error message to throw if the assertion fails.
|
||||
* @returns The value itself if it is not null or undefined.
|
||||
* @throws Error if the value is null, undefined, or an empty string.
|
||||
*/
|
||||
export function assertNotNull<T>(
|
||||
value: T | null | undefined,
|
||||
errorMessage?: string,
|
||||
): T {
|
||||
if (
|
||||
value === null ||
|
||||
value === undefined ||
|
||||
(typeof value === 'string' && !value.length)
|
||||
) {
|
||||
throw new Error(errorMessage ?? 'Assertion failed: value is null')
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue