Skip to content

feat: improve Amount formatting capabilities - #871

Merged
rohanchkrabrty merged 5 commits into
mainfrom
worktree-amount-formatting-improvements
Sep 25, 2026
Merged

rohanchkrabrty merged 5 commits into
mainfrom
worktree-amount-formatting-improvements

Conversation

@ravisuhag

Copy link
Copy Markdown
Member

Summary

Implements the remaining items from #595, plus a tabularNums prop.

New props

  • notation ('standard' | 'compact', default 'standard') — compact formatting like $1.2M / $13K for dashboards and summary views. Note: compact rounds aggressively by design (Intl behavior), so small values like $12.99 render as $13.
  • currencyDisplay: 'narrowSymbol' — always shows the narrow symbol ($ instead of US$ in non-US locales).
  • signDisplay ('auto' | 'always' | 'exceptZero' | 'never', default 'auto') — controls the +/- sign, useful for gains/losses.
  • tabularNums (default true) — fixed-width figures so digits align across table rows. Previously this was always on via CSS; it is now a prop so running text can opt out for proportional figures. Default behavior is unchanged.

Performance

  • Intl.NumberFormat instances are now memoized in a module-level cache (keyed by locale + options, capped at 64 entries). Previously every render created 3 formatter instances, which adds up fast when Amount renders in table rows.
  • isValidCurrency() and getCurrencyDecimals() are merged into a single cached getCurrencyInfo() lookup. Decimals now come from resolvedOptions().maximumFractionDigits instead of regex-matching formatted output.

Not included

  • Issue item 6 (string slicing edge case) is already fixed on main — the padStart conversion handles short strings correctly ("5" → $0.05).

Docs

  • Playground controls for the new props, plus notation, signDisplay, and tabularNums example sections.

Test plan

  • 15 new test cases covering all four props and their interactions (compact + hideDecimals / string values / hideCurrency / explicit fraction digits; sign preserved when hideCurrency strips the currency token; tabular class toggling).
  • Full Amount suite: 64/64 passing. Lint and typecheck clean for the touched files.

Closes #595

- Add notation prop for compact formatting ($1.2M) in dashboards
- Add narrowSymbol option to currencyDisplay
- Add signDisplay prop for gains/losses (+/- sign control)
- Add tabularNums prop (default true) to toggle fixed-width figures
- Memoize Intl.NumberFormat instances module-wide; resolve currency
  validity and decimals in one cached lookup
@vercel

vercel Bot commented Jul 31, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
apsara Ready Ready Preview Sep 25, 2026 8:32am UTC

@coderabbitai

coderabbitai Bot commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

Next included review available in 2 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 6f90b7e1-eb81-4b1d-acee-386f89438243

📥 Commits

Reviewing files that changed from the base of the PR and between 9a2aadf and e5a38f7.

📒 Files selected for processing (5)
  • apps/www/src/content/docs/components/amount/demo.ts
  • apps/www/src/content/docs/components/amount/index.mdx
  • packages/raystack/components/amount/__tests__/amount.test.tsx
  • packages/raystack/components/amount/amount.module.css
  • packages/raystack/components/amount/amount.tsx
📝 Walkthrough

Walkthrough

The Amount component now supports compact notation, configurable sign display, narrow currency symbols, and tabular numerals. It combines currency validation and decimal detection through cached formatter metadata. Formatter instances are cached for reuse. Tests and documentation cover the new options and formatting combinations.

Sequence Diagram(s)

sequenceDiagram
  participant Amount
  participant getCurrencyInfo
  participant getFormatter
  participant IntlNumberFormat
  Amount->>getCurrencyInfo: Validate currency and read decimal count
  getCurrencyInfo->>getFormatter: Request cached currency formatter
  getFormatter->>IntlNumberFormat: Create or reuse formatter
  Amount->>getFormatter: Format with notation and signDisplay
  getFormatter->>IntlNumberFormat: Create or reuse formatter
  Amount->>Amount: Render value with optional tabular styling
Loading

Suggested reviewers: rohanchkrabrty, shreyag02

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the primary change: expanded Amount formatting capabilities.
Description check ✅ Passed The description directly explains the new Amount props, performance changes, tests, and documentation updates.
Linked Issues check ✅ Passed The changes implement the linked issue requirements for notation, narrow symbols, sign display, formatter caching, and related Amount behavior.
Out of Scope Changes check ✅ Passed The playground, documentation, tests, styling, and tabularNums additions are directly related to the stated Amount formatting objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/raystack/components/amount/amount.tsx`:
- Around line 142-163: Bound currencyInfoCache before inserting entries in
getCurrencyInfo, using the same 64-entry limit as formatterCache; alternatively,
skip caching when the currency code is invalid. Preserve cached lookups for
valid currencies while ensuring unlimited distinct invalid inputs cannot cause
persistent module-level growth.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d73b795e-3e56-4627-9a51-35c6ad4032e4

📥 Commits

Reviewing files that changed from the base of the PR and between 5e38ff6 and 9a2aadf.

📒 Files selected for processing (6)
  • apps/www/src/components/playground/amount-examples.tsx
  • apps/www/src/content/docs/components/amount/demo.ts
  • apps/www/src/content/docs/components/amount/index.mdx
  • packages/raystack/components/amount/__tests__/amount.test.tsx
  • packages/raystack/components/amount/amount.module.css
  • packages/raystack/components/amount/amount.tsx

Comment thread packages/raystack/components/amount/amount.tsx Outdated
Comment thread packages/raystack/components/amount/amount.tsx Outdated
Comment thread apps/www/src/content/docs/components/amount/demo.ts
Comment thread packages/raystack/components/amount/amount.tsx Outdated
…tting-improvements

# Conflicts:
#	apps/www/src/components/playground/amount-examples.tsx
#	apps/www/src/content/docs/components/amount/index.mdx
#	packages/raystack/components/amount/amount.tsx
- Fall back to USD when currency is not a string. The lookup called
  toUpperCase outside its try, so null rendered the raw value.
- Set proportional-nums when tabularNums is false, so a tabular parent
  such as Table or DataTable does not override it.
- Read currency decimals with a plain Intl.NumberFormat, so the lookup
  does not use a slot in the formatter cache.
- Pin the short string case from #595 and use the CSS module in the
  tabularNums tests.
- Document narrowSymbol, and bring the new JSDoc and docs in line with
  the writing style rules.
- hideDecimals turned values between -1 and 0 into -0, which printed
  "-$0". Both the number and the string path now print "$0".
- Document that hideDecimals truncates, and that compact notation
  rounds the abbreviated value instead. Pin both with tests.
@pkg-pr-new

pkg-pr-new Bot commented Sep 25, 2026

Copy link
Copy Markdown

Open in StackBlitz

pnpm add https://pkg.pr.new/@raystack/apsara@871

commit: e5a38f7

@rohanchkrabrty
rohanchkrabrty merged commit 379e35c into main Sep 25, 2026
8 checks passed
@rohanchkrabrty
rohanchkrabrty deleted the worktree-amount-formatting-improvements branch September 25, 2026 08:33

This branch was successfully deployed

1 active deployment
Preview — e5a38f76 Deployed Sep 25, 2026 by vercel[bot]
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.

[Amount] Improve formatting capabilities and fix edge cases

3 participants