1 | "use strict";
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports.NumberLocalizer = exports.DateLocalizer = void 0;
|
5 |
|
6 | var _dates = _interopRequireDefault(require("./dates"));
|
7 |
|
8 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
9 |
|
10 |
|
11 | let supportStyles = false;
|
12 | new Intl.DateTimeFormat(undefined, {
|
13 |
|
14 | get dateStyle() {
|
15 | supportStyles = true;
|
16 | }
|
17 |
|
18 | });
|
19 | const dateShort = {
|
20 | day: 'numeric',
|
21 | month: 'numeric',
|
22 | year: 'numeric'
|
23 | };
|
24 | const timeShort = {
|
25 | hour: 'numeric',
|
26 | minute: 'numeric'
|
27 | };
|
28 |
|
29 | const getFormatter = (culture, options) => Intl.DateTimeFormat(culture, options).format;
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | class IntlDateLocalizer {
|
37 | constructor({
|
38 | culture = undefined,
|
39 | firstOfWeek = 0
|
40 | } = {}) {
|
41 | this.culture = culture;
|
42 |
|
43 | this.firstOfWeek = () => firstOfWeek;
|
44 |
|
45 | function normalizeFormat(date, format) {
|
46 | return typeof format === 'function' ? format(date, culture) : date.toLocaleString(culture, format);
|
47 | }
|
48 |
|
49 | const formats = {
|
50 | date: getFormatter(culture,
|
51 | supportStyles ? {
|
52 | dateStyle: 'short'
|
53 | } : dateShort),
|
54 | time: getFormatter(culture,
|
55 | supportStyles ? {
|
56 | timeStyle: 'short'
|
57 | } : timeShort),
|
58 | datetime: getFormatter(culture,
|
59 | supportStyles ? {
|
60 | dateStyle: 'short',
|
61 | timeStyle: 'short'
|
62 | } : Object.assign({}, dateShort, timeShort)),
|
63 | header: getFormatter(culture, {
|
64 | month: 'short',
|
65 | year: 'numeric'
|
66 | }),
|
67 | weekday: getFormatter(culture, {
|
68 | weekday: 'narrow'
|
69 | }),
|
70 | dayOfMonth: getFormatter(culture, {
|
71 | day: '2-digit'
|
72 | }),
|
73 | month: getFormatter(culture, {
|
74 | month: 'short'
|
75 | }),
|
76 | year: getFormatter(culture, {
|
77 | year: 'numeric'
|
78 | }),
|
79 | decade: date => `${this.year(date)} - ${this.year(_dates.default.endOf(date, 'decade'))}`,
|
80 | century: date => `${this.year(date)} - ${this.year(_dates.default.endOf(date, 'century'))}`
|
81 | };
|
82 | Object.keys(formats).forEach(key => {
|
83 | this[key] = (date, format) => format ? normalizeFormat(date, format) : formats[key](date);
|
84 | });
|
85 | }
|
86 |
|
87 | toFormattedParts(date, format = {
|
88 | dateStyle: 'short',
|
89 | timeStyle: 'short'
|
90 | }) {
|
91 | return Intl.DateTimeFormat(this.culture, format).formatToParts(date).filter(p => p.type !== 'timeZoneName');
|
92 | }
|
93 |
|
94 | parse(value) {
|
95 | const date = new Date(value);
|
96 | return isNaN(+date) ? null : date;
|
97 | }
|
98 |
|
99 | }
|
100 |
|
101 | exports.DateLocalizer = IntlDateLocalizer;
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | class IntlNumberLocalizer {
|
107 | constructor({
|
108 | culture = undefined
|
109 | } = {}) {
|
110 | var _$toLocaleString$m;
|
111 |
|
112 | this.culture = culture;
|
113 | const decimal = 'formatToParts' in Intl.NumberFormat(culture) ? Intl.NumberFormat(culture).formatToParts(1.1)[1].value : ((_$toLocaleString$m = 1.1.toLocaleString(culture).match(/[^\d]/)) == null ? void 0 : _$toLocaleString$m[0]) || '.';
|
114 | const formatter = Intl.NumberFormat(culture).format;
|
115 |
|
116 | this.decimalCharacter = () => decimal;
|
117 |
|
118 | this.format = (num, format) => {
|
119 | if (format) {
|
120 | return typeof format === 'function' ? format(num, culture) : num.toLocaleString(culture, format);
|
121 | }
|
122 |
|
123 | return formatter(num);
|
124 | };
|
125 | }
|
126 |
|
127 | parse(value) {
|
128 | return parseFloat(value.replace(this.decimalCharacter(), '.'));
|
129 | }
|
130 |
|
131 | }
|
132 |
|
133 | exports.NumberLocalizer = IntlNumberLocalizer; |
\ | No newline at end of file |