Skip to content

Add support for formatting numbers #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

codeandcats
Copy link

@codeandcats codeandcats commented Mar 20, 2025

Thank you for this great little library, I use it often.

Problem

The option to include the number in the output is very convenient.

pluralize('record', recordCount, true) // '1000000 records'

versus

`${recordCount} ${pluralize('record', recordCount)}` // '1000000 records'

However most of the time I cannot use this option because I wish to format the number so that it includes grouping separators, to make large numbers much more readable for users.

`${formatInteger(recordCount)} ${pluralize('record', recordCount)}` // '1,000,000 records'

Proposed Solution

This PR adds support for passing a format function as the inclusive parameter

pluralize('record', recordCount, formatInteger) // '1,000,000 records'

Or alternatively, passing an Intl.NumberFormat instance

const integerFormatter = new Intl.NumberFormat(undefined, { useGrouping: true })

pluralize('record', recordCount, integerFormatter) // '1,000,000 records'

I have:
✅ Updated the JSDoc comments appropriately
✅ Updated Readme with formatting number examples
✅ Added tests which include full coverage for the added lines

Additionally

Despite using this wonderful library for years, I was unaware until now that you could pluralize pronouns. 😮 I don't believe the readme makes this especially obvious, so I added an example to hopefully better highlight this awesome capability.

Thank you for your consideration of this PR. If you agree with these changes I would love to see them published at your earliest convenience. Please let me know if you would like any changes.

@codeandcats codeandcats force-pushed the feat/number-formatting branch from 1349056 to be2ba2d Compare March 22, 2025 00:03
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