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

usageFormat.ts

108 lines3.4 KB

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