Skip to content

Fix compact star formatter rounding gap at 999.5k+#11

Merged
kanywst merged 2 commits into
mainfrom
fix/compact-star-rounding
May 6, 2026
Merged

Fix compact star formatter rounding gap at 999.5k+#11
kanywst merged 2 commits into
mainfrom
fix/compact-star-rounding

Conversation

@kanywst
Copy link
Copy Markdown
Owner

@kanywst kanywst commented May 6, 2026

Summary

Gemini Code Assist flagged a rounding gap in the manual formatter introduced in #9: 999,999 stars satisfies the >= 1_000 branch 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 uppercase K, 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 .0 since Intl emits "1k" rather than "1.0k" for exact thousands. A new test pins the edge case at 999,999 directly.

Test plan

  • npm run lint
  • npm run build
  • npm run test -- --run (3 files, 9 tests)

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 6, 2026

Warning

Rate limit exceeded

@kanywst has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 38 minutes and 27 seconds before requesting another review.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 15f8260b-4575-49c9-92ca-bc4c7ced92d3

📥 Commits

Reviewing files that changed from the base of the PR and between 87cb847 and 1f12203.

📒 Files selected for processing (2)
  • src/lib/format.test.ts
  • src/lib/format.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/compact-star-rounding

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.
@kanywst kanywst force-pushed the fix/compact-star-rounding branch from 85032e3 to 5a7a15e Compare May 6, 2026 17:08
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

medium

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.
@kanywst kanywst merged commit 0cd71ea into main May 6, 2026
3 checks passed
@kanywst kanywst deleted the fix/compact-star-rounding branch May 6, 2026 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant