@formatjs/icu-skeleton-parser 2.0.7 → 2.0.8

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