UNPKG

3.32 kBJavaScriptView Raw
1import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
2import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
3import _objectSpread from "@babel/runtime/helpers/objectSpread";
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 */
21var 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};
38var IntlPatches = {
39 formatToPartsPatch: function formatToPartsPatch(_ref, num) {
40 var currency = _ref.currency,
41 maximumFractionDigits = _ref.maximumFractionDigits,
42 useGrouping = _ref.useGrouping;
43
44 var format = intlFormats[currency] || _objectSpread({}, intlFormats.USD, {
45 symbol: currency
46 });
47
48 var symbol = format.symbol,
49 decimal = format.decimal,
50 groupDelim = format.groupDelim;
51 var parts = [{
52 type: 'currency',
53 value: symbol
54 }];
55
56 var _num$toFixed$match = num.toFixed(maximumFractionDigits).match(/\d+/g),
57 _num$toFixed$match2 = _slicedToArray(_num$toFixed$match, 2),
58 integer = _num$toFixed$match2[0],
59 fraction = _num$toFixed$match2[1];
60
61 if (useGrouping !== false) {
62 var intParts = [];
63 var firstGroupLength = integer.length % 3;
64 var integerSlice = integer;
65
66 if (firstGroupLength > 0) {
67 intParts.push(JSON.stringify({
68 type: 'integer',
69 value: integer.slice(0, firstGroupLength)
70 }));
71 integerSlice = integer.slice(firstGroupLength);
72 }
73
74 var groups = integerSlice.match(/\d{3}/g);
75
76 if (groups) {
77 intParts.push.apply(intParts, _toConsumableArray(groups.map(function (intPart) {
78 return JSON.stringify({
79 type: 'integer',
80 value: intPart
81 });
82 })));
83 }
84
85 var groupDelimJSON = ',' + JSON.stringify({
86 type: 'group',
87 value: groupDelim
88 }) + ',';
89 var intAndGroupParts = JSON.parse("[".concat(intParts.join(groupDelimJSON), "]"));
90 parts.push.apply(parts, _toConsumableArray(intAndGroupParts));
91 } else {
92 parts.push({
93 type: 'integer',
94 value: integer
95 });
96 }
97
98 return parts.concat([{
99 type: 'decimal',
100 value: decimal
101 }, {
102 type: 'fraction',
103 value: fraction
104 }]);
105 },
106 toParts: function toParts(num) {
107 return this.formatToParts ? this.formatToParts(num) : IntlPatches.formatToPartsPatch(this.resolvedOptions(), num);
108 }
109};
110export default IntlPatches;
111//# sourceMappingURL=intlPatches.js.map
\No newline at end of file