Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ async function fetchStats(): Promise<Stats> {
return { stars, chatters };
}

const compact = new Intl.NumberFormat("en", { notation: "compact", maximumFractionDigits: 1 });
const round = (n: number) => compact.format(n).toLowerCase();
const TITLES: Record<keyof Stats, string> = { stars: "GitHub stars", chatters: "Discord members" };

// Fills every element with a `data-stat="stars"` / `data-stat="chatters"`
// attribute, e.g. "1,515 stars". Elements stay empty if a fetch fails; the
// links around them still work.
// attribute, e.g. "1.5k stars", and titles the surrounding link with the exact
// count. Elements stay empty if a fetch fails; the links still work.
export async function renderStats() {
const cached = readCache();
const stats = cached ?? (await fetchStats());
Expand All @@ -82,6 +86,9 @@ export async function renderStats() {
for (const el of document.querySelectorAll<HTMLElement>("[data-stat]")) {
const key = el.dataset.stat as keyof Stats;
const value = stats[key];
if (typeof value === "number") el.textContent = `${value.toLocaleString()} ${key}`;
if (typeof value !== "number") continue;
el.textContent = `${round(value)} ${key}`;
const link = el.closest("a");
if (link) link.title = `${value.toLocaleString()} ${TITLES[key]}`;
}
}
Loading