intl.js
9.24 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
export var NumberFormatStyle = {};
NumberFormatStyle.Decimal = 0;
NumberFormatStyle.Percent = 1;
NumberFormatStyle.Currency = 2;
NumberFormatStyle[NumberFormatStyle.Decimal] = "Decimal";
NumberFormatStyle[NumberFormatStyle.Percent] = "Percent";
NumberFormatStyle[NumberFormatStyle.Currency] = "Currency";
export var NumberFormatter = (function () {
function NumberFormatter() {
}
/**
* @param {?} num
* @param {?} locale
* @param {?} style
* @param {?=} __3
* @return {?}
*/
NumberFormatter.format = function (num, locale, style, _a) {
var _b = _a === void 0 ? {} : _a, minimumIntegerDigits = _b.minimumIntegerDigits, minimumFractionDigits = _b.minimumFractionDigits, maximumFractionDigits = _b.maximumFractionDigits, currency = _b.currency, _c = _b.currencyAsSymbol, currencyAsSymbol = _c === void 0 ? false : _c;
var /** @type {?} */ options = {
minimumIntegerDigits: minimumIntegerDigits,
minimumFractionDigits: minimumFractionDigits,
maximumFractionDigits: maximumFractionDigits,
style: NumberFormatStyle[style].toLowerCase()
};
if (style == NumberFormatStyle.Currency) {
options.currency = currency;
options.currencyDisplay = currencyAsSymbol ? 'symbol' : 'code';
}
return new Intl.NumberFormat(locale, options).format(num);
};
return NumberFormatter;
}());
var /** @type {?} */ DATE_FORMATS_SPLIT = /((?:[^yMLdHhmsazZEwGjJ']+)|(?:'(?:[^']|'')*')|(?:E+|y+|M+|L+|d+|H+|h+|J+|j+|m+|s+|a|z|Z|G+|w+))(.*)/;
var /** @type {?} */ PATTERN_ALIASES = {
// Keys are quoted so they do not get renamed during closure compilation.
'yMMMdjms': datePartGetterFactory(combine([
digitCondition('year', 1),
nameCondition('month', 3),
digitCondition('day', 1),
digitCondition('hour', 1),
digitCondition('minute', 1),
digitCondition('second', 1),
])),
'yMdjm': datePartGetterFactory(combine([
digitCondition('year', 1), digitCondition('month', 1), digitCondition('day', 1),
digitCondition('hour', 1), digitCondition('minute', 1)
])),
'yMMMMEEEEd': datePartGetterFactory(combine([
digitCondition('year', 1), nameCondition('month', 4), nameCondition('weekday', 4),
digitCondition('day', 1)
])),
'yMMMMd': datePartGetterFactory(combine([digitCondition('year', 1), nameCondition('month', 4), digitCondition('day', 1)])),
'yMMMd': datePartGetterFactory(combine([digitCondition('year', 1), nameCondition('month', 3), digitCondition('day', 1)])),
'yMd': datePartGetterFactory(combine([digitCondition('year', 1), digitCondition('month', 1), digitCondition('day', 1)])),
'jms': datePartGetterFactory(combine([digitCondition('hour', 1), digitCondition('second', 1), digitCondition('minute', 1)])),
'jm': datePartGetterFactory(combine([digitCondition('hour', 1), digitCondition('minute', 1)]))
};
var /** @type {?} */ DATE_FORMATS = {
// Keys are quoted so they do not get renamed.
'yyyy': datePartGetterFactory(digitCondition('year', 4)),
'yy': datePartGetterFactory(digitCondition('year', 2)),
'y': datePartGetterFactory(digitCondition('year', 1)),
'MMMM': datePartGetterFactory(nameCondition('month', 4)),
'MMM': datePartGetterFactory(nameCondition('month', 3)),
'MM': datePartGetterFactory(digitCondition('month', 2)),
'M': datePartGetterFactory(digitCondition('month', 1)),
'LLLL': datePartGetterFactory(nameCondition('month', 4)),
'L': datePartGetterFactory(nameCondition('month', 1)),
'dd': datePartGetterFactory(digitCondition('day', 2)),
'd': datePartGetterFactory(digitCondition('day', 1)),
'HH': digitModifier(hourExtractor(datePartGetterFactory(hour12Modify(digitCondition('hour', 2), false)))),
'H': hourExtractor(datePartGetterFactory(hour12Modify(digitCondition('hour', 1), false))),
'hh': digitModifier(hourExtractor(datePartGetterFactory(hour12Modify(digitCondition('hour', 2), true)))),
'h': hourExtractor(datePartGetterFactory(hour12Modify(digitCondition('hour', 1), true))),
'jj': datePartGetterFactory(digitCondition('hour', 2)),
'j': datePartGetterFactory(digitCondition('hour', 1)),
'mm': digitModifier(datePartGetterFactory(digitCondition('minute', 2))),
'm': datePartGetterFactory(digitCondition('minute', 1)),
'ss': digitModifier(datePartGetterFactory(digitCondition('second', 2))),
's': datePartGetterFactory(digitCondition('second', 1)),
// while ISO 8601 requires fractions to be prefixed with `.` or `,`
// we can be just safely rely on using `sss` since we currently don't support single or two digit
// fractions
'sss': datePartGetterFactory(digitCondition('second', 3)),
'EEEE': datePartGetterFactory(nameCondition('weekday', 4)),
'EEE': datePartGetterFactory(nameCondition('weekday', 3)),
'EE': datePartGetterFactory(nameCondition('weekday', 2)),
'E': datePartGetterFactory(nameCondition('weekday', 1)),
'a': hourClockExtractor(datePartGetterFactory(hour12Modify(digitCondition('hour', 1), true))),
'Z': timeZoneGetter('short'),
'z': timeZoneGetter('long'),
'ww': datePartGetterFactory({}),
// first Thursday of the year. not support ?
'w': datePartGetterFactory({}),
// of the year not support ?
'G': datePartGetterFactory(nameCondition('era', 1)),
'GG': datePartGetterFactory(nameCondition('era', 2)),
'GGG': datePartGetterFactory(nameCondition('era', 3)),
'GGGG': datePartGetterFactory(nameCondition('era', 4))
};
/**
* @param {?} inner
* @return {?}
*/
function digitModifier(inner) {
return function (date, locale) {
var /** @type {?} */ result = inner(date, locale);
return result.length == 1 ? '0' + result : result;
};
}
/**
* @param {?} inner
* @return {?}
*/
function hourClockExtractor(inner) {
return function (date, locale) { return inner(date, locale).split(' ')[1]; };
}
/**
* @param {?} inner
* @return {?}
*/
function hourExtractor(inner) {
return function (date, locale) { return inner(date, locale).split(' ')[0]; };
}
/**
* @param {?} date
* @param {?} locale
* @param {?} options
* @return {?}
*/
function intlDateFormat(date, locale, options) {
return new Intl.DateTimeFormat(locale, options).format(date).replace(/[\u200e\u200f]/g, '');
}
/**
* @param {?} timezone
* @return {?}
*/
function timeZoneGetter(timezone) {
// To workaround `Intl` API restriction for single timezone let format with 24 hours
var /** @type {?} */ options = { hour: '2-digit', hour12: false, timeZoneName: timezone };
return function (date, locale) {
var /** @type {?} */ result = intlDateFormat(date, locale, options);
// Then extract first 3 letters that related to hours
return result ? result.substring(3) : '';
};
}
/**
* @param {?} options
* @param {?} value
* @return {?}
*/
function hour12Modify(options, value) {
options.hour12 = value;
return options;
}
/**
* @param {?} prop
* @param {?} len
* @return {?}
*/
function digitCondition(prop, len) {
var /** @type {?} */ result = {};
result[prop] = len === 2 ? '2-digit' : 'numeric';
return result;
}
/**
* @param {?} prop
* @param {?} len
* @return {?}
*/
function nameCondition(prop, len) {
var /** @type {?} */ result = {};
if (len < 4) {
result[prop] = len > 1 ? 'short' : 'narrow';
}
else {
result[prop] = 'long';
}
return result;
}
/**
* @param {?} options
* @return {?}
*/
function combine(options) {
return (_a = ((Object))).assign.apply(_a, [{}].concat(options));
var _a;
}
/**
* @param {?} ret
* @return {?}
*/
function datePartGetterFactory(ret) {
return function (date, locale) { return intlDateFormat(date, locale, ret); };
}
var /** @type {?} */ DATE_FORMATTER_CACHE = new Map();
/**
* @param {?} format
* @param {?} date
* @param {?} locale
* @return {?}
*/
function dateFormatter(format, date, locale) {
var /** @type {?} */ fn = PATTERN_ALIASES[format];
if (fn)
return fn(date, locale);
var /** @type {?} */ cacheKey = format;
var /** @type {?} */ parts = DATE_FORMATTER_CACHE.get(cacheKey);
if (!parts) {
parts = [];
var /** @type {?} */ match = void 0;
DATE_FORMATS_SPLIT.exec(format);
while (format) {
match = DATE_FORMATS_SPLIT.exec(format);
if (match) {
parts = parts.concat(match.slice(1));
format = parts.pop();
}
else {
parts.push(format);
format = null;
}
}
DATE_FORMATTER_CACHE.set(cacheKey, parts);
}
return parts.reduce(function (text, part) {
var /** @type {?} */ fn = DATE_FORMATS[part];
return text + (fn ? fn(date, locale) : partToTime(part));
}, '');
}
/**
* @param {?} part
* @return {?}
*/
function partToTime(part) {
return part === '\'\'' ? '\'' : part.replace(/(^'|'$)/g, '').replace(/''/g, '\'');
}
export var DateFormatter = (function () {
function DateFormatter() {
}
/**
* @param {?} date
* @param {?} locale
* @param {?} pattern
* @return {?}
*/
DateFormatter.format = function (date, locale, pattern) {
return dateFormatter(pattern, date, locale);
};
return DateFormatter;
}());
//# sourceMappingURL=intl.js.map