-
Notifications
You must be signed in to change notification settings - Fork 2k
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
base: trunk
Are you sure you want to change the base?
Conversation
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. |
a151518
to
d88d0b2
Compare
There was a problem hiding this 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.
packages/wp-checkout/src/lib/format-monetary-amount-for-locale.js
Outdated
Show resolved
Hide resolved
packages/wp-checkout/src/lib/format-monetary-amount-for-locale.js
Outdated
Show resolved
Hide resolved
packages/wp-checkout/src/lib/format-monetary-amount-for-locale.js
Outdated
Show resolved
Hide resolved
I also just saw this, but I don't know much about it: https://github.com/Automattic/wp-calypso/tree/master/packages/format-currency |
e1ddf84
to
55453d3
Compare
fc0b649
to
0d914c7
Compare
I think this is great. My inclination is to wait until #36720 is merged and then make this PR into its own package (maybe |
That sounds good to me. |
b5b15d2
to
651fefb
Compare
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
651fefb
to
ac5b5a8
Compare
packages/localize-monetary-amount/src/localize-monetary-amount.ts
Outdated
Show resolved
Hide resolved
packages/localize-monetary-amount/src/localize-monetary-amount.ts
Outdated
Show resolved
Hide resolved
packages/localize-monetary-amount/src/localize-monetary-amount.ts
Outdated
Show resolved
Hide resolved
packages/localize-monetary-amount/src/localize-monetary-amount.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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/localize-monetary-amount.ts
Outdated
Show resolved
Hide resolved
packages/localize-monetary-amount/src/localize-monetary-amount.ts
Outdated
Show resolved
Hide resolved
currencyCode: CurrencyCode, | ||
absoluteAmount: number | ||
): { integerPart: string[]; fractionalPart: string } { | ||
if ( ! ( Number.isInteger( absoluteAmount ) && absoluteAmount >= 0 ) ) { |
There was a problem hiding this comment.
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 ), |
There was a problem hiding this comment.
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.
packages/localize-monetary-amount/test/localize-monetary-amount.js
Outdated
Show resolved
Hide resolved
packages/localize-monetary-amount/src/localize-monetary-amount.ts
Outdated
Show resolved
Hide resolved
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)? |
Thanks for taking a look @jblz!
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. |
Curious if this is still planned to move forward. There appears to be a lot of valuable work here! |
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. |
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 😁 |
@nbloomf What do you think? |
This PR has been marked as stale due to lack of activity within the last 30 days. |
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. |
This PR has been marked as stale due to lack of activity within the last 30 days. |
I still like it |
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
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