@ultraq/icu-message-formatter 0.15.0-beta.1 → 0.15.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/CHANGELOG.md +3 -1
- package/README.md +6 -6
- package/dist/icu-message-formatter.browser.es.min.js +1 -180
- package/dist/icu-message-formatter.browser.es.min.js.map +1 -1
- package/dist/icu-message-formatter.browser.min.js +1 -1
- package/dist/icu-message-formatter.browser.min.js.map +1 -1
- package/dist/icu-message-formatter.cjs +462 -219
- package/dist/icu-message-formatter.cjs.map +1 -1
- package/dist/icu-message-formatter.d.cts +153 -0
- package/dist/icu-message-formatter.d.ts +49 -54
- package/dist/icu-message-formatter.js +461 -225
- package/dist/icu-message-formatter.js.map +1 -1
- package/package.json +27 -18
@@ -1,239 +1,482 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
var
|
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
|
-
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var functionUtils = require('@ultraq/function-utils');
|
4
|
+
|
5
|
+
/*
|
6
|
+
* Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)
|
7
|
+
*
|
8
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
9
|
+
* you may not use this file except in compliance with the License.
|
10
|
+
* You may obtain a copy of the License at
|
11
|
+
*
|
12
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
13
|
+
*
|
14
|
+
* Unless required by applicable law or agreed to in writing, software
|
15
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
16
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
17
|
+
* See the License for the specific language governing permissions and
|
18
|
+
* limitations under the License.
|
19
|
+
*/
|
20
|
+
|
21
|
+
/**
|
22
|
+
* @typedef ParseCasesResult
|
23
|
+
* @property {string[]} args
|
24
|
+
* A list of prepended arguments.
|
25
|
+
* @property {Record<string,string>} cases
|
26
|
+
* A map of all cases.
|
27
|
+
*/
|
28
|
+
|
29
|
+
/**
|
30
|
+
* Most branch-based type handlers are based around "cases". For example,
|
31
|
+
* `select` and `plural` compare compare a value to "case keys" to choose a
|
32
|
+
* subtranslation.
|
33
|
+
*
|
34
|
+
* This util splits "matches" portions provided to the aforementioned handlers
|
35
|
+
* into case strings, and extracts any prepended arguments (for example,
|
36
|
+
* `plural` supports an `offset:n` argument used for populating the magic `#`
|
37
|
+
* variable).
|
38
|
+
*
|
39
|
+
* @param {string} string
|
40
|
+
* @return {ParseCasesResult}
|
41
|
+
*/
|
42
|
+
function parseCases(string = '') {
|
43
|
+
const isWhitespace = ch => /\s/.test(ch);
|
44
|
+
|
45
|
+
const args = [];
|
46
|
+
const cases = {};
|
47
|
+
|
48
|
+
let currTermStart = 0;
|
49
|
+
let latestTerm = null;
|
50
|
+
let inTerm = false;
|
51
|
+
|
52
|
+
let i = 0;
|
53
|
+
while (i < string.length) {
|
54
|
+
// Term ended
|
55
|
+
if (inTerm && (isWhitespace(string[i]) || string[i] === '{')) {
|
56
|
+
inTerm = false;
|
57
|
+
latestTerm = string.slice(currTermStart, i);
|
58
|
+
|
59
|
+
// We want to process the opening char again so the case will be properly registered.
|
60
|
+
if (string[i] === '{') {
|
61
|
+
i--;
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
// New term
|
66
|
+
else if (!inTerm && !isWhitespace(string[i])) {
|
67
|
+
const caseBody = string[i] === '{';
|
68
|
+
|
69
|
+
// If there's a previous term, we can either handle a whole
|
70
|
+
// case, or add that as an argument.
|
71
|
+
if (latestTerm && caseBody) {
|
72
|
+
const branchEndIndex = findClosingBracket(string, i);
|
73
|
+
|
74
|
+
if (branchEndIndex === -1) {
|
75
|
+
throw new Error(`Unbalanced curly braces in string: "${string}"`);
|
76
|
+
}
|
77
|
+
|
78
|
+
cases[latestTerm] = string.slice(i + 1, branchEndIndex); // Don't include the braces
|
79
|
+
|
80
|
+
i = branchEndIndex; // Will be moved up where needed at end of loop.
|
81
|
+
latestTerm = null;
|
82
|
+
}
|
83
|
+
else {
|
84
|
+
if (latestTerm) {
|
85
|
+
args.push(latestTerm);
|
86
|
+
latestTerm = null;
|
87
|
+
}
|
88
|
+
|
89
|
+
inTerm = true;
|
90
|
+
currTermStart = i;
|
91
|
+
}
|
92
|
+
}
|
93
|
+
i++;
|
94
|
+
}
|
95
|
+
|
96
|
+
if (inTerm) {
|
97
|
+
latestTerm = string.slice(currTermStart);
|
98
|
+
}
|
99
|
+
|
100
|
+
if (latestTerm) {
|
101
|
+
args.push(latestTerm);
|
102
|
+
}
|
103
|
+
|
104
|
+
return {
|
105
|
+
args,
|
106
|
+
cases
|
107
|
+
};
|
53
108
|
}
|
109
|
+
|
110
|
+
/**
|
111
|
+
* Finds the index of the matching closing curly bracket, including through
|
112
|
+
* strings that could have nested brackets.
|
113
|
+
*
|
114
|
+
* @param {string} string
|
115
|
+
* @param {number} fromIndex
|
116
|
+
* @return {number}
|
117
|
+
* The index of the matching closing bracket, or -1 if no closing bracket
|
118
|
+
* could be found.
|
119
|
+
*/
|
54
120
|
function findClosingBracket(string, fromIndex) {
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
121
|
+
let depth = 0;
|
122
|
+
for (let i = fromIndex + 1; i < string.length; i++) {
|
123
|
+
let char = string.charAt(i);
|
124
|
+
if (char === '}') {
|
125
|
+
if (depth === 0) {
|
126
|
+
return i;
|
127
|
+
}
|
128
|
+
depth--;
|
129
|
+
}
|
130
|
+
else if (char === '{') {
|
131
|
+
depth++;
|
132
|
+
}
|
133
|
+
}
|
134
|
+
return -1;
|
68
135
|
}
|
136
|
+
|
137
|
+
/**
|
138
|
+
* Split a `{key, type, format}` block into those 3 parts, taking into account
|
139
|
+
* nested message syntax that can exist in the `format` part.
|
140
|
+
*
|
141
|
+
* @param {string} block
|
142
|
+
* @return {string[]}
|
143
|
+
* An array with `key`, `type`, and `format` items in that order, if present
|
144
|
+
* in the formatted argument block.
|
145
|
+
*/
|
69
146
|
function splitFormattedArgument(block) {
|
70
|
-
|
147
|
+
return split(block.slice(1, -1), ',', 3);
|
71
148
|
}
|
149
|
+
|
150
|
+
/**
|
151
|
+
* Like `String.prototype.split()` but where the limit parameter causes the
|
152
|
+
* remainder of the string to be grouped together in a final entry.
|
153
|
+
*
|
154
|
+
* @private
|
155
|
+
* @param {string} string
|
156
|
+
* @param {string} separator
|
157
|
+
* @param {number} limit
|
158
|
+
* @param {string[]} accumulator
|
159
|
+
* @return {string[]}
|
160
|
+
*/
|
72
161
|
function split(string, separator, limit, accumulator = []) {
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
162
|
+
if (!string) {
|
163
|
+
return accumulator;
|
164
|
+
}
|
165
|
+
if (limit === 1) {
|
166
|
+
accumulator.push(string);
|
167
|
+
return accumulator;
|
168
|
+
}
|
169
|
+
let indexOfDelimiter = string.indexOf(separator);
|
170
|
+
if (indexOfDelimiter === -1) {
|
171
|
+
accumulator.push(string);
|
172
|
+
return accumulator;
|
173
|
+
}
|
174
|
+
let head = string.substring(0, indexOfDelimiter).trim();
|
175
|
+
let tail = string.substring(indexOfDelimiter + separator.length + 1).trim();
|
176
|
+
accumulator.push(head);
|
177
|
+
return split(tail, separator, limit - 1, accumulator);
|
89
178
|
}
|
179
|
+
|
180
|
+
/*
|
181
|
+
* Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)
|
182
|
+
*
|
183
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
184
|
+
* you may not use this file except in compliance with the License.
|
185
|
+
* You may obtain a copy of the License at
|
186
|
+
*
|
187
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
188
|
+
*
|
189
|
+
* Unless required by applicable law or agreed to in writing, software
|
190
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
191
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
192
|
+
* See the License for the specific language governing permissions and
|
193
|
+
* limitations under the License.
|
194
|
+
*/
|
195
|
+
|
196
|
+
|
197
|
+
/**
|
198
|
+
* @typedef {Record<string,any>} FormatValues
|
199
|
+
*/
|
200
|
+
|
201
|
+
/**
|
202
|
+
* @callback ProcessFunction
|
203
|
+
* @param {string} message
|
204
|
+
* @param {FormatValues} [values={}]
|
205
|
+
* @return {any[]}
|
206
|
+
*/
|
207
|
+
|
208
|
+
/**
|
209
|
+
* @callback TypeHandler
|
210
|
+
* @param {any} value
|
211
|
+
* The object which matched the key of the block being processed.
|
212
|
+
* @param {string} matches
|
213
|
+
* Any format options associated with the block being processed.
|
214
|
+
* @param {string} locale
|
215
|
+
* The locale to use for formatting.
|
216
|
+
* @param {FormatValues} values
|
217
|
+
* The object of placeholder data given to the original `format`/`process`
|
218
|
+
* call.
|
219
|
+
* @param {ProcessFunction} process
|
220
|
+
* The `process` function itself so that sub-messages can be processed by type
|
221
|
+
* handlers.
|
222
|
+
* @return {any | any[]}
|
223
|
+
*/
|
224
|
+
|
225
|
+
/**
|
226
|
+
* The main class for formatting messages.
|
227
|
+
*
|
228
|
+
* @author Emanuel Rabina
|
229
|
+
*/
|
90
230
|
class MessageFormatter {
|
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
|
-
|
231
|
+
|
232
|
+
/**
|
233
|
+
* Creates a new formatter that can work using any of the custom type handlers
|
234
|
+
* you register.
|
235
|
+
*
|
236
|
+
* @param {string} locale
|
237
|
+
* @param {Record<string,TypeHandler>} [typeHandlers]
|
238
|
+
* Optional object where the keys are the names of the types to register,
|
239
|
+
* their values being the functions that will return a nicely formatted
|
240
|
+
* string for the data and locale they are given.
|
241
|
+
*/
|
242
|
+
constructor(locale, typeHandlers = {}) {
|
243
|
+
|
244
|
+
this.locale = locale;
|
245
|
+
this.typeHandlers = typeHandlers;
|
246
|
+
}
|
247
|
+
|
248
|
+
/**
|
249
|
+
* Formats an ICU message syntax string using `values` for placeholder data
|
250
|
+
* and any currently-registered type handlers.
|
251
|
+
*
|
252
|
+
* @type {(message: string, values?: FormatValues) => string}
|
253
|
+
*/
|
254
|
+
format = functionUtils.memoize((message, values = {}) => {
|
255
|
+
|
256
|
+
return this.process(message, values).flat(Infinity).join('');
|
257
|
+
});
|
258
|
+
|
259
|
+
/**
|
260
|
+
* Process an ICU message syntax string using `values` for placeholder data
|
261
|
+
* and any currently-registered type handlers. The result of this method is
|
262
|
+
* an array of the component parts after they have been processed in turn by
|
263
|
+
* their own type handlers. This raw output is useful for other renderers,
|
264
|
+
* eg: React where components can be used instead of being forced to return
|
265
|
+
* raw strings.
|
266
|
+
*
|
267
|
+
* This method is used by {@link MessageFormatter#format} where it acts as a
|
268
|
+
* string renderer.
|
269
|
+
*
|
270
|
+
* @param {string} message
|
271
|
+
* @param {FormatValues} [values]
|
272
|
+
* @return {any[]}
|
273
|
+
*/
|
274
|
+
process(message, values = {}) {
|
275
|
+
|
276
|
+
if (!message) {
|
277
|
+
return [];
|
278
|
+
}
|
279
|
+
|
280
|
+
let blockStartIndex = message.indexOf('{');
|
281
|
+
if (blockStartIndex !== -1) {
|
282
|
+
let blockEndIndex = findClosingBracket(message, blockStartIndex);
|
283
|
+
if (blockEndIndex !== -1) {
|
284
|
+
let block = message.substring(blockStartIndex, blockEndIndex + 1);
|
285
|
+
if (block) {
|
286
|
+
let result = [];
|
287
|
+
let head = message.substring(0, blockStartIndex);
|
288
|
+
if (head) {
|
289
|
+
result.push(head);
|
290
|
+
}
|
291
|
+
let [key, type, format] = splitFormattedArgument(block);
|
292
|
+
let body = values[key];
|
293
|
+
let typeHandler = type && this.typeHandlers[type];
|
294
|
+
result.push(typeHandler ?
|
295
|
+
typeHandler(body, format, this.locale, values, this.process.bind(this)) :
|
296
|
+
body);
|
297
|
+
let tail = message.substring(blockEndIndex + 1);
|
298
|
+
if (tail) {
|
299
|
+
result.push(this.process(tail, values));
|
300
|
+
}
|
301
|
+
return result;
|
302
|
+
}
|
303
|
+
}
|
304
|
+
else {
|
305
|
+
throw new Error(`Unbalanced curly braces in string: "${message}"`);
|
306
|
+
}
|
307
|
+
}
|
308
|
+
return [message];
|
309
|
+
}
|
163
310
|
}
|
311
|
+
|
312
|
+
/*
|
313
|
+
* Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)
|
314
|
+
*
|
315
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
316
|
+
* you may not use this file except in compliance with the License.
|
317
|
+
* You may obtain a copy of the License at
|
318
|
+
*
|
319
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
320
|
+
*
|
321
|
+
* Unless required by applicable law or agreed to in writing, software
|
322
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
323
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
324
|
+
* See the License for the specific language governing permissions and
|
325
|
+
* limitations under the License.
|
326
|
+
*/
|
327
|
+
|
328
|
+
|
164
329
|
let pluralFormatter;
|
330
|
+
|
165
331
|
let keyCounter = 0;
|
166
|
-
|
167
|
-
|
332
|
+
|
333
|
+
// All the special keywords that can be used in `plural` blocks for the various branches
|
334
|
+
const ONE = 'one';
|
335
|
+
const OTHER$1 = 'other';
|
336
|
+
|
337
|
+
/**
|
338
|
+
* @private
|
339
|
+
* @param {string} caseBody
|
340
|
+
* @param {number} value
|
341
|
+
* @return {{caseBody: string, numberValues: object}}
|
342
|
+
*/
|
168
343
|
function replaceNumberSign(caseBody, value) {
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
344
|
+
let i = 0;
|
345
|
+
let output = '';
|
346
|
+
let numBraces = 0;
|
347
|
+
const numberValues = {};
|
348
|
+
|
349
|
+
while (i < caseBody.length) {
|
350
|
+
if (caseBody[i] === '#' && !numBraces) {
|
351
|
+
let keyParam = `__hashToken${keyCounter++}`;
|
352
|
+
output += `{${keyParam}, number}`;
|
353
|
+
numberValues[keyParam] = value;
|
354
|
+
}
|
355
|
+
else {
|
356
|
+
output += caseBody[i];
|
357
|
+
}
|
358
|
+
|
359
|
+
if (caseBody[i] === '{') {
|
360
|
+
numBraces++;
|
361
|
+
}
|
362
|
+
else if (caseBody[i] === '}') {
|
363
|
+
numBraces--;
|
364
|
+
}
|
365
|
+
|
366
|
+
i++;
|
367
|
+
}
|
368
|
+
|
369
|
+
return {
|
370
|
+
caseBody: output,
|
371
|
+
numberValues
|
372
|
+
};
|
192
373
|
}
|
374
|
+
|
375
|
+
/**
|
376
|
+
* Handler for `plural` statements within ICU message syntax strings. Returns
|
377
|
+
* a formatted string for the branch that closely matches the current value.
|
378
|
+
*
|
379
|
+
* See https://formatjs.io/docs/core-concepts/icu-syntax#plural-format for more
|
380
|
+
* details on how the `plural` statement works.
|
381
|
+
*
|
382
|
+
* @param {string} value
|
383
|
+
* @param {string} matches
|
384
|
+
* @param {string} locale
|
385
|
+
* @param {import('./MessageFormatter.js').FormatValues} values
|
386
|
+
* @param {import('./MessageFormatter.js').ProcessFunction} process
|
387
|
+
* @return {any | any[]}
|
388
|
+
*/
|
193
389
|
function pluralTypeHandler(value, matches, locale, values, process) {
|
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
|
-
|
390
|
+
const {args, cases} = parseCases(matches);
|
391
|
+
|
392
|
+
let intValue = parseInt(value);
|
393
|
+
|
394
|
+
args.forEach((arg) => {
|
395
|
+
if (arg.startsWith('offset:')) {
|
396
|
+
intValue -= parseInt(arg.slice('offset:'.length));
|
397
|
+
}
|
398
|
+
});
|
399
|
+
|
400
|
+
const keywordPossibilities = [];
|
401
|
+
|
402
|
+
if ('PluralRules' in Intl) {
|
403
|
+
// Effectively memoize because instantiation of `Int.*` objects is expensive.
|
404
|
+
if (pluralFormatter === undefined || pluralFormatter.resolvedOptions().locale !== locale) {
|
405
|
+
pluralFormatter = new Intl.PluralRules(locale);
|
406
|
+
}
|
407
|
+
|
408
|
+
const pluralKeyword = pluralFormatter.select(intValue);
|
409
|
+
|
410
|
+
// Other is always added last with least priority, so we don't want to add it here.
|
411
|
+
if (pluralKeyword !== OTHER$1) {
|
412
|
+
keywordPossibilities.push(pluralKeyword);
|
413
|
+
}
|
414
|
+
}
|
415
|
+
if (intValue === 1) {
|
416
|
+
keywordPossibilities.push(ONE);
|
417
|
+
}
|
418
|
+
keywordPossibilities.push(`=${intValue}`, OTHER$1);
|
419
|
+
|
420
|
+
for (let i = 0; i < keywordPossibilities.length; i++) {
|
421
|
+
const keyword = keywordPossibilities[i];
|
422
|
+
if (keyword in cases) {
|
423
|
+
const {caseBody, numberValues} = replaceNumberSign(cases[keyword], intValue);
|
424
|
+
return process(caseBody, {
|
425
|
+
...values,
|
426
|
+
...numberValues
|
427
|
+
});
|
428
|
+
}
|
429
|
+
}
|
430
|
+
|
431
|
+
return value;
|
226
432
|
}
|
227
|
-
|
433
|
+
|
434
|
+
/*
|
435
|
+
* Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)
|
436
|
+
*
|
437
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
438
|
+
* you may not use this file except in compliance with the License.
|
439
|
+
* You may obtain a copy of the License at
|
440
|
+
*
|
441
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
442
|
+
*
|
443
|
+
* Unless required by applicable law or agreed to in writing, software
|
444
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
445
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
446
|
+
* See the License for the specific language governing permissions and
|
447
|
+
* limitations under the License.
|
448
|
+
*/
|
449
|
+
|
450
|
+
|
451
|
+
const OTHER = 'other';
|
452
|
+
|
453
|
+
/**
|
454
|
+
* Handler for `select` statements within ICU message syntax strings. Returns
|
455
|
+
* a formatted string for the branch that closely matches the current value.
|
456
|
+
*
|
457
|
+
* See https://formatjs.io/docs/core-concepts/icu-syntax#select-format for more
|
458
|
+
* details on how the `select` statement works.
|
459
|
+
*
|
460
|
+
* @param {string} value
|
461
|
+
* @param {string} matches
|
462
|
+
* @param {string} locale
|
463
|
+
* @param {import('./MessageFormatter.js').FormatValues} values
|
464
|
+
* @param {import('./MessageFormatter.js').ProcessFunction} process
|
465
|
+
* @return {any | any[]}
|
466
|
+
*/
|
228
467
|
function selectTypeHandler(value, matches, locale, values, process) {
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
468
|
+
const {cases} = parseCases(matches);
|
469
|
+
|
470
|
+
if (value in cases) {
|
471
|
+
return process(cases[value], values);
|
472
|
+
}
|
473
|
+
else if (OTHER in cases) {
|
474
|
+
return process(cases[OTHER], values);
|
475
|
+
}
|
476
|
+
|
477
|
+
return value;
|
236
478
|
}
|
479
|
+
|
237
480
|
exports.MessageFormatter = MessageFormatter;
|
238
481
|
exports.findClosingBracket = findClosingBracket;
|
239
482
|
exports.parseCases = parseCases;
|