mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-14 14:13:43 +01: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:
@@ -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)) {
|
||||||
|
options.minimumFractionDigits = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
return amount.toLocaleString(locale, {
|
return amount.toLocaleString(locale, {
|
||||||
style: 'currency',
|
...options,
|
||||||
currency,
|
|
||||||
minimumFractionDigits: 0,
|
|
||||||
currencyDisplay: 'narrowSymbol',
|
currencyDisplay: 'narrowSymbol',
|
||||||
})
|
})
|
||||||
}
|
} catch {}
|
||||||
return amount.toLocaleString(locale, {
|
|
||||||
style: 'currency',
|
try {
|
||||||
currency,
|
return amount.toLocaleString(locale, options)
|
||||||
currencyDisplay: 'narrowSymbol',
|
} 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)) {
|
||||||
|
options.minimumFractionDigits = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
return amount.toLocaleString(locale, {
|
return amount.toLocaleString(locale, {
|
||||||
style: 'currency',
|
...options,
|
||||||
currency,
|
|
||||||
minimumFractionDigits: 0,
|
|
||||||
currencyDisplay: 'narrowSymbol',
|
currencyDisplay: 'narrowSymbol',
|
||||||
})
|
})
|
||||||
}
|
} catch {}
|
||||||
return amount.toLocaleString(locale, {
|
|
||||||
style: 'currency',
|
try {
|
||||||
currency,
|
return amount.toLocaleString(locale, options)
|
||||||
currencyDisplay: 'narrowSymbol',
|
} catch {}
|
||||||
})
|
|
||||||
|
return `${currency} ${amount}`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user