@formatjs/icu-skeleton-parser 1.3.10 → 1.3.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/BUILD +80 -0
- package/CHANGELOG.md +133 -0
- package/LICENSE.md +0 -0
- package/README.md +0 -0
- package/date-time.ts +144 -0
- package/index.ts +2 -0
- package/number.ts +357 -0
- package/package.json +3 -3
- package/regex.generated.ts +2 -0
- package/scripts/global.ts +1 -0
- package/scripts/regex-gen.ts +19 -0
- package/tests/__snapshots__/index.test.ts.snap +389 -0
- package/tests/index.test.ts +65 -0
- package/tsconfig.json +5 -0
- package/date-time.d.ts +0 -8
- package/date-time.d.ts.map +0 -1
- package/date-time.js +0 -125
- package/index.d.ts +0 -3
- package/index.d.ts.map +0 -1
- package/index.js +0 -5
- package/lib/date-time.d.ts +0 -8
- package/lib/date-time.d.ts.map +0 -1
- package/lib/date-time.js +0 -121
- package/lib/index.d.ts +0 -3
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -2
- package/lib/number.d.ts +0 -14
- package/lib/number.d.ts.map +0 -1
- package/lib/number.js +0 -295
- package/lib/regex.generated.d.ts +0 -2
- package/lib/regex.generated.d.ts.map +0 -1
- package/lib/regex.generated.js +0 -2
- package/number.d.ts +0 -14
- package/number.d.ts.map +0 -1
- package/number.js +0 -300
- package/regex.generated.d.ts +0 -2
- package/regex.generated.d.ts.map +0 -1
- package/regex.generated.js +0 -5
package/number.js
DELETED
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseNumberSkeleton = exports.parseNumberSkeletonFromString = void 0;
|
|
4
|
-
var tslib_1 = require("tslib");
|
|
5
|
-
var regex_generated_1 = require("./regex.generated");
|
|
6
|
-
function parseNumberSkeletonFromString(skeleton) {
|
|
7
|
-
if (skeleton.length === 0) {
|
|
8
|
-
throw new Error('Number skeleton cannot be empty');
|
|
9
|
-
}
|
|
10
|
-
// Parse the skeleton
|
|
11
|
-
var stringTokens = skeleton
|
|
12
|
-
.split(regex_generated_1.WHITE_SPACE_REGEX)
|
|
13
|
-
.filter(function (x) { return x.length > 0; });
|
|
14
|
-
var tokens = [];
|
|
15
|
-
for (var _i = 0, stringTokens_1 = stringTokens; _i < stringTokens_1.length; _i++) {
|
|
16
|
-
var stringToken = stringTokens_1[_i];
|
|
17
|
-
var stemAndOptions = stringToken.split('/');
|
|
18
|
-
if (stemAndOptions.length === 0) {
|
|
19
|
-
throw new Error('Invalid number skeleton');
|
|
20
|
-
}
|
|
21
|
-
var stem = stemAndOptions[0], options = stemAndOptions.slice(1);
|
|
22
|
-
for (var _a = 0, options_1 = options; _a < options_1.length; _a++) {
|
|
23
|
-
var option = options_1[_a];
|
|
24
|
-
if (option.length === 0) {
|
|
25
|
-
throw new Error('Invalid number skeleton');
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
tokens.push({ stem: stem, options: options });
|
|
29
|
-
}
|
|
30
|
-
return tokens;
|
|
31
|
-
}
|
|
32
|
-
exports.parseNumberSkeletonFromString = parseNumberSkeletonFromString;
|
|
33
|
-
function icuUnitToEcma(unit) {
|
|
34
|
-
return unit.replace(/^(.*?)-/, '');
|
|
35
|
-
}
|
|
36
|
-
var FRACTION_PRECISION_REGEX = /^\.(?:(0+)(\*)?|(#+)|(0+)(#+))$/g;
|
|
37
|
-
var SIGNIFICANT_PRECISION_REGEX = /^(@+)?(\+|#+)?[rs]?$/g;
|
|
38
|
-
var INTEGER_WIDTH_REGEX = /(\*)(0+)|(#+)(0+)|(0+)/g;
|
|
39
|
-
var CONCISE_INTEGER_WIDTH_REGEX = /^(0+)$/;
|
|
40
|
-
function parseSignificantPrecision(str) {
|
|
41
|
-
var result = {};
|
|
42
|
-
if (str[str.length - 1] === 'r') {
|
|
43
|
-
result.roundingPriority = 'morePrecision';
|
|
44
|
-
}
|
|
45
|
-
else if (str[str.length - 1] === 's') {
|
|
46
|
-
result.roundingPriority = 'lessPrecision';
|
|
47
|
-
}
|
|
48
|
-
str.replace(SIGNIFICANT_PRECISION_REGEX, function (_, g1, g2) {
|
|
49
|
-
// @@@ case
|
|
50
|
-
if (typeof g2 !== 'string') {
|
|
51
|
-
result.minimumSignificantDigits = g1.length;
|
|
52
|
-
result.maximumSignificantDigits = g1.length;
|
|
53
|
-
}
|
|
54
|
-
// @@@+ case
|
|
55
|
-
else if (g2 === '+') {
|
|
56
|
-
result.minimumSignificantDigits = g1.length;
|
|
57
|
-
}
|
|
58
|
-
// .### case
|
|
59
|
-
else if (g1[0] === '#') {
|
|
60
|
-
result.maximumSignificantDigits = g1.length;
|
|
61
|
-
}
|
|
62
|
-
// .@@## or .@@@ case
|
|
63
|
-
else {
|
|
64
|
-
result.minimumSignificantDigits = g1.length;
|
|
65
|
-
result.maximumSignificantDigits =
|
|
66
|
-
g1.length + (typeof g2 === 'string' ? g2.length : 0);
|
|
67
|
-
}
|
|
68
|
-
return '';
|
|
69
|
-
});
|
|
70
|
-
return result;
|
|
71
|
-
}
|
|
72
|
-
function parseSign(str) {
|
|
73
|
-
switch (str) {
|
|
74
|
-
case 'sign-auto':
|
|
75
|
-
return {
|
|
76
|
-
signDisplay: 'auto',
|
|
77
|
-
};
|
|
78
|
-
case 'sign-accounting':
|
|
79
|
-
case '()':
|
|
80
|
-
return {
|
|
81
|
-
currencySign: 'accounting',
|
|
82
|
-
};
|
|
83
|
-
case 'sign-always':
|
|
84
|
-
case '+!':
|
|
85
|
-
return {
|
|
86
|
-
signDisplay: 'always',
|
|
87
|
-
};
|
|
88
|
-
case 'sign-accounting-always':
|
|
89
|
-
case '()!':
|
|
90
|
-
return {
|
|
91
|
-
signDisplay: 'always',
|
|
92
|
-
currencySign: 'accounting',
|
|
93
|
-
};
|
|
94
|
-
case 'sign-except-zero':
|
|
95
|
-
case '+?':
|
|
96
|
-
return {
|
|
97
|
-
signDisplay: 'exceptZero',
|
|
98
|
-
};
|
|
99
|
-
case 'sign-accounting-except-zero':
|
|
100
|
-
case '()?':
|
|
101
|
-
return {
|
|
102
|
-
signDisplay: 'exceptZero',
|
|
103
|
-
currencySign: 'accounting',
|
|
104
|
-
};
|
|
105
|
-
case 'sign-never':
|
|
106
|
-
case '+_':
|
|
107
|
-
return {
|
|
108
|
-
signDisplay: 'never',
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
function parseConciseScientificAndEngineeringStem(stem) {
|
|
113
|
-
// Engineering
|
|
114
|
-
var result;
|
|
115
|
-
if (stem[0] === 'E' && stem[1] === 'E') {
|
|
116
|
-
result = {
|
|
117
|
-
notation: 'engineering',
|
|
118
|
-
};
|
|
119
|
-
stem = stem.slice(2);
|
|
120
|
-
}
|
|
121
|
-
else if (stem[0] === 'E') {
|
|
122
|
-
result = {
|
|
123
|
-
notation: 'scientific',
|
|
124
|
-
};
|
|
125
|
-
stem = stem.slice(1);
|
|
126
|
-
}
|
|
127
|
-
if (result) {
|
|
128
|
-
var signDisplay = stem.slice(0, 2);
|
|
129
|
-
if (signDisplay === '+!') {
|
|
130
|
-
result.signDisplay = 'always';
|
|
131
|
-
stem = stem.slice(2);
|
|
132
|
-
}
|
|
133
|
-
else if (signDisplay === '+?') {
|
|
134
|
-
result.signDisplay = 'exceptZero';
|
|
135
|
-
stem = stem.slice(2);
|
|
136
|
-
}
|
|
137
|
-
if (!CONCISE_INTEGER_WIDTH_REGEX.test(stem)) {
|
|
138
|
-
throw new Error('Malformed concise eng/scientific notation');
|
|
139
|
-
}
|
|
140
|
-
result.minimumIntegerDigits = stem.length;
|
|
141
|
-
}
|
|
142
|
-
return result;
|
|
143
|
-
}
|
|
144
|
-
function parseNotationOptions(opt) {
|
|
145
|
-
var result = {};
|
|
146
|
-
var signOpts = parseSign(opt);
|
|
147
|
-
if (signOpts) {
|
|
148
|
-
return signOpts;
|
|
149
|
-
}
|
|
150
|
-
return result;
|
|
151
|
-
}
|
|
152
|
-
/**
|
|
153
|
-
* https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#skeleton-stems-and-options
|
|
154
|
-
*/
|
|
155
|
-
function parseNumberSkeleton(tokens) {
|
|
156
|
-
var result = {};
|
|
157
|
-
for (var _i = 0, tokens_1 = tokens; _i < tokens_1.length; _i++) {
|
|
158
|
-
var token = tokens_1[_i];
|
|
159
|
-
switch (token.stem) {
|
|
160
|
-
case 'percent':
|
|
161
|
-
case '%':
|
|
162
|
-
result.style = 'percent';
|
|
163
|
-
continue;
|
|
164
|
-
case '%x100':
|
|
165
|
-
result.style = 'percent';
|
|
166
|
-
result.scale = 100;
|
|
167
|
-
continue;
|
|
168
|
-
case 'currency':
|
|
169
|
-
result.style = 'currency';
|
|
170
|
-
result.currency = token.options[0];
|
|
171
|
-
continue;
|
|
172
|
-
case 'group-off':
|
|
173
|
-
case ',_':
|
|
174
|
-
result.useGrouping = false;
|
|
175
|
-
continue;
|
|
176
|
-
case 'precision-integer':
|
|
177
|
-
case '.':
|
|
178
|
-
result.maximumFractionDigits = 0;
|
|
179
|
-
continue;
|
|
180
|
-
case 'measure-unit':
|
|
181
|
-
case 'unit':
|
|
182
|
-
result.style = 'unit';
|
|
183
|
-
result.unit = icuUnitToEcma(token.options[0]);
|
|
184
|
-
continue;
|
|
185
|
-
case 'compact-short':
|
|
186
|
-
case 'K':
|
|
187
|
-
result.notation = 'compact';
|
|
188
|
-
result.compactDisplay = 'short';
|
|
189
|
-
continue;
|
|
190
|
-
case 'compact-long':
|
|
191
|
-
case 'KK':
|
|
192
|
-
result.notation = 'compact';
|
|
193
|
-
result.compactDisplay = 'long';
|
|
194
|
-
continue;
|
|
195
|
-
case 'scientific':
|
|
196
|
-
result = (0, tslib_1.__assign)((0, tslib_1.__assign)((0, tslib_1.__assign)({}, result), { notation: 'scientific' }), token.options.reduce(function (all, opt) { return ((0, tslib_1.__assign)((0, tslib_1.__assign)({}, all), parseNotationOptions(opt))); }, {}));
|
|
197
|
-
continue;
|
|
198
|
-
case 'engineering':
|
|
199
|
-
result = (0, tslib_1.__assign)((0, tslib_1.__assign)((0, tslib_1.__assign)({}, result), { notation: 'engineering' }), token.options.reduce(function (all, opt) { return ((0, tslib_1.__assign)((0, tslib_1.__assign)({}, all), parseNotationOptions(opt))); }, {}));
|
|
200
|
-
continue;
|
|
201
|
-
case 'notation-simple':
|
|
202
|
-
result.notation = 'standard';
|
|
203
|
-
continue;
|
|
204
|
-
// https://github.com/unicode-org/icu/blob/master/icu4c/source/i18n/unicode/unumberformatter.h
|
|
205
|
-
case 'unit-width-narrow':
|
|
206
|
-
result.currencyDisplay = 'narrowSymbol';
|
|
207
|
-
result.unitDisplay = 'narrow';
|
|
208
|
-
continue;
|
|
209
|
-
case 'unit-width-short':
|
|
210
|
-
result.currencyDisplay = 'code';
|
|
211
|
-
result.unitDisplay = 'short';
|
|
212
|
-
continue;
|
|
213
|
-
case 'unit-width-full-name':
|
|
214
|
-
result.currencyDisplay = 'name';
|
|
215
|
-
result.unitDisplay = 'long';
|
|
216
|
-
continue;
|
|
217
|
-
case 'unit-width-iso-code':
|
|
218
|
-
result.currencyDisplay = 'symbol';
|
|
219
|
-
continue;
|
|
220
|
-
case 'scale':
|
|
221
|
-
result.scale = parseFloat(token.options[0]);
|
|
222
|
-
continue;
|
|
223
|
-
// https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#integer-width
|
|
224
|
-
case 'integer-width':
|
|
225
|
-
if (token.options.length > 1) {
|
|
226
|
-
throw new RangeError('integer-width stems only accept a single optional option');
|
|
227
|
-
}
|
|
228
|
-
token.options[0].replace(INTEGER_WIDTH_REGEX, function (_, g1, g2, g3, g4, g5) {
|
|
229
|
-
if (g1) {
|
|
230
|
-
result.minimumIntegerDigits = g2.length;
|
|
231
|
-
}
|
|
232
|
-
else if (g3 && g4) {
|
|
233
|
-
throw new Error('We currently do not support maximum integer digits');
|
|
234
|
-
}
|
|
235
|
-
else if (g5) {
|
|
236
|
-
throw new Error('We currently do not support exact integer digits');
|
|
237
|
-
}
|
|
238
|
-
return '';
|
|
239
|
-
});
|
|
240
|
-
continue;
|
|
241
|
-
}
|
|
242
|
-
// https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#integer-width
|
|
243
|
-
if (CONCISE_INTEGER_WIDTH_REGEX.test(token.stem)) {
|
|
244
|
-
result.minimumIntegerDigits = token.stem.length;
|
|
245
|
-
continue;
|
|
246
|
-
}
|
|
247
|
-
if (FRACTION_PRECISION_REGEX.test(token.stem)) {
|
|
248
|
-
// Precision
|
|
249
|
-
// https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#fraction-precision
|
|
250
|
-
// precision-integer case
|
|
251
|
-
if (token.options.length > 1) {
|
|
252
|
-
throw new RangeError('Fraction-precision stems only accept a single optional option');
|
|
253
|
-
}
|
|
254
|
-
token.stem.replace(FRACTION_PRECISION_REGEX, function (_, g1, g2, g3, g4, g5) {
|
|
255
|
-
// .000* case (before ICU67 it was .000+)
|
|
256
|
-
if (g2 === '*') {
|
|
257
|
-
result.minimumFractionDigits = g1.length;
|
|
258
|
-
}
|
|
259
|
-
// .### case
|
|
260
|
-
else if (g3 && g3[0] === '#') {
|
|
261
|
-
result.maximumFractionDigits = g3.length;
|
|
262
|
-
}
|
|
263
|
-
// .00## case
|
|
264
|
-
else if (g4 && g5) {
|
|
265
|
-
result.minimumFractionDigits = g4.length;
|
|
266
|
-
result.maximumFractionDigits = g4.length + g5.length;
|
|
267
|
-
}
|
|
268
|
-
else {
|
|
269
|
-
result.minimumFractionDigits = g1.length;
|
|
270
|
-
result.maximumFractionDigits = g1.length;
|
|
271
|
-
}
|
|
272
|
-
return '';
|
|
273
|
-
});
|
|
274
|
-
var opt = token.options[0];
|
|
275
|
-
// https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#trailing-zero-display
|
|
276
|
-
if (opt === 'w') {
|
|
277
|
-
result = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, result), { trailingZeroDisplay: 'stripIfInteger' });
|
|
278
|
-
}
|
|
279
|
-
else if (opt) {
|
|
280
|
-
result = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, result), parseSignificantPrecision(opt));
|
|
281
|
-
}
|
|
282
|
-
continue;
|
|
283
|
-
}
|
|
284
|
-
// https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#significant-digits-precision
|
|
285
|
-
if (SIGNIFICANT_PRECISION_REGEX.test(token.stem)) {
|
|
286
|
-
result = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, result), parseSignificantPrecision(token.stem));
|
|
287
|
-
continue;
|
|
288
|
-
}
|
|
289
|
-
var signOpts = parseSign(token.stem);
|
|
290
|
-
if (signOpts) {
|
|
291
|
-
result = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, result), signOpts);
|
|
292
|
-
}
|
|
293
|
-
var conciseScientificAndEngineeringOpts = parseConciseScientificAndEngineeringStem(token.stem);
|
|
294
|
-
if (conciseScientificAndEngineeringOpts) {
|
|
295
|
-
result = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, result), conciseScientificAndEngineeringOpts);
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
return result;
|
|
299
|
-
}
|
|
300
|
-
exports.parseNumberSkeleton = parseNumberSkeleton;
|
package/regex.generated.d.ts
DELETED
package/regex.generated.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"regex.generated.d.ts","sourceRoot":"","sources":["../../../../../packages/icu-skeleton-parser/regex.generated.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,iBAAiB,QAA0C,CAAA"}
|