From e8d9d094f3aa7dd68d335502b97378a76c35bc37 Mon Sep 17 00:00:00 2001 From: theluckystrike <51033404+theluckystrike@users.noreply.github.com> Date: Mon, 5 Oct 2026 17:07:07 +0000 Subject: [PATCH] fix: star count shouldn't round down near the next thousand --- .../__tests__/unit/github-star-count.test.ts | 26 +++++++++++++++++++ apps/web/utils/github.ts | 4 +-- 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 apps/web/__tests__/unit/github-star-count.test.ts diff --git a/apps/web/__tests__/unit/github-star-count.test.ts b/apps/web/__tests__/unit/github-star-count.test.ts new file mode 100644 index 00000000000..afe17eb0a4a --- /dev/null +++ b/apps/web/__tests__/unit/github-star-count.test.ts @@ -0,0 +1,26 @@ +import { describe, expect, it } from "vitest"; +import { formatStarCount } from "@/utils/github"; + +describe("formatStarCount", () => { + it("returns an empty string when the count is unavailable", () => { + expect(formatStarCount(0)).toBe(""); + }); + + it("returns counts below 1000 unchanged", () => { + expect(formatStarCount(999)).toBe("999"); + }); + + it("formats thousands with one decimal place", () => { + expect(formatStarCount(16_420)).toBe("16.4k"); + }); + + it("drops a trailing .0", () => { + expect(formatStarCount(16_000)).toBe("16k"); + expect(formatStarCount(16_049)).toBe("16k"); + }); + + it("rounds up to the next whole thousand", () => { + expect(formatStarCount(16_960)).toBe("17k"); + expect(formatStarCount(19_999)).toBe("20k"); + }); +}); diff --git a/apps/web/utils/github.ts b/apps/web/utils/github.ts index 6da86af44e6..1bdbf890da0 100644 --- a/apps/web/utils/github.ts +++ b/apps/web/utils/github.ts @@ -25,9 +25,7 @@ export function formatStarCount(count: number): string { if (count === 0) return ""; if (count >= 1000) { const formatted = (count / 1000).toFixed(1); - return formatted.endsWith(".0") - ? `${Math.floor(count / 1000)}k` - : `${formatted}k`; + return `${formatted.replace(/\.0$/, "")}k`; } return count.toString(); }