Fix compact star formatter rounding gap at 999.5k+#11
Conversation
|
Warning Rate limit exceeded
To continue reviewing without waiting, purchase usage credits in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
The branch-by-magnitude implementation merged in #9 had a rounding gap: 999_999 satisfies the >= 1_000 branch but not >= 1_000_000, so `(999_999 / 1_000).toFixed(1)` produced '1000.0' and the function returned '1000.0k' — the exact failure mode this formatter was introduced to prevent. Delegate to Intl.NumberFormat with compact notation, which handles unit transitions correctly (999_999 -> '1M'). Patch the 'K' suffix to lowercase so the design keeps small-k thousands / capital-M millions. Test asserts the edge case directly.
85032e3 to
5a7a15e
Compare
There was a problem hiding this comment.
Code Review
This pull request refactors the formatCompactStars function to use Intl.NumberFormat for compact notation, which simplifies the logic and improves rounding accuracy. The unit tests have been updated to reflect changes in the output format, such as the removal of trailing zeros for whole numbers. A review comment suggests using a global regular expression when replacing the 'K' suffix to ensure robustness across different JavaScript environments.
I am having trouble creating individual review comments. Click here to see my feedback.
src/lib/format.ts (8)
The current implementation uses .replace('K', 'k'), which only replaces the first occurrence of an uppercase 'K'. While this is sufficient for the 'en' locale's compact notation, using a regular expression with the global flag (e.g., replace(/K/g, 'k')) or targeting the suffix specifically (e.g., replace(/K$/, 'k')) is more robust against potential variations in how different JavaScript engines or ICU versions might format the string.
return COMPACT_FORMATTER.format(count).replace(/K/g, 'k');
Per code review feedback. `.replace('K', 'k')` only swaps the first
match; while the current 'en' compact output only contains one, future
locale or ICU versions could emit more than one (or position it
differently). Use `/K/g` so the suffix normalization stays robust.
Summary
Gemini Code Assist flagged a rounding gap in the manual formatter introduced in #9: 999,999 stars satisfies the
>= 1_000branch but not>= 1_000_000, so(999_999 / 1_000).toFixed(1)resolved to"1000.0"and the function returned"1000.0k"— exactly the failure mode the formatter was meant to prevent.Replace the magnitude-branching implementation with
Intl.NumberFormat({ notation: 'compact', maximumFractionDigits: 1 }), which delegates unit transitions to ICU and rounds 999,999 up to"1M". ICU emits an uppercaseK, so we patch it back to lowercase to keep the existing design (small-k thousands, capital-M millions).Existing assertions are updated to drop the trailing
.0sinceIntlemits"1k"rather than"1.0k"for exact thousands. A new test pins the edge case at 999,999 directly.Test plan
npm run lintnpm run buildnpm run test -- --run(3 files, 9 tests)