2021-08-19 12:32:51 +00:00
|
|
|
export function asStringOrNull(str: unknown) {
|
|
|
|
return typeof str === "string" ? str : null;
|
|
|
|
}
|
2021-09-06 13:51:15 +00:00
|
|
|
|
2021-09-26 21:49:16 +00:00
|
|
|
export function asStringOrUndefined(str: unknown) {
|
|
|
|
return typeof str === "string" ? str : undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function asNumberOrUndefined(str: unknown) {
|
|
|
|
return typeof str === "string" ? parseInt(str) : undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function asNumberOrThrow(str: unknown) {
|
|
|
|
return parseInt(asStringOrThrow(str));
|
|
|
|
}
|
|
|
|
|
2021-09-06 13:51:15 +00:00
|
|
|
export function asStringOrThrow(str: unknown): string {
|
2021-10-18 21:07:06 +00:00
|
|
|
if (typeof str !== "string") {
|
|
|
|
throw new Error(`Expected "string" - got ${typeof str}`);
|
2021-09-06 13:51:15 +00:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|