UNPKG

3.4 kBJavaScriptView Raw
1function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
2
3function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
4
5/**
6 * A [ponyfill](https://github.com/sindresorhus/ponyfill) is an opt-in polyfill,
7 * which replaces missing builtins on older or non-compliant platforms, but
8 * without monkey-patching the native API.
9 *
10 * Many browsers do not yet support Intl.NumberFormat.prototype.formatToParts,
11 * which is tracked here: https://github.com/tc39/proposal-intl-formatToParts
12 *
13 * The polyfill available in that repository covers many edge cases, but it's
14 * more functionality than we need at this stage, it's not distributed over
15 * NPM, and it's 18kb. The below
16 * [ponyfill](https://github.com/sindresorhus/ponyfill) is not fully compliant,
17 * but it'll do for now.
18 *
19 * TODO: Replace with a formally maintained, but small-enough, ponyfill.
20 */
21const intlFormats = {
22 USD: {
23 symbol: '$',
24 decimal: '.',
25 groupDelim: ','
26 },
27 GBP: {
28 symbol: '£',
29 decimal: '.',
30 groupDelim: ','
31 },
32 EUR: {
33 symbol: '€',
34 decimal: '.',
35 groupDelim: ','
36 }
37};
38const IntlPatches = {
39 formatToPartsPatch({
40 currency,
41 maximumFractionDigits,
42 useGrouping
43 }, num) {
44 const format = intlFormats[currency] || _objectSpread({}, intlFormats.USD, {
45 symbol: currency
46 });
47
48 const {
49 symbol,
50 decimal,
51 groupDelim
52 } = format;
53 const parts = [{
54 type: 'currency',
55 value: symbol
56 }];
57 const [integer, fraction] = num.toFixed(maximumFractionDigits).match(/\d+/g);
58
59 if (useGrouping !== false) {
60 const intParts = [];
61 const firstGroupLength = integer.length % 3;
62 let integerSlice = integer;
63
64 if (firstGroupLength > 0) {
65 intParts.push(JSON.stringify({
66 type: 'integer',
67 value: integer.slice(0, firstGroupLength)
68 }));
69 integerSlice = integer.slice(firstGroupLength);
70 }
71
72 const groups = integerSlice.match(/\d{3}/g);
73
74 if (groups) {
75 intParts.push(...groups.map(intPart => JSON.stringify({
76 type: 'integer',
77 value: intPart
78 })));
79 }
80
81 const groupDelimJSON = ',' + JSON.stringify({
82 type: 'group',
83 value: groupDelim
84 }) + ',';
85 const intAndGroupParts = JSON.parse(`[${intParts.join(groupDelimJSON)}]`);
86 parts.push(...intAndGroupParts);
87 } else {
88 parts.push({
89 type: 'integer',
90 value: integer
91 });
92 }
93
94 return parts.concat([{
95 type: 'decimal',
96 value: decimal
97 }, {
98 type: 'fraction',
99 value: fraction
100 }]);
101 },
102
103 toParts(num) {
104 return this.formatToParts ? this.formatToParts(num) : IntlPatches.formatToPartsPatch(this.resolvedOptions(), num);
105 }
106
107};
108export default IntlPatches;
109//# sourceMappingURL=intlPatches.js.map
\No newline at end of file