Add conditional paremeters

This commit is contained in:
lomonoshka
2023-08-07 12:10:55 +03:00
parent e205ebeb97
commit 994d766391
2 changed files with 22 additions and 2 deletions

View File

@@ -48,4 +48,13 @@ export function safeGet<V>(obj: Partial<Record<string, V>>, key: string, errorMe
const value = obj[key];
if (value === undefined) throw new Error(`Key '${key.toString()}' not found in object. Available keys: ${Object.keys(obj).join(', ')}.${errorMessage ? ` ${errorMessage}` : ''}`);
return value;
}
const prefix = 'Requirement not met';
export default function must(condition: unknown, message?: string | (() => string)): asserts condition {
if (condition) return;
const provided = typeof message === 'function' ? message() : message;
const value = provided ? `${prefix}: ${provided}` : prefix;
throw new Error(value);
}