mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-21 04:39:06 +00:00
self-review-fix
This commit is contained in:
@@ -137,9 +137,28 @@ export function extractDomain(url) {
|
||||
}
|
||||
}
|
||||
|
||||
export function assertExists<T>(value: T | null | undefined, err?: string): T {
|
||||
export type NonEmptyString = string & { __brand: "NonEmptyString" };
|
||||
export const parseMaybeNonEmptyString = (
|
||||
s: string,
|
||||
trim = true,
|
||||
): NonEmptyString | null => {
|
||||
s = trim ? s.trim() : s;
|
||||
return s.length > 0 ? (s as NonEmptyString) : null;
|
||||
};
|
||||
export const parseNonEmptyString = (s: string, trim = true): NonEmptyString =>
|
||||
assertExists(parseMaybeNonEmptyString(s, trim), "Expected non-empty string");
|
||||
|
||||
export const assertExists = <T>(
|
||||
value: T | null | undefined,
|
||||
err?: string,
|
||||
): T => {
|
||||
if (value === null || value === undefined) {
|
||||
throw new Error(`Assertion failed: ${err ?? "value is null or undefined"}`);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
};
|
||||
|
||||
export const assertExistsAndNonEmptyString = (
|
||||
value: string | null | undefined,
|
||||
): NonEmptyString =>
|
||||
parseNonEmptyString(assertExists(value, "Expected non-empty string"));
|
||||
|
||||
Reference in New Issue
Block a user