-
Notifications
You must be signed in to change notification settings - Fork 1
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
HelpScout 1199960 - Fixing intlFormat.ts error when currency is not defined #987
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,12 +19,21 @@ export const currencyFormat = ( | |
): string => { | ||
const amount = Number.isNaN(value) ? 0 : value; | ||
const decimal = amount % 1 !== 0; | ||
return new Intl.NumberFormat(locale, { | ||
style: 'currency', | ||
currency: currency ?? 'USD', | ||
minimumFractionDigits: decimal ? 2 : 0, | ||
maximumFractionDigits: decimal ? 2 : 0, | ||
}).format(Number.isFinite(amount) ? amount : 0); | ||
if (!currency) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we get the same effect by giving it default argument of USD? I guess in that case we’d have to give locale a default as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is what we had before with |
||
currency = 'USD'; | ||
} | ||
try { | ||
return new Intl.NumberFormat(locale, { | ||
style: 'currency', | ||
currency: currency, | ||
minimumFractionDigits: decimal ? 2 : 0, | ||
maximumFractionDigits: decimal ? 2 : 0, | ||
}).format(Number.isFinite(amount) ? amount : 0); | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.error(`Error formatting currency: ${error}`); | ||
return `${amount} ${currency}`; | ||
} | ||
}; | ||
|
||
export const dayMonthFormat = ( | ||
|
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.
Do we know what happens if an absurd currency is given?
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.
If it is and it causes an error, we will revert to only returning the total amount with the currency in the text after it.
See line 36 on src/lib/intlFormat.ts