Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(ui): compact k/m number formatting and years/months age display in stats hero (#798)

Matt Van Horna3ca0dd887a4
diff --git a/packages/ui/__tests__/lib/format.test.ts b/packages/ui/__tests__/lib/format.test.ts
index 76e15a6c0..121574bb8 100644
--- a/packages/ui/__tests__/lib/format.test.ts
+++ b/packages/ui/__tests__/lib/format.test.ts
@@ -1,5 +1,44 @@
import { describe, expect, it } from "vitest";
-import { formatBytes, formatPercent } from "../../src/lib/format";
+import {
+ formatAgeDays,
+ formatBytes,
+ formatCompact,
+ formatPercent,
+} from "../../src/lib/format";
+
+describe("formatCompact", () => {
+ it("leaves counts below one thousand unchanged", () => {
+ expect(formatCompact(0)).toBe("0");
+ expect(formatCompact(999)).toBe("999");
+ });
+
+ it("shortens thousands and millions without unnecessary decimal zeroes", () => {
+ expect(formatCompact(1_000)).toBe("1K");
+ expect(formatCompact(98_432)).toBe("98.4K");
+ expect(formatCompact(999_999)).toBe("1M");
+ expect(formatCompact(1_234_567)).toBe("1.2M");
+ });
+});
+
+describe("formatAgeDays", () => {
+ it("formats ages shorter than a month in days", () => {
+ expect(formatAgeDays(0.5)).toBe("< 1 day");
+ expect(formatAgeDays(1)).toBe("1 day");
+ expect(formatAgeDays(18)).toBe("18 days");
+ });
+
+ it("splits sub-year ages into months and days", () => {
+ expect(formatAgeDays(45)).toBe("1 month 15 days");
+ });
+
+ it("splits longer ages into years and months", () => {
+ expect(formatAgeDays(365)).toBe("1 year");
+ expect(formatAgeDays(400)).toBe("1 year 1 month");
+ expect(formatAgeDays(725)).toBe("2 years");
+ expect(formatAgeDays(730)).toBe("2 years");
+ expect(formatAgeDays(1_000)).toBe("2 years 9 months");
+ });
+});
describe("formatPercent", () => {
it("rounds ratios to whole percentages by default", () => {
diff --git a/packages/ui/__tests__/stats/size-class-hero.test.tsx b/packages/ui/__tests__/stats/size-class-hero.test.tsx
new file mode 100644
index 000000000..3da16db77
--- /dev/null
+++ b/packages/ui/__tests__/stats/size-class-hero.test.tsx
@@ -0,0 +1,49 @@
+import { render, screen } from "@testing-library/react";
+import type { StatsScale } from "@repowise-dev/types/stats";
+import { describe, expect, it } from "vitest";
+import { SizeClassHero } from "../../src/stats/size-class-hero.js";
+
+function makeScale(overrides: Partial<StatsScale> = {}): StatsScale {
+ return {
+ file_count: 1_234_567,
+ symbol_count: 98_432,
+ entry_point_count: 12,
+ module_count: 80,
+ total_nloc: 4_200_000,
+ language_count: 3,
+ languages: [],
+ size_class: {
+ name: "Metropolis",
+ blurb: "A large codebase.",
+ nloc: 4_200_000,
+ },
+ ...overrides,
+ };
+}
+
+describe("SizeClassHero", () => {
+ it("renders large file and symbol counts compactly", () => {
+ render(<SizeClassHero scale={makeScale()} />);
+
+ expect(screen.getByText("1.2M")).toBeInTheDocument();
+ expect(screen.getByText("98.4K")).toBeInTheDocument();
+ expect(screen.queryByText("1,234,567")).not.toBeInTheDocument();
+ expect(screen.queryByText("98,432")).not.toBeInTheDocument();
+ });
+
+ it("renders small counts verbatim", () => {
+ render(<SizeClassHero scale={makeScale({ file_count: 999, symbol_count: 42 })} />);
+
+ expect(screen.getByText("999")).toBeInTheDocument();
+ expect(screen.getByText("42")).toBeInTheDocument();
+ expect(screen.getByText("3")).toBeInTheDocument();
+ });
+
+ it("constrains figure values to their tiles", () => {
+ render(<SizeClassHero scale={makeScale()} />);
+
+ const value = screen.getByText("1.2M");
+ expect(value).toHaveClass("truncate");
+ expect(value.parentElement).toHaveClass("min-w-0");
+ });
+});
diff --git a/packages/ui/src/lib/format.ts b/packages/ui/src/lib/format.ts
index 82663bd4a..ce2641fda 100644
--- a/packages/ui/src/lib/format.ts
+++ b/packages/ui/src/lib/format.ts
@@ -7,6 +7,16 @@ export function formatNumber(n: number): string {
return new Intl.NumberFormat().format(n);
}
+/** Format large counts compactly: 1234567 → "1.2M", 98432 → "98.4K", 999 → "999" */
+export function formatCompact(n: number): string {
+ if (n >= 1_000_000) return `${Number((n / 1_000_000).toFixed(1))}M`;
+ if (n >= 1_000) {
+ const thousands = Number((n / 1_000).toFixed(1));
+ return thousands === 1_000 ? "1M" : `${thousands}K`;
+ }
+ return String(n);
+}
+
/** Format token counts: 4200000 → "4.2M", 980000 → "980K", 1234 → "1,234" */
export function formatTokens(n: number): string {
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`;
@@ -103,16 +113,32 @@ export function truncatePath(path: string, maxChars = 60): string {
return filename.length <= maxChars ? `…/${filename}` : `…${filename.slice(-(maxChars - 1))}`;
}
-/** Format age in days to human-readable: 45 → "1.5 months", 400 → "1.1 years" */
+/** Format age in days to split units: 45 → "1 month 15 days", 400 → "1 year 1 month" */
export function formatAgeDays(n: number): string {
if (n < 1) return "< 1 day";
- if (n < 30) return `${n} day${n !== 1 ? "s" : ""}`;
- if (n < 365) {
- const months = Math.round(n / 30 * 10) / 10;
- return `${months} month${months !== 1 ? "s" : ""}`;
+
+ const days = Math.floor(n);
+ const pluralize = (value: number, unit: string) =>
+ `${value} ${unit}${value === 1 ? "" : "s"}`;
+
+ if (days < 30) return pluralize(days, "day");
+ if (days < 365) {
+ const months = Math.floor(days / 30);
+ const remainingDays = days % 30;
+ return [pluralize(months, "month"), remainingDays && pluralize(remainingDays, "day")]
+ .filter(Boolean)
+ .join(" ");
+ }
+
+ let years = Math.floor(days / 365);
+ let months = Math.floor((days % 365) / 30);
+ if (months === 12) {
+ years += 1;
+ months = 0;
}
- const years = Math.round(n / 365 * 10) / 10;
- return `${years} year${years !== 1 ? "s" : ""}`;
+ return [pluralize(years, "year"), months && pluralize(months, "month")]
+ .filter(Boolean)
+ .join(" ");
}
/** Strip inline markdown emphasis/code markers for plain-text display:
diff --git a/packages/ui/src/stats/size-class-hero.tsx b/packages/ui/src/stats/size-class-hero.tsx
index b27b97211..c2681cad8 100644
--- a/packages/ui/src/stats/size-class-hero.tsx
+++ b/packages/ui/src/stats/size-class-hero.tsx
@@ -8,7 +8,7 @@ import {
Globe2,
} from "lucide-react";
import type { StatsScale } from "@repowise-dev/types/stats";
-import { formatNumber, formatLOC } from "../lib/format";
+import { formatCompact, formatNumber, formatLOC } from "../lib/format";
import { NLOC_HINT } from "./stat-callout";
const SIZE_ICON: Record<string, React.ComponentType<{ className?: string }>> = {
@@ -44,8 +44,8 @@ export function SizeClassHero({ scale, repoName }: SizeClassHeroProps) {
const figures: HeroFigure[] = [
{ label: "Lines of code", value: formatLOC(scale.total_nloc), hint: NLOC_HINT },
- { label: "Files", value: formatNumber(scale.file_count) },
- { label: "Symbols", value: formatNumber(scale.symbol_count) },
+ { label: "Files", value: formatCompact(scale.file_count) },
+ { label: "Symbols", value: formatCompact(scale.symbol_count) },
{ label: "Languages", value: formatNumber(scale.language_count) },
];
@@ -84,9 +84,9 @@ export function SizeClassHero({ scale, repoName }: SizeClassHeroProps) {
<div
key={f.label}
title={f.hint}
- className={`rounded-xl border border-[var(--color-border-subtle,var(--color-border-default))] bg-[var(--color-bg-surface)]/70 px-4 py-3 backdrop-blur-sm${f.hint ? " cursor-help" : ""}`}
+ className={`min-w-0 rounded-xl border border-[var(--color-border-subtle,var(--color-border-default))] bg-[var(--color-bg-surface)]/70 px-4 py-3 backdrop-blur-sm${f.hint ? " cursor-help" : ""}`}
>
- <p className="text-2xl font-bold tabular-nums text-[var(--color-text-primary)] sm:text-3xl">
+ <p className="truncate text-2xl font-bold tabular-nums text-[var(--color-text-primary)] sm:text-3xl">
{f.value}
</p>
<p className="mt-0.5 text-[11px] font-medium uppercase tracking-wider text-[var(--color-text-tertiary)]">