30 lines
1.1 KiB
TypeScript
30 lines
1.1 KiB
TypeScript
// Number formatting that fits a 280px-wide widget.
|
|
|
|
export function formatTokens(n: number): string {
|
|
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(2)}M`;
|
|
if (n >= 10_000) return `${(n / 1_000).toFixed(0)}k`;
|
|
if (n >= 1_000) return `${(n / 1_000).toFixed(1)}k`;
|
|
return n.toString();
|
|
}
|
|
|
|
export function formatPct(num: number, denom: number): string {
|
|
if (denom <= 0) return "0%";
|
|
const pct = Math.min(999, Math.round((num / denom) * 100));
|
|
return `${pct}%`;
|
|
}
|
|
|
|
export function formatCountdown(seconds: number): string {
|
|
const s = Math.max(0, Math.floor(seconds));
|
|
const h = Math.floor(s / 3600);
|
|
const m = Math.floor((s % 3600) / 60);
|
|
const sec = s % 60;
|
|
if (h > 0) return `${h}h ${m.toString().padStart(2, "0")}m`;
|
|
return `${m.toString().padStart(2, "0")}:${sec.toString().padStart(2, "0")}`;
|
|
}
|
|
|
|
/// "2026-05-09" → "Sat" (weekday in user's local).
|
|
export function weekdayShort(date_local: string): string {
|
|
// date_local is already a calendar day; treat it as local midnight.
|
|
const d = new Date(`${date_local}T00:00:00`);
|
|
return d.toLocaleDateString(undefined, { weekday: "short" });
|
|
}
|