Skip to content

Commit

Permalink
fix(Currency): Add fallback for Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree committed Nov 26, 2021
1 parent bc16685 commit f693825
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions lib/currency-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,29 @@ export function formatCurrency(amount, currency = 'USD', options = {}) {
minimumFractionDigits = options.precision;
maximumFractionDigits = options.precision;
}
return amount.toLocaleString(options.locale, {
style: 'currency',
currency,
minimumFractionDigits: minimumFractionDigits,
maximumFractionDigits: maximumFractionDigits,
currencyDisplay: 'narrowSymbol', // We manually add the exact currency (e.g. "$10 USD") in many places. This is to prevent showing the currency twice is some locales (10$US USD)
});

const formatAmount = currencyDisplay => {
return amount.toLocaleString(options.locale, {
style: 'currency',
currency,
minimumFractionDigits: minimumFractionDigits,
maximumFractionDigits: maximumFractionDigits,
currencyDisplay,
});
};

try {
// We manually add the exact currency (e.g. "$10 USD") in many places. This is to prevent
// showing the currency twice is some locales ($US10 USD)
return formatAmount('narrowSymbol');
} catch (e) {
// ... unfortunately, some old versions of Safari doesn't support it, so we need a fallback
if (e instanceof RangeError) {
return formatAmount('symbol');
} else {
throw e;
}
}
}

export const formatValueAsCurrency = (value, options) =>
Expand Down

0 comments on commit f693825

Please sign in to comment.