export const HOUR = 3600e3, MIN = 60e3, DAY = 24 * HOUR; export const clamp = (v: number, a = 0, b = 100) => Math.max(a, Math.min(b, v)); export const pick = (xs: readonly T[]): T => xs[Math.floor(Math.random() * xs.length)]; export const rand = (a: number, b: number) => a + Math.random() * (b - a); export const fmtMB = (n: number) => `${Math.round(n / 1e6)} MB`; export const errorMessage = (e: unknown) => (e instanceof Error ? e.message : String(e)); /** A local date for download file names. */ export function dateKey(t: number) { const d = new Date(t); return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`; } export function download(raw: string, name: string) { const a = document.createElement("a"); a.href = URL.createObjectURL(new Blob([raw], { type: "application/json" })); a.download = name; a.click(); setTimeout(() => URL.revokeObjectURL(a.href), 1000); }