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

fix: use currencyDisplay: 'symbol' #22

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

Conversation

bashunaimiroy
Copy link
Contributor

@bashunaimiroy bashunaimiroy commented Nov 26, 2024

This ensures that we match the twig money filter output. For clarity about how this functions:

When the currency and locale match, we don't render the country abbreviation. E.g. 100 Canadian Dollars in en-US locale will be shown as CA$100 , but in en-CA it'll be rendered just $100.

@bashunaimiroy bashunaimiroy requested a review from a team as a code owner November 26, 2024 22:55
Copy link

github-actions bot commented Nov 26, 2024

size-limit report 📦

Path Size
lib/index.js 6.35 KB (+0.83% 🔺)
lib/index.umd.cjs 6.5 KB (+0.79% 🔺)

Copy link

github-actions bot commented Nov 26, 2024

📊 Package size report   0.6%↑

File Before After
lib/helpers/money.d.ts 1.5 kB 0.9%↑1.5 kB
lib/index.js 39.4 kB 1%↑39.9 kB
lib/index.umd.cjs 19.4 kB 0.8%↑19.5 kB
Total (Includes all files) 124.0 kB 0.6%↑124.7 kB
Tarball size 31.8 kB 0.7%↑32.0 kB
Unchanged files
File Size
lib/api/cart.d.ts 6.2 kB
lib/api/customers.d.ts 1.3 kB
lib/api/places.d.ts 1.3 kB
lib/api/resources.d.ts 1.3 kB
lib/api/template.d.ts 686 B
lib/clients/buyers.d.ts 236 B
lib/helpers/auth.d.ts 239 B
lib/helpers/cookies.d.ts 71 B
lib/helpers/datetime.d.ts 4.8 kB
lib/helpers/item.d.ts 3.7 kB
lib/helpers/location.d.ts 3.9 kB
lib/index.d.ts 875 B
lib/types/api/cart/index.d.ts 14.9 kB
lib/types/api/cart/private.types.d.ts 829 B
lib/types/api/customers/index.d.ts 427 B
lib/types/api/places/index.d.ts 1.9 kB
lib/types/api/resources/index.d.ts 4.2 kB
lib/types/api/resources/private.types.d.ts 60 B
lib/types/api/template/index.d.ts 808 B
lib/types/clients/buyers.private.d.ts 365 B
lib/types/helpers/datetime/index.d.ts 3.9 kB
lib/types/helpers/item/index.d.ts 3.2 kB
lib/types/helpers/location/index.d.ts 3.6 kB
lib/types/helpers/money/index.d.ts 92 B
lib/types/looseobject.d.ts 78 B
LICENSE 1.1 kB
package.json 2.2 kB
README.md 1.6 kB

🤖 This report was automatically generated by pkg-size-action

@bashunaimiroy bashunaimiroy changed the title use currencyDisplay: 'symbol' fix: use currencyDisplay: 'symbol' Nov 26, 2024
Copy link
Contributor

@joe-boudreau joe-boudreau left a comment

Choose a reason for hiding this comment

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

Will this display the currency symbol always? Like on a US Site in USD with a buyer with en_US locale, it will show US $100 ?
Or will it only show it if the locale and currency don't match

Copy link
Collaborator

@kevinlee11 kevinlee11 left a comment

Choose a reason for hiding this comment

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

Prob worth adding new unit tests to cover this addition!

@bashunaimiroy
Copy link
Contributor Author

@joe-boudreau yes, it will only show it it doesn't match, like Canadian Dollars in en-US locale will be shown as CA$.

Some interesting edge cases:

  • Singapore uses English and Singaporean Dollars. CAD and USD in the en-SG locale will be shown with CA$ and US$ respectively, which makes sense.
  • But several countries use Pounds, and GBP in en-US will just show the £ symbol, but another country that uses the pound will show UK£.

Anyways, see the tests for more details

@bashunaimiroy
Copy link
Contributor Author

@kevinlee11 I've also updated it to cache the NumberFormat objects; otherwise we create a new one every time, which is a performance hit. This was called out by @rgershon , thanks Gershon

* The key is a string representing the locale and currency,
* and the value is the corresponding `Intl.NumberFormat` instance.
*/
#cachedFormatters: { [key: string]: Intl.NumberFormat } = {};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was suggested by @rgershon

Copy link
Contributor

Choose a reason for hiding this comment

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

why? just curious what's going on behind the scenes here

Copy link
Contributor

Choose a reason for hiding this comment

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

Is this something we're noticing initializing Intl.NumberFormat as being a heavy operation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so; But we're doing it a lot, and we're just trying to make Lightning Themes as performant as possible and this seemed like low hanging fruit

];

currencyAndLocaleList.forEach(({ money, locale }) => {
expect(sdk.helpers.money.formatMoney(money, locale)).toBe(money.formatted);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The output of Intl.NumberFormat.format() is actually not guaranteed by the Javascript spec, and specifically MDN says we should not compare to a hardcoded string.

However, I think this is fine in a test case; if it starts failing, that would be good to know

@bashunaimiroy
Copy link
Contributor Author

@kchung rebased and also marked this as a breaking change, since it changes the text output of a helper

Copy link
Contributor

@kchung kchung left a comment

Choose a reason for hiding this comment

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

Thanks!

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.

6 participants