feat(src/utilities): Add function that asserts that a value is not null or undefined.

This commit is contained in:
Emil Ivanichkov 2024-04-23 14:05:59 +03:00 committed by Emil Ivanichkov
parent 3dbb1220b5
commit ce6d719779
1 changed files with 22 additions and 0 deletions

View File

@ -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
}