/
settingsCurrency.js
61 lines (59 loc) · 1.37 KB
/
settingsCurrency.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// See: https://en.wikipedia.org/wiki/ISO_4217
// See: https://stripe.com/docs/currencies
export const subUnitDivisors = {
AUD: 100,
BGN: 100,
CAD: 100,
CHF: 100,
CNY: 100,
CZK: 100,
DKK: 100,
EUR: 100,
GBP: 100,
HKD: 100,
INR: 100,
JPY: 1,
MXN: 100,
NOK: 100,
NZD: 100,
PLN: 100,
RON: 100,
SEK: 100,
SGD: 100,
USD: 100,
};
/**
* Currency formatting options.
* See: https://github.com/yahoo/react-intl/wiki/API#formatnumber
*
* @param {string} currency
*/
export const currencyFormatting = (currency, options) => {
const { enforceSupportedCurrencies = true } = options || {};
if (enforceSupportedCurrencies && !subUnitDivisors[currency]) {
const currencies = Object.keys(subUnitDivisors);
throw new Error(
`Configuration missing for currency: ${currency}. Supported currencies: ${currencies.join(
', '
)}.`
);
}
return subUnitDivisors[currency] === 1
? {
style: 'currency',
currency,
currencyDisplay: 'symbol',
useGrouping: true,
// If the currency is not using subunits (like JPY), remove fractions.
minimumFractionDigits: 0,
maximumFractionDigits: 0,
}
: {
style: 'currency',
currency,
currencyDisplay: 'symbol',
useGrouping: true,
minimumFractionDigits: 2,
maximumFractionDigits: 2,
};
};