mirror of
https://github.com/overleaf/overleaf.git
synced 2025-02-27 05:40:56 +00:00
[web] Handle error cause by currencyDisplay: 'narrowSymbol'
in old browsers (#18060)
* Handle error cause by `currencyDisplay: 'narrowSymbol'` in old browsers RangeError Value narrowSymbol out of range for Number.prototype.toLocaleString options property currencyDisplay * Make `formatCurrencyLocalized` bulletproof GitOrigin-RevId: 26e8abc6f9fb7c06c2d14b9d86af2d84fb9f32e3
This commit is contained in:
parent
65313e6692
commit
e32b4f0db1
2 changed files with 26 additions and 18 deletions
|
@ -14,19 +14,23 @@
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
function formatCurrencyLocalized(amount, currency, locale, stripIfInteger) {
|
function formatCurrencyLocalized(amount, currency, locale, stripIfInteger) {
|
||||||
|
const options = { style: 'currency', currency }
|
||||||
if (stripIfInteger && Number.isInteger(amount)) {
|
if (stripIfInteger && Number.isInteger(amount)) {
|
||||||
return amount.toLocaleString(locale, {
|
options.minimumFractionDigits = 0
|
||||||
style: 'currency',
|
|
||||||
currency,
|
|
||||||
minimumFractionDigits: 0,
|
|
||||||
currencyDisplay: 'narrowSymbol',
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
return amount.toLocaleString(locale, {
|
return amount.toLocaleString(locale, {
|
||||||
style: 'currency',
|
...options,
|
||||||
currency,
|
|
||||||
currencyDisplay: 'narrowSymbol',
|
currencyDisplay: 'narrowSymbol',
|
||||||
})
|
})
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return amount.toLocaleString(locale, options)
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
return `${currency} ${amount}`
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
|
@ -23,17 +23,21 @@ export function formatCurrencyLocalized(
|
||||||
locale: string,
|
locale: string,
|
||||||
stripIfInteger = false
|
stripIfInteger = false
|
||||||
): string {
|
): string {
|
||||||
|
const options: Intl.NumberFormatOptions = { style: 'currency', currency }
|
||||||
if (stripIfInteger && Number.isInteger(amount)) {
|
if (stripIfInteger && Number.isInteger(amount)) {
|
||||||
return amount.toLocaleString(locale, {
|
options.minimumFractionDigits = 0
|
||||||
style: 'currency',
|
|
||||||
currency,
|
|
||||||
minimumFractionDigits: 0,
|
|
||||||
currencyDisplay: 'narrowSymbol',
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
return amount.toLocaleString(locale, {
|
return amount.toLocaleString(locale, {
|
||||||
style: 'currency',
|
...options,
|
||||||
currency,
|
|
||||||
currencyDisplay: 'narrowSymbol',
|
currencyDisplay: 'narrowSymbol',
|
||||||
})
|
})
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return amount.toLocaleString(locale, options)
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
return `${currency} ${amount}`
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue