Better handle bigint

This commit is contained in:
2024-05-31 14:24:57 +08:00
parent 15ff42d8c4
commit 8437bf1436
36 changed files with 320 additions and 227 deletions

View File

@@ -1,3 +1,5 @@
import { isNumNaN, parseBigInt } from "../utils.ts";
export async function get_string(value: FormDataEntryValue | null) {
if (value === null) return null;
return typeof value === "string" ? value : await value.text();
@@ -28,3 +30,14 @@ export async function parse_int<T extends number | null>(
if (isNaN(n)) return def;
return n;
}
export async function parse_big_int<T extends number | bigint | null>(
value: FormDataEntryValue | null,
def: T,
): Promise<number | bigint | T> {
if (value === null) return def;
const v = typeof value === "string" ? value : await value.text();
const n = parseBigInt(v);
if (isNumNaN(n)) return def;
return n;
}