@formatjs/icu-skeleton-parser 1.8.16 → 2.0.0
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/date-time.js +1 -4
- package/index.d.ts +2 -2
- package/index.js +2 -5
- package/number.js +12 -16
- package/package.json +10 -8
- package/regex.generated.js +1 -4
- package/lib/date-time.d.ts +0 -7
- package/lib/date-time.js +0 -121
- package/lib/index.d.ts +0 -2
- package/lib/index.js +0 -2
- package/lib/number.d.ts +0 -13
- package/lib/number.js +0 -316
- package/lib/regex.generated.d.ts +0 -1
- package/lib/regex.generated.js +0 -2
package/date-time.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseDateTimeSkeleton = parseDateTimeSkeleton;
|
|
4
1
|
/**
|
|
5
2
|
* https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
|
|
6
3
|
* Credit: https://github.com/caridy/intl-datetimeformat-pattern/blob/master/index.js
|
|
@@ -13,7 +10,7 @@ var DATE_TIME_REGEX = /(?:[Eec]{1,6}|G{1,5}|[Qq]{1,5}|(?:[yYur]+|U{1,5})|[ML]{1,
|
|
|
13
10
|
* @public
|
|
14
11
|
* @param skeleton skeleton string
|
|
15
12
|
*/
|
|
16
|
-
function parseDateTimeSkeleton(skeleton) {
|
|
13
|
+
export function parseDateTimeSkeleton(skeleton) {
|
|
17
14
|
var result = {};
|
|
18
15
|
skeleton.replace(DATE_TIME_REGEX, function (match) {
|
|
19
16
|
var len = match.length;
|
package/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './date-time';
|
|
2
|
-
export * from './number';
|
|
1
|
+
export * from './date-time.js';
|
|
2
|
+
export * from './number.js';
|
package/index.js
CHANGED
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var tslib_1 = require("tslib");
|
|
4
|
-
tslib_1.__exportStar(require("./date-time"), exports);
|
|
5
|
-
tslib_1.__exportStar(require("./number"), exports);
|
|
1
|
+
export * from './date-time.js';
|
|
2
|
+
export * from './number.js';
|
package/number.js
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
exports.parseNumberSkeleton = parseNumberSkeleton;
|
|
5
|
-
var tslib_1 = require("tslib");
|
|
6
|
-
var regex_generated_1 = require("./regex.generated");
|
|
7
|
-
function parseNumberSkeletonFromString(skeleton) {
|
|
1
|
+
import { __assign } from "tslib";
|
|
2
|
+
import { WHITE_SPACE_REGEX } from './regex.generated.js';
|
|
3
|
+
export function parseNumberSkeletonFromString(skeleton) {
|
|
8
4
|
if (skeleton.length === 0) {
|
|
9
5
|
throw new Error('Number skeleton cannot be empty');
|
|
10
6
|
}
|
|
11
7
|
// Parse the skeleton
|
|
12
8
|
var stringTokens = skeleton
|
|
13
|
-
.split(
|
|
9
|
+
.split(WHITE_SPACE_REGEX)
|
|
14
10
|
.filter(function (x) { return x.length > 0; });
|
|
15
11
|
var tokens = [];
|
|
16
12
|
for (var _i = 0, stringTokens_1 = stringTokens; _i < stringTokens_1.length; _i++) {
|
|
@@ -152,7 +148,7 @@ function parseNotationOptions(opt) {
|
|
|
152
148
|
/**
|
|
153
149
|
* https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#skeleton-stems-and-options
|
|
154
150
|
*/
|
|
155
|
-
function parseNumberSkeleton(tokens) {
|
|
151
|
+
export function parseNumberSkeleton(tokens) {
|
|
156
152
|
var result = {};
|
|
157
153
|
for (var _i = 0, tokens_1 = tokens; _i < tokens_1.length; _i++) {
|
|
158
154
|
var token = tokens_1[_i];
|
|
@@ -193,10 +189,10 @@ function parseNumberSkeleton(tokens) {
|
|
|
193
189
|
result.compactDisplay = 'long';
|
|
194
190
|
continue;
|
|
195
191
|
case 'scientific':
|
|
196
|
-
result =
|
|
192
|
+
result = __assign(__assign(__assign({}, result), { notation: 'scientific' }), token.options.reduce(function (all, opt) { return (__assign(__assign({}, all), parseNotationOptions(opt))); }, {}));
|
|
197
193
|
continue;
|
|
198
194
|
case 'engineering':
|
|
199
|
-
result =
|
|
195
|
+
result = __assign(__assign(__assign({}, result), { notation: 'engineering' }), token.options.reduce(function (all, opt) { return (__assign(__assign({}, all), parseNotationOptions(opt))); }, {}));
|
|
200
196
|
continue;
|
|
201
197
|
case 'notation-simple':
|
|
202
198
|
result.notation = 'standard';
|
|
@@ -295,25 +291,25 @@ function parseNumberSkeleton(tokens) {
|
|
|
295
291
|
var opt = token.options[0];
|
|
296
292
|
// https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#trailing-zero-display
|
|
297
293
|
if (opt === 'w') {
|
|
298
|
-
result =
|
|
294
|
+
result = __assign(__assign({}, result), { trailingZeroDisplay: 'stripIfInteger' });
|
|
299
295
|
}
|
|
300
296
|
else if (opt) {
|
|
301
|
-
result =
|
|
297
|
+
result = __assign(__assign({}, result), parseSignificantPrecision(opt));
|
|
302
298
|
}
|
|
303
299
|
continue;
|
|
304
300
|
}
|
|
305
301
|
// https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#significant-digits-precision
|
|
306
302
|
if (SIGNIFICANT_PRECISION_REGEX.test(token.stem)) {
|
|
307
|
-
result =
|
|
303
|
+
result = __assign(__assign({}, result), parseSignificantPrecision(token.stem));
|
|
308
304
|
continue;
|
|
309
305
|
}
|
|
310
306
|
var signOpts = parseSign(token.stem);
|
|
311
307
|
if (signOpts) {
|
|
312
|
-
result =
|
|
308
|
+
result = __assign(__assign({}, result), signOpts);
|
|
313
309
|
}
|
|
314
310
|
var conciseScientificAndEngineeringOpts = parseConciseScientificAndEngineeringStem(token.stem);
|
|
315
311
|
if (conciseScientificAndEngineeringOpts) {
|
|
316
|
-
result =
|
|
312
|
+
result = __assign(__assign({}, result), conciseScientificAndEngineeringOpts);
|
|
317
313
|
}
|
|
318
314
|
}
|
|
319
315
|
return result;
|
package/package.json
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formatjs/icu-skeleton-parser",
|
|
3
|
-
"version": "
|
|
4
|
-
"license": "MIT",
|
|
5
|
-
"types": "index.d.ts",
|
|
3
|
+
"version": "2.0.0",
|
|
6
4
|
"dependencies": {
|
|
7
|
-
"
|
|
8
|
-
"
|
|
5
|
+
"@formatjs/ecma402-abstract": "3.0.0",
|
|
6
|
+
"tslib": "^2.8.0"
|
|
7
|
+
},
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./index.js"
|
|
9
10
|
},
|
|
10
|
-
"
|
|
11
|
-
"module": "lib/index.js",
|
|
11
|
+
"license": "MIT",
|
|
12
12
|
"repository": {
|
|
13
13
|
"type": "git",
|
|
14
14
|
"url": "https://github.com/formatjs/formatjs.git",
|
|
15
15
|
"directory": "packages/icu-skeleton-parser"
|
|
16
|
-
}
|
|
16
|
+
},
|
|
17
|
+
"type": "module",
|
|
18
|
+
"types": "index.d.ts"
|
|
17
19
|
}
|
package/regex.generated.js
CHANGED
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WHITE_SPACE_REGEX = void 0;
|
|
4
1
|
// @generated from regex-gen.ts
|
|
5
|
-
|
|
2
|
+
export var WHITE_SPACE_REGEX = /[\t-\r \x85\u200E\u200F\u2028\u2029]/i;
|
package/lib/date-time.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Parse Date time skeleton into Intl.DateTimeFormatOptions
|
|
3
|
-
* Ref: https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
|
|
4
|
-
* @public
|
|
5
|
-
* @param skeleton skeleton string
|
|
6
|
-
*/
|
|
7
|
-
export declare function parseDateTimeSkeleton(skeleton: string): Intl.DateTimeFormatOptions;
|
package/lib/date-time.js
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
|
|
3
|
-
* Credit: https://github.com/caridy/intl-datetimeformat-pattern/blob/master/index.js
|
|
4
|
-
* with some tweaks
|
|
5
|
-
*/
|
|
6
|
-
var DATE_TIME_REGEX = /(?:[Eec]{1,6}|G{1,5}|[Qq]{1,5}|(?:[yYur]+|U{1,5})|[ML]{1,5}|d{1,2}|D{1,3}|F{1}|[abB]{1,5}|[hkHK]{1,2}|w{1,2}|W{1}|m{1,2}|s{1,2}|[zZOvVxX]{1,4})(?=([^']*'[^']*')*[^']*$)/g;
|
|
7
|
-
/**
|
|
8
|
-
* Parse Date time skeleton into Intl.DateTimeFormatOptions
|
|
9
|
-
* Ref: https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
|
|
10
|
-
* @public
|
|
11
|
-
* @param skeleton skeleton string
|
|
12
|
-
*/
|
|
13
|
-
export function parseDateTimeSkeleton(skeleton) {
|
|
14
|
-
var result = {};
|
|
15
|
-
skeleton.replace(DATE_TIME_REGEX, function (match) {
|
|
16
|
-
var len = match.length;
|
|
17
|
-
switch (match[0]) {
|
|
18
|
-
// Era
|
|
19
|
-
case 'G':
|
|
20
|
-
result.era = len === 4 ? 'long' : len === 5 ? 'narrow' : 'short';
|
|
21
|
-
break;
|
|
22
|
-
// Year
|
|
23
|
-
case 'y':
|
|
24
|
-
result.year = len === 2 ? '2-digit' : 'numeric';
|
|
25
|
-
break;
|
|
26
|
-
case 'Y':
|
|
27
|
-
case 'u':
|
|
28
|
-
case 'U':
|
|
29
|
-
case 'r':
|
|
30
|
-
throw new RangeError('`Y/u/U/r` (year) patterns are not supported, use `y` instead');
|
|
31
|
-
// Quarter
|
|
32
|
-
case 'q':
|
|
33
|
-
case 'Q':
|
|
34
|
-
throw new RangeError('`q/Q` (quarter) patterns are not supported');
|
|
35
|
-
// Month
|
|
36
|
-
case 'M':
|
|
37
|
-
case 'L':
|
|
38
|
-
result.month = ['numeric', '2-digit', 'short', 'long', 'narrow'][len - 1];
|
|
39
|
-
break;
|
|
40
|
-
// Week
|
|
41
|
-
case 'w':
|
|
42
|
-
case 'W':
|
|
43
|
-
throw new RangeError('`w/W` (week) patterns are not supported');
|
|
44
|
-
case 'd':
|
|
45
|
-
result.day = ['numeric', '2-digit'][len - 1];
|
|
46
|
-
break;
|
|
47
|
-
case 'D':
|
|
48
|
-
case 'F':
|
|
49
|
-
case 'g':
|
|
50
|
-
throw new RangeError('`D/F/g` (day) patterns are not supported, use `d` instead');
|
|
51
|
-
// Weekday
|
|
52
|
-
case 'E':
|
|
53
|
-
result.weekday = len === 4 ? 'long' : len === 5 ? 'narrow' : 'short';
|
|
54
|
-
break;
|
|
55
|
-
case 'e':
|
|
56
|
-
if (len < 4) {
|
|
57
|
-
throw new RangeError('`e..eee` (weekday) patterns are not supported');
|
|
58
|
-
}
|
|
59
|
-
result.weekday = ['short', 'long', 'narrow', 'short'][len - 4];
|
|
60
|
-
break;
|
|
61
|
-
case 'c':
|
|
62
|
-
if (len < 4) {
|
|
63
|
-
throw new RangeError('`c..ccc` (weekday) patterns are not supported');
|
|
64
|
-
}
|
|
65
|
-
result.weekday = ['short', 'long', 'narrow', 'short'][len - 4];
|
|
66
|
-
break;
|
|
67
|
-
// Period
|
|
68
|
-
case 'a': // AM, PM
|
|
69
|
-
result.hour12 = true;
|
|
70
|
-
break;
|
|
71
|
-
case 'b': // am, pm, noon, midnight
|
|
72
|
-
case 'B': // flexible day periods
|
|
73
|
-
throw new RangeError('`b/B` (period) patterns are not supported, use `a` instead');
|
|
74
|
-
// Hour
|
|
75
|
-
case 'h':
|
|
76
|
-
result.hourCycle = 'h12';
|
|
77
|
-
result.hour = ['numeric', '2-digit'][len - 1];
|
|
78
|
-
break;
|
|
79
|
-
case 'H':
|
|
80
|
-
result.hourCycle = 'h23';
|
|
81
|
-
result.hour = ['numeric', '2-digit'][len - 1];
|
|
82
|
-
break;
|
|
83
|
-
case 'K':
|
|
84
|
-
result.hourCycle = 'h11';
|
|
85
|
-
result.hour = ['numeric', '2-digit'][len - 1];
|
|
86
|
-
break;
|
|
87
|
-
case 'k':
|
|
88
|
-
result.hourCycle = 'h24';
|
|
89
|
-
result.hour = ['numeric', '2-digit'][len - 1];
|
|
90
|
-
break;
|
|
91
|
-
case 'j':
|
|
92
|
-
case 'J':
|
|
93
|
-
case 'C':
|
|
94
|
-
throw new RangeError('`j/J/C` (hour) patterns are not supported, use `h/H/K/k` instead');
|
|
95
|
-
// Minute
|
|
96
|
-
case 'm':
|
|
97
|
-
result.minute = ['numeric', '2-digit'][len - 1];
|
|
98
|
-
break;
|
|
99
|
-
// Second
|
|
100
|
-
case 's':
|
|
101
|
-
result.second = ['numeric', '2-digit'][len - 1];
|
|
102
|
-
break;
|
|
103
|
-
case 'S':
|
|
104
|
-
case 'A':
|
|
105
|
-
throw new RangeError('`S/A` (second) patterns are not supported, use `s` instead');
|
|
106
|
-
// Zone
|
|
107
|
-
case 'z': // 1..3, 4: specific non-location format
|
|
108
|
-
result.timeZoneName = len < 4 ? 'short' : 'long';
|
|
109
|
-
break;
|
|
110
|
-
case 'Z': // 1..3, 4, 5: The ISO8601 varios formats
|
|
111
|
-
case 'O': // 1, 4: milliseconds in day short, long
|
|
112
|
-
case 'v': // 1, 4: generic non-location format
|
|
113
|
-
case 'V': // 1, 2, 3, 4: time zone ID or city
|
|
114
|
-
case 'X': // 1, 2, 3, 4: The ISO8601 varios formats
|
|
115
|
-
case 'x': // 1, 2, 3, 4: The ISO8601 varios formats
|
|
116
|
-
throw new RangeError('`Z/O/v/V/X/x` (timeZone) patterns are not supported, use `z` instead');
|
|
117
|
-
}
|
|
118
|
-
return '';
|
|
119
|
-
});
|
|
120
|
-
return result;
|
|
121
|
-
}
|
package/lib/index.d.ts
DELETED
package/lib/index.js
DELETED
package/lib/number.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { NumberFormatOptions } from '@formatjs/ecma402-abstract';
|
|
2
|
-
export interface ExtendedNumberFormatOptions extends NumberFormatOptions {
|
|
3
|
-
scale?: number;
|
|
4
|
-
}
|
|
5
|
-
export interface NumberSkeletonToken {
|
|
6
|
-
stem: string;
|
|
7
|
-
options: string[];
|
|
8
|
-
}
|
|
9
|
-
export declare function parseNumberSkeletonFromString(skeleton: string): NumberSkeletonToken[];
|
|
10
|
-
/**
|
|
11
|
-
* https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#skeleton-stems-and-options
|
|
12
|
-
*/
|
|
13
|
-
export declare function parseNumberSkeleton(tokens: NumberSkeletonToken[]): ExtendedNumberFormatOptions;
|
package/lib/number.js
DELETED
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
import { __assign } from "tslib";
|
|
2
|
-
import { WHITE_SPACE_REGEX } from './regex.generated';
|
|
3
|
-
export function parseNumberSkeletonFromString(skeleton) {
|
|
4
|
-
if (skeleton.length === 0) {
|
|
5
|
-
throw new Error('Number skeleton cannot be empty');
|
|
6
|
-
}
|
|
7
|
-
// Parse the skeleton
|
|
8
|
-
var stringTokens = skeleton
|
|
9
|
-
.split(WHITE_SPACE_REGEX)
|
|
10
|
-
.filter(function (x) { return x.length > 0; });
|
|
11
|
-
var tokens = [];
|
|
12
|
-
for (var _i = 0, stringTokens_1 = stringTokens; _i < stringTokens_1.length; _i++) {
|
|
13
|
-
var stringToken = stringTokens_1[_i];
|
|
14
|
-
var stemAndOptions = stringToken.split('/');
|
|
15
|
-
if (stemAndOptions.length === 0) {
|
|
16
|
-
throw new Error('Invalid number skeleton');
|
|
17
|
-
}
|
|
18
|
-
var stem = stemAndOptions[0], options = stemAndOptions.slice(1);
|
|
19
|
-
for (var _a = 0, options_1 = options; _a < options_1.length; _a++) {
|
|
20
|
-
var option = options_1[_a];
|
|
21
|
-
if (option.length === 0) {
|
|
22
|
-
throw new Error('Invalid number skeleton');
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
tokens.push({ stem: stem, options: options });
|
|
26
|
-
}
|
|
27
|
-
return tokens;
|
|
28
|
-
}
|
|
29
|
-
function icuUnitToEcma(unit) {
|
|
30
|
-
return unit.replace(/^(.*?)-/, '');
|
|
31
|
-
}
|
|
32
|
-
var FRACTION_PRECISION_REGEX = /^\.(?:(0+)(\*)?|(#+)|(0+)(#+))$/g;
|
|
33
|
-
var SIGNIFICANT_PRECISION_REGEX = /^(@+)?(\+|#+)?[rs]?$/g;
|
|
34
|
-
var INTEGER_WIDTH_REGEX = /(\*)(0+)|(#+)(0+)|(0+)/g;
|
|
35
|
-
var CONCISE_INTEGER_WIDTH_REGEX = /^(0+)$/;
|
|
36
|
-
function parseSignificantPrecision(str) {
|
|
37
|
-
var result = {};
|
|
38
|
-
if (str[str.length - 1] === 'r') {
|
|
39
|
-
result.roundingPriority = 'morePrecision';
|
|
40
|
-
}
|
|
41
|
-
else if (str[str.length - 1] === 's') {
|
|
42
|
-
result.roundingPriority = 'lessPrecision';
|
|
43
|
-
}
|
|
44
|
-
str.replace(SIGNIFICANT_PRECISION_REGEX, function (_, g1, g2) {
|
|
45
|
-
// @@@ case
|
|
46
|
-
if (typeof g2 !== 'string') {
|
|
47
|
-
result.minimumSignificantDigits = g1.length;
|
|
48
|
-
result.maximumSignificantDigits = g1.length;
|
|
49
|
-
}
|
|
50
|
-
// @@@+ case
|
|
51
|
-
else if (g2 === '+') {
|
|
52
|
-
result.minimumSignificantDigits = g1.length;
|
|
53
|
-
}
|
|
54
|
-
// .### case
|
|
55
|
-
else if (g1[0] === '#') {
|
|
56
|
-
result.maximumSignificantDigits = g1.length;
|
|
57
|
-
}
|
|
58
|
-
// .@@## or .@@@ case
|
|
59
|
-
else {
|
|
60
|
-
result.minimumSignificantDigits = g1.length;
|
|
61
|
-
result.maximumSignificantDigits =
|
|
62
|
-
g1.length + (typeof g2 === 'string' ? g2.length : 0);
|
|
63
|
-
}
|
|
64
|
-
return '';
|
|
65
|
-
});
|
|
66
|
-
return result;
|
|
67
|
-
}
|
|
68
|
-
function parseSign(str) {
|
|
69
|
-
switch (str) {
|
|
70
|
-
case 'sign-auto':
|
|
71
|
-
return {
|
|
72
|
-
signDisplay: 'auto',
|
|
73
|
-
};
|
|
74
|
-
case 'sign-accounting':
|
|
75
|
-
case '()':
|
|
76
|
-
return {
|
|
77
|
-
currencySign: 'accounting',
|
|
78
|
-
};
|
|
79
|
-
case 'sign-always':
|
|
80
|
-
case '+!':
|
|
81
|
-
return {
|
|
82
|
-
signDisplay: 'always',
|
|
83
|
-
};
|
|
84
|
-
case 'sign-accounting-always':
|
|
85
|
-
case '()!':
|
|
86
|
-
return {
|
|
87
|
-
signDisplay: 'always',
|
|
88
|
-
currencySign: 'accounting',
|
|
89
|
-
};
|
|
90
|
-
case 'sign-except-zero':
|
|
91
|
-
case '+?':
|
|
92
|
-
return {
|
|
93
|
-
signDisplay: 'exceptZero',
|
|
94
|
-
};
|
|
95
|
-
case 'sign-accounting-except-zero':
|
|
96
|
-
case '()?':
|
|
97
|
-
return {
|
|
98
|
-
signDisplay: 'exceptZero',
|
|
99
|
-
currencySign: 'accounting',
|
|
100
|
-
};
|
|
101
|
-
case 'sign-never':
|
|
102
|
-
case '+_':
|
|
103
|
-
return {
|
|
104
|
-
signDisplay: 'never',
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
function parseConciseScientificAndEngineeringStem(stem) {
|
|
109
|
-
// Engineering
|
|
110
|
-
var result;
|
|
111
|
-
if (stem[0] === 'E' && stem[1] === 'E') {
|
|
112
|
-
result = {
|
|
113
|
-
notation: 'engineering',
|
|
114
|
-
};
|
|
115
|
-
stem = stem.slice(2);
|
|
116
|
-
}
|
|
117
|
-
else if (stem[0] === 'E') {
|
|
118
|
-
result = {
|
|
119
|
-
notation: 'scientific',
|
|
120
|
-
};
|
|
121
|
-
stem = stem.slice(1);
|
|
122
|
-
}
|
|
123
|
-
if (result) {
|
|
124
|
-
var signDisplay = stem.slice(0, 2);
|
|
125
|
-
if (signDisplay === '+!') {
|
|
126
|
-
result.signDisplay = 'always';
|
|
127
|
-
stem = stem.slice(2);
|
|
128
|
-
}
|
|
129
|
-
else if (signDisplay === '+?') {
|
|
130
|
-
result.signDisplay = 'exceptZero';
|
|
131
|
-
stem = stem.slice(2);
|
|
132
|
-
}
|
|
133
|
-
if (!CONCISE_INTEGER_WIDTH_REGEX.test(stem)) {
|
|
134
|
-
throw new Error('Malformed concise eng/scientific notation');
|
|
135
|
-
}
|
|
136
|
-
result.minimumIntegerDigits = stem.length;
|
|
137
|
-
}
|
|
138
|
-
return result;
|
|
139
|
-
}
|
|
140
|
-
function parseNotationOptions(opt) {
|
|
141
|
-
var result = {};
|
|
142
|
-
var signOpts = parseSign(opt);
|
|
143
|
-
if (signOpts) {
|
|
144
|
-
return signOpts;
|
|
145
|
-
}
|
|
146
|
-
return result;
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* https://github.com/unicode-org/icu/blob/master/docs/userguide/format_parse/numbers/skeletons.md#skeleton-stems-and-options
|
|
150
|
-
*/
|
|
151
|
-
export function parseNumberSkeleton(tokens) {
|
|
152
|
-
var result = {};
|
|
153
|
-
for (var _i = 0, tokens_1 = tokens; _i < tokens_1.length; _i++) {
|
|
154
|
-
var token = tokens_1[_i];
|
|
155
|
-
switch (token.stem) {
|
|
156
|
-
case 'percent':
|
|
157
|
-
case '%':
|
|
158
|
-
result.style = 'percent';
|
|
159
|
-
continue;
|
|
160
|
-
case '%x100':
|
|
161
|
-
result.style = 'percent';
|
|
162
|
-
result.scale = 100;
|
|
163
|
-
continue;
|
|
164
|
-
case 'currency':
|
|
165
|
-
result.style = 'currency';
|
|
166
|
-
result.currency = token.options[0];
|
|
167
|
-
continue;
|
|
168
|
-
case 'group-off':
|
|
169
|
-
case ',_':
|
|
170
|
-
result.useGrouping = false;
|
|
171
|
-
continue;
|
|
172
|
-
case 'precision-integer':
|
|
173
|
-
case '.':
|
|
174
|
-
result.maximumFractionDigits = 0;
|
|
175
|
-
continue;
|
|
176
|
-
case 'measure-unit':
|
|
177
|
-
case 'unit':
|
|
178
|
-
result.style = 'unit';
|
|
179
|
-
result.unit = icuUnitToEcma(token.options[0]);
|
|
180
|
-
continue;
|
|
181
|
-
case 'compact-short':
|
|
182
|
-
case 'K':
|
|
183
|
-
result.notation = 'compact';
|
|
184
|
-
result.compactDisplay = 'short';
|
|
185
|
-
continue;
|
|
186
|
-
case 'compact-long':
|
|
187
|
-
case 'KK':
|
|
188
|
-
result.notation = 'compact';
|
|
189
|
-
result.compactDisplay = 'long';
|
|
190
|
-
continue;
|
|
191
|
-
case 'scientific':
|
|
192
|
-
result = __assign(__assign(__assign({}, result), { notation: 'scientific' }), token.options.reduce(function (all, opt) { return (__assign(__assign({}, all), parseNotationOptions(opt))); }, {}));
|
|
193
|
-
continue;
|
|
194
|
-
case 'engineering':
|
|
195
|
-
result = __assign(__assign(__assign({}, result), { notation: 'engineering' }), token.options.reduce(function (all, opt) { return (__assign(__assign({}, all), parseNotationOptions(opt))); }, {}));
|
|
196
|
-
continue;
|
|
197
|
-
case 'notation-simple':
|
|
198
|
-
result.notation = 'standard';
|
|
199
|
-
continue;
|
|
200
|
-
// https://github.com/unicode-org/icu/blob/master/icu4c/source/i18n/unicode/unumberformatter.h
|
|
201
|
-
case 'unit-width-narrow':
|
|
202
|
-
result.currencyDisplay = 'narrowSymbol';
|
|
203
|
-
result.unitDisplay = 'narrow';
|
|
204
|
-
continue;
|
|
205
|
-
case 'unit-width-short':
|
|
206
|
-
result.currencyDisplay = 'code';
|
|
207
|
-
result.unitDisplay = 'short';
|
|
208
|
-
continue;
|
|
209
|
-
case 'unit-width-full-name':
|
|
210
|
-
result.currencyDisplay = 'name';
|
|
211
|
-
result.unitDisplay = 'long';
|
|
212
|
-
continue;
|
|
213
|
-
case 'unit-width-iso-code':
|
|
214
|
-
result.currencyDisplay = 'symbol';
|
|
215
|
-
continue;
|
|
216
|
-
case 'scale':
|
|
217
|
-
result.scale = parseFloat(token.options[0]);
|
|
218
|
-
continue;
|
|
219
|
-
case 'rounding-mode-floor':
|
|
220
|
-
result.roundingMode = 'floor';
|
|
221
|
-
continue;
|
|
222
|
-
case 'rounding-mode-ceiling':
|
|
223
|
-
result.roundingMode = 'ceil';
|
|
224
|
-
continue;
|
|
225
|
-
case 'rounding-mode-down':
|
|
226
|
-
result.roundingMode = 'trunc';
|
|
227
|
-
continue;
|
|
228
|
-
case 'rounding-mode-up':
|
|
229
|
-
result.roundingMode = 'expand';
|
|
230
|
-
continue;
|
|
231
|
-
case 'rounding-mode-half-even':
|
|
232
|
-
result.roundingMode = 'halfEven';
|
|
233
|
-
continue;
|
|
234
|
-
case 'rounding-mode-half-down':
|
|
235
|
-
result.roundingMode = 'halfTrunc';
|
|
236
|
-
continue;
|
|
237
|
-
case 'rounding-mode-half-up':
|
|
238
|
-
result.roundingMode = 'halfExpand';
|
|
239
|
-
continue;
|
|
240
|
-
// https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#integer-width
|
|
241
|
-
case 'integer-width':
|
|
242
|
-
if (token.options.length > 1) {
|
|
243
|
-
throw new RangeError('integer-width stems only accept a single optional option');
|
|
244
|
-
}
|
|
245
|
-
token.options[0].replace(INTEGER_WIDTH_REGEX, function (_, g1, g2, g3, g4, g5) {
|
|
246
|
-
if (g1) {
|
|
247
|
-
result.minimumIntegerDigits = g2.length;
|
|
248
|
-
}
|
|
249
|
-
else if (g3 && g4) {
|
|
250
|
-
throw new Error('We currently do not support maximum integer digits');
|
|
251
|
-
}
|
|
252
|
-
else if (g5) {
|
|
253
|
-
throw new Error('We currently do not support exact integer digits');
|
|
254
|
-
}
|
|
255
|
-
return '';
|
|
256
|
-
});
|
|
257
|
-
continue;
|
|
258
|
-
}
|
|
259
|
-
// https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#integer-width
|
|
260
|
-
if (CONCISE_INTEGER_WIDTH_REGEX.test(token.stem)) {
|
|
261
|
-
result.minimumIntegerDigits = token.stem.length;
|
|
262
|
-
continue;
|
|
263
|
-
}
|
|
264
|
-
if (FRACTION_PRECISION_REGEX.test(token.stem)) {
|
|
265
|
-
// Precision
|
|
266
|
-
// https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#fraction-precision
|
|
267
|
-
// precision-integer case
|
|
268
|
-
if (token.options.length > 1) {
|
|
269
|
-
throw new RangeError('Fraction-precision stems only accept a single optional option');
|
|
270
|
-
}
|
|
271
|
-
token.stem.replace(FRACTION_PRECISION_REGEX, function (_, g1, g2, g3, g4, g5) {
|
|
272
|
-
// .000* case (before ICU67 it was .000+)
|
|
273
|
-
if (g2 === '*') {
|
|
274
|
-
result.minimumFractionDigits = g1.length;
|
|
275
|
-
}
|
|
276
|
-
// .### case
|
|
277
|
-
else if (g3 && g3[0] === '#') {
|
|
278
|
-
result.maximumFractionDigits = g3.length;
|
|
279
|
-
}
|
|
280
|
-
// .00## case
|
|
281
|
-
else if (g4 && g5) {
|
|
282
|
-
result.minimumFractionDigits = g4.length;
|
|
283
|
-
result.maximumFractionDigits = g4.length + g5.length;
|
|
284
|
-
}
|
|
285
|
-
else {
|
|
286
|
-
result.minimumFractionDigits = g1.length;
|
|
287
|
-
result.maximumFractionDigits = g1.length;
|
|
288
|
-
}
|
|
289
|
-
return '';
|
|
290
|
-
});
|
|
291
|
-
var opt = token.options[0];
|
|
292
|
-
// https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#trailing-zero-display
|
|
293
|
-
if (opt === 'w') {
|
|
294
|
-
result = __assign(__assign({}, result), { trailingZeroDisplay: 'stripIfInteger' });
|
|
295
|
-
}
|
|
296
|
-
else if (opt) {
|
|
297
|
-
result = __assign(__assign({}, result), parseSignificantPrecision(opt));
|
|
298
|
-
}
|
|
299
|
-
continue;
|
|
300
|
-
}
|
|
301
|
-
// https://unicode-org.github.io/icu/userguide/format_parse/numbers/skeletons.html#significant-digits-precision
|
|
302
|
-
if (SIGNIFICANT_PRECISION_REGEX.test(token.stem)) {
|
|
303
|
-
result = __assign(__assign({}, result), parseSignificantPrecision(token.stem));
|
|
304
|
-
continue;
|
|
305
|
-
}
|
|
306
|
-
var signOpts = parseSign(token.stem);
|
|
307
|
-
if (signOpts) {
|
|
308
|
-
result = __assign(__assign({}, result), signOpts);
|
|
309
|
-
}
|
|
310
|
-
var conciseScientificAndEngineeringOpts = parseConciseScientificAndEngineeringStem(token.stem);
|
|
311
|
-
if (conciseScientificAndEngineeringOpts) {
|
|
312
|
-
result = __assign(__assign({}, result), conciseScientificAndEngineeringOpts);
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
return result;
|
|
316
|
-
}
|
package/lib/regex.generated.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const WHITE_SPACE_REGEX: RegExp;
|
package/lib/regex.generated.js
DELETED