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 {
|
|
|
|
const type = typeof str;
|
|
|
|
if (type !== "string") {
|
|
|
|
throw new Error(`Expected "string" - got ${type}`);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|