Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usageFormat.ts

107 lines3.3 KB

1/**
2 * Display formatting for the usage page.
3 *
4 * @module usageFormat
5 */
6import { UsageDay, type UsageSummaryInput } from "@t3tools/contracts";
7​
8const CURRENCY = new Intl.NumberFormat("en-US", {
9 style: "currency",
10 currency: "USD",
11 minimumFractionDigits: 2,
12 maximumFractionDigits: 2,
13});
14​
15const INTEGER = new Intl.NumberFormat("en-US");
16​
17export function formatUsd(value: number): string {
18 return CURRENCY.format(value);
20​
21export function formatCount(value: number): string {
22 return INTEGER.format(Math.round(value));
24​
25/**
26 * Compacts a token count to three significant figures with a unit suffix, so
27 * columns of numbers line up at a glance (`19.9B`, `76.7M`, `804K`).
28 */
29export function formatTokens(value: number): string {
30 const abs = Math.abs(value);
31 if (abs >= 1e12) return `${trim(value / 1e12)}T`;
32 if (abs >= 1e9) return `${trim(value / 1e9)}B`;
33 if (abs >= 1e6) return `${trim(value / 1e6)}M`;
34 if (abs >= 1e3) return `${trim(value / 1e3)}K`;
35 return INTEGER.format(Math.round(value));
37​
38function trim(value: number): string {
39 const abs = Math.abs(value);
40 const digits = abs >= 100 ? 0 : abs >= 10 ? 1 : 2;
41 return value.toFixed(digits).replace(/\.0+$/, "");
43​
44export function formatPercent(share: number, digits = 1): string {
45 return `${(share * 100).toFixed(digits)}%`;
47​
48/** `2026-08-07` to `Aug 7`. */
49export function formatDayShort(day: string): string {
50 const [year, month, dayOfMonth] = day.split("-").map((part) => Number(part));
51 if (year === undefined || month === undefined || dayOfMonth === undefined) return day;
52 const MONTHS = [
53 "Jan",
54 "Feb",
55 "Mar",
56 "Apr",
57 "May",
58 "Jun",
59 "Jul",
60 "Aug",
61 "Sep",
62 "Oct",
63 "Nov",
64 "Dec",
65 ];
66 return `${MONTHS[month - 1] ?? ""} ${dayOfMonth}`;
68​
69/** Inclusive day list between two `YYYY-MM-DD` bounds. */
70export function enumerateDays(sinceDay: string, untilDay: string): readonly string[] {
71 const days: string[] = [];
72 const start = Date.parse(`${sinceDay}T00:00:00Z`);
73 const end = Date.parse(`${untilDay}T00:00:00Z`);
74 if (Number.isNaN(start) || Number.isNaN(end) || end < start) return days;
75​
76 for (let cursor = start; cursor <= end; cursor += 86_400_000) {
77 days.push(new Date(cursor).toISOString().slice(0, 10));
78 }
79 return days;
81​
82/**
83 * The window the page requests, expressed in the viewer's own time zone so days
84 * line up with what they actually experienced.
85 */
86export function makeWindow(days: number, now = new Date()): UsageSummaryInput {
87 const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
88 const format = new Intl.DateTimeFormat("en-CA", {
89 timeZone,
90 year: "numeric",
91 month: "2-digit",
92 day: "2-digit",
93 });
94 const untilDay = format.format(now);
95 // Subtracting fixed milliseconds from `now` lands on the wrong calendar day
96 // around a DST transition. Only "today" needs the zone; the window start is
97 // pure calendar arithmetic on that day, done in UTC where days are uniform.
98 const [year = 0, month = 1, dayOfMonth = 1] = untilDay
99 .split("-")
100 .map((part) => Number.parseInt(part, 10));
101 const start = new Date(Date.UTC(year, month - 1, dayOfMonth - (days - 1)));
102 return {
103 sinceDay: UsageDay.make(start.toISOString().slice(0, 10)),
104 untilDay: UsageDay.make(untilDay),
105 timeZone,
106 };