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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions src/helpers/money.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ const getCurrencyFractionLength = (currency: string) => {
};

export class Money {

/**
* A private property that stores cached instances of `Intl.NumberFormat`
* for different locale and currency combinations.
* 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

/**
* Formats the Money object based on the provided locale.
*
Expand All @@ -33,18 +41,26 @@ export class Money {
* @returns The formatted amount.
*/
formatAmount(amount: number, currency: string, formattedLocale = 'en-US'): string {
let formatter;
try {
formatter = new Intl.NumberFormat(formattedLocale, {
style: 'currency',
currency: currency
});
} catch (err) {
// Fallback to en-US if it's an invalid BCP 47 locale
formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: currency
});
// use the cached formatter if present, to avoid creating a new formatter every time
const cacheKey = `${currency}:${formattedLocale}`;
let formatter = this.#cachedFormatters[cacheKey];
if (!formatter) {
// create a new formatter for this currency + locale
try {
formatter = new Intl.NumberFormat(formattedLocale, {
style: 'currency',
currencyDisplay: 'symbol',
currency: currency
});
} catch (err) {
// Fallback to en-US if it's an invalid BCP 47 locale
formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currencyDisplay: 'symbol',
currency: currency
});
}
this.#cachedFormatters[cacheKey] = formatter;
}
amount = this.convertSubunitsToFloat(amount, currency);
return formatter.format(amount);
Expand Down
84 changes: 84 additions & 0 deletions test/helpers.money.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,88 @@ describe('convertSubunitsToFloat', () => {
expect(sdk.helpers.money.convertSubunitsToFloat(subunits, currency)).toBe(float);
});
});
});

describe('formatMoney', () => {
it('should not show the currency, when the currency matches locale', () => {
const currencyAndLocaleList = [
{
money: {
currency: 'CAD',
amount: 123456,
formatted: '$1,234.56'
},
locale: 'en-CA',
},
{
money: {
currency: 'GBP',
amount: 123456,
formatted: '£1,234.56'
},
locale: 'en-GB',
},
{
money: {
currency: 'USD',
amount: 123456,
formatted: '$1,234.56'
},
locale: 'en-US',
},
];

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

});
});

it('should show the currency, when the currency does not match locale', () => {
const currencyAndLocaleList = [
{
money: {
currency: 'CAD',
amount: 123456,
formatted: 'CA$1,234.56'
},
locale: 'en-US',
},
{
money: {
currency: 'USD',
amount: 123456,
formatted: 'US$1,234.56'
},
locale: 'en-CA',
},
];

currencyAndLocaleList.forEach(({ money, locale }) => {
expect(sdk.helpers.money.formatMoney(money, locale)).toBe(money.formatted);
});
});
it('should default to en-US, when the locale is invalid', () => {
const currencyAndLocaleList = [
{
money: {
currency: 'CAD',
amount: 123456,
formatted: 'CA$1,234.56'
},
locale: 'fake-FAKE',
},
{
money: {
currency: 'USD',
amount: 123456,
formatted: '$1,234.56'
},
locale: 'fake-FAKE',
},
];

currencyAndLocaleList.forEach(({ money, locale }) => {
expect(sdk.helpers.money.formatMoney(money, locale)).toBe(money.formatted);
});
});
});
Loading