2021-08-19 09:32:51 -03:00
|
|
|
export function asStringOrNull(str: unknown) {
|
|
|
|
return typeof str === "string" ? str : null;
|
|
|
|
}
|
2021-09-06 10:51:15 -03:00
|
|
|
|
2021-09-26 18:49:16 -03: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 10:51:15 -03:00
|
|
|
export function asStringOrThrow(str: unknown): string {
|
|
|
|
const type = typeof str;
|
|
|
|
if (type !== "string") {
|
|
|
|
throw new Error(`Expected "string" - got ${type}`);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|