mirror of
https://github.com/status-im/nimbus-gui.git
synced 2025-02-27 18:40:33 +00:00
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'
|
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…
x
Reference in New Issue
Block a user