From ce6d7197799ac020f1dedc851fe303a1da5df8f9 Mon Sep 17 00:00:00 2001 From: Emil Ivanichkov Date: Tue, 23 Apr 2024 14:05:59 +0300 Subject: [PATCH] feat(src/utilities): Add function that asserts that a value is not null or undefined. --- src/utilities.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/utilities.ts b/src/utilities.ts index 936cf779..f429acec 100644 --- a/src/utilities.ts +++ b/src/utilities.ts @@ -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( + 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 +}