1 | function _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 |
|
3 | function _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 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | const 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 | };
|
38 | const 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 | };
|
108 | export default IntlPatches;
|
109 |
|
\ | No newline at end of file |