Skip to content
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

localize-currency-amount: Utility for locale-aware currency formatting #36758

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from

Conversation

nbloomf
Copy link
Contributor

@nbloomf nbloomf commented Oct 15, 2019

Formatting monetary amounts depends on the currency and the user's locale. This package adds a function for dealing with this.

Changes proposed in this Pull Request

  • Adds localizeMonetaryAmount(). This improves on other methods by working with integers only and using non breaking spaces everywhere.

Testing instructions

npm run test-packages localize-monetary-amount

@nbloomf nbloomf requested a review from sirbrillig October 15, 2019 04:34
@matticbot
Copy link
Contributor

@matticbot
Copy link
Contributor

matticbot commented Oct 15, 2019

This PR does not affect the size of JS and CSS bundles shipped to the user's browser.

Generated by performance advisor bot at iscalypsofastyet.com.

@nbloomf nbloomf force-pushed the add/wp-checkout-format-currency branch from a151518 to d88d0b2 Compare October 15, 2019 15:24
@nbloomf nbloomf requested a review from a team as a code owner October 15, 2019 15:24
@nbloomf nbloomf changed the base branch from add/wp-checkout-component to add/wp-checkout-component-test October 15, 2019 15:26
Copy link
Member

@sirbrillig sirbrillig left a comment

Choose a reason for hiding this comment

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

This looks amazing. I think we can actually make this into its own package and then depend on it.

@sirbrillig
Copy link
Member

I also just saw this, but I don't know much about it: https://github.com/Automattic/wp-calypso/tree/master/packages/format-currency

@nbloomf nbloomf force-pushed the add/wp-checkout-component-test branch from e1ddf84 to 55453d3 Compare October 18, 2019 16:56
@nbloomf nbloomf force-pushed the add/wp-checkout-format-currency branch from fc0b649 to 0d914c7 Compare October 21, 2019 21:05
@nbloomf nbloomf changed the base branch from add/wp-checkout-component-test to add/wp-checkout-component October 21, 2019 21:08
@sirbrillig
Copy link
Member

I think this is great. My inclination is to wait until #36720 is merged and then make this PR into its own package (maybe @automattic/format-amount-for-locale?) and add it to the dependencies of @automattic/composite-component.

@nbloomf
Copy link
Contributor Author

nbloomf commented Oct 21, 2019

That sounds good to me.

@sirbrillig sirbrillig changed the title wp-checkout: Add a function for formatting currency amounts composite-checkout: Add a function for formatting currency amounts Oct 21, 2019
@nbloomf nbloomf changed the base branch from add/wp-checkout-component to master October 22, 2019 17:06
@nbloomf nbloomf changed the base branch from master to add/wp-checkout-component October 22, 2019 17:25
@nbloomf nbloomf force-pushed the add/wp-checkout-format-currency branch from b5b15d2 to 651fefb Compare October 22, 2019 18:13
@nbloomf nbloomf requested a review from a team as a code owner October 22, 2019 18:13
@nbloomf nbloomf changed the base branch from add/wp-checkout-component to master October 22, 2019 18:14
This library was written for the in-progress composite-checkout package but has no external dependencies, so we're extracting it as its own package. Features: exact arithmetic and non-breaking spaces
@nbloomf nbloomf force-pushed the add/wp-checkout-format-currency branch from 651fefb to ac5b5a8 Compare November 7, 2019 17:26
@nbloomf nbloomf changed the title composite-checkout: Add a function for formatting currency amounts localize-currency-amount: Utility for locale-aware currency formatting Nov 7, 2019
@nbloomf nbloomf requested a review from sirreal November 7, 2019 17:30
@sirreal sirreal requested review from a team and removed request for sirreal November 7, 2019 17:37
Copy link
Member

@sirreal sirreal left a comment

Choose a reason for hiding this comment

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

This is some impressive work, well done 💯

I did my best to provide a thorough review. I learned a lot in the process 🙂

A few high level notes:

  • We should copy .eslintrc from a sibling package. That's required for one of our package dependency lint rules.
  • We need to get the build set up and I'd like to use the tsc compiler to output types.
  • Let's remove types from jsdoc, they're redundant.
  • We need to check browser compatibility. If we're targetting es5, we should use es5 or make sure that code is properly transpiled. I don't know whether TypeScript handles that.
  • The structure and comments are impressive and make this pretty easy to follow even when it's completely new material. Well done 👍

packages/localize-monetary-amount/src/currency-code.ts Outdated Show resolved Hide resolved
packages/localize-monetary-amount/src/currency-code.ts Outdated Show resolved Hide resolved
packages/localize-monetary-amount/src/currency-code.ts Outdated Show resolved Hide resolved
packages/localize-monetary-amount/src/currency-code.ts Outdated Show resolved Hide resolved
currencyCode: CurrencyCode,
absoluteAmount: number
): { integerPart: string[]; fractionalPart: string } {
if ( ! ( Number.isInteger( absoluteAmount ) && absoluteAmount >= 0 ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Flagging Number.isInteger again (see my other comment)


return groupDigitsAccum(
remaining,
[ nextGroup.toString().padStart( numDigitsPerGroup, '0' ) ].concat( localDigits ),
Copy link
Member

Choose a reason for hiding this comment

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

String.prototype.padStart will be problematic in terms of support. There are several occurrences.

@jblz
Copy link
Member

jblz commented Nov 8, 2019

I just did a cursory scan of this, so please forgive my lack of context.

What's the driver behind putting this in the client? Won't monetary data be coming from the API? What's preventing us from formatting it before we send it to the client (e.g. with our CLDR functionality)?

@nbloomf
Copy link
Contributor Author

nbloomf commented Nov 8, 2019

Thanks for taking a look @jblz!

What's preventing us from formatting it before we send it to the client?

Nothing, and WPCOM does this already. But this package is an offshoot of composite-checkout, which will be used on other services. That package requires its host client to provide its own formatted currency strings, and this package allows them to do so without changing their own API if needed.

@sirreal
Copy link
Member

sirreal commented Mar 10, 2020

Curious if this is still planned to move forward. There appears to be a lot of valuable work here!

@sirbrillig
Copy link
Member

I think it would be great if this work could be shared. Maybe we could merge and publish this package, regardless of our current use of it? Or it could be published outside of the calypso monorepo.

@sirreal
Copy link
Member

sirreal commented Mar 11, 2020

With #39786 merged, this will likely require a few build-related changes, but shouldn't be too much work.

Just getting this merged would be a great first step 😁

@sirbrillig
Copy link
Member

@nbloomf What do you think?

@sarayourfriend sarayourfriend changed the base branch from master to trunk November 20, 2020 16:14
@matticbot matticbot added the [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. label Nov 20, 2020
@github-actions
Copy link

github-actions bot commented May 5, 2021

This PR has been marked as stale due to lack of activity within the last 30 days.

@sirbrillig
Copy link
Member

I still think this would be great to have. We have a bunch of places in calypso where we're forced to get a lot of extra data from the server to avoid client-side math and this would resolve that need.

@github-actions
Copy link

github-actions bot commented Jul 5, 2021

This PR has been marked as stale due to lack of activity within the last 30 days.

@sirbrillig
Copy link
Member

I still like it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
i18n Packages [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. [Type] Enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants