@ultraq/icu-message-formatter 0.12.0 → 0.14.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.
Files changed (32) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +5 -6
  3. package/dist/icu-message-formatter.browser.es.min.js +2 -0
  4. package/dist/icu-message-formatter.browser.es.min.js.map +1 -0
  5. package/dist/icu-message-formatter.browser.min.js +2 -0
  6. package/dist/icu-message-formatter.browser.min.js.map +1 -0
  7. package/dist/icu-message-formatter.cjs +473 -0
  8. package/dist/icu-message-formatter.cjs.map +1 -0
  9. package/dist/icu-message-formatter.d.cts +153 -0
  10. package/dist/icu-message-formatter.d.ts +153 -0
  11. package/dist/icu-message-formatter.js +466 -0
  12. package/dist/icu-message-formatter.js.map +1 -0
  13. package/package.json +43 -26
  14. package/dist/icu-message-formatter.es.min.js +0 -2
  15. package/dist/icu-message-formatter.es.min.js.map +0 -1
  16. package/dist/icu-message-formatter.min.js +0 -2
  17. package/dist/icu-message-formatter.min.js.map +0 -1
  18. package/lib/icu-message-formatter.cjs.js +0 -476
  19. package/lib/icu-message-formatter.cjs.js.map +0 -1
  20. package/lib/icu-message-formatter.es.js +0 -460
  21. package/lib/icu-message-formatter.es.js.map +0 -1
  22. package/rollup.config.dist.js +0 -37
  23. package/rollup.config.js +0 -35
  24. package/source/IcuMessageFormatter.js +0 -9
  25. package/source/MessageFormatter.js +0 -112
  26. package/source/MessageFormatter.test.js +0 -153
  27. package/source/pluralTypeHandler.js +0 -122
  28. package/source/pluralTypeHandler.test.js +0 -188
  29. package/source/selectTypeHandler.js +0 -46
  30. package/source/selectTypeHandler.test.js +0 -59
  31. package/source/utilities.js +0 -166
  32. package/source/utilities.test.js +0 -123
@@ -0,0 +1,466 @@
1
+ import { memoize } from '@ultraq/function-utils';
2
+
3
+ /*
4
+ * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ /**
20
+ * @typedef ParseCasesResult
21
+ * @property {string[]} args
22
+ * A list of prepended arguments.
23
+ * @property {Record<string,string>} cases
24
+ * A map of all cases.
25
+ */
26
+
27
+ /**
28
+ * Most branch-based type handlers are based around "cases". For example,
29
+ * `select` and `plural` compare compare a value to "case keys" to choose a
30
+ * subtranslation.
31
+ *
32
+ * This util splits "matches" portions provided to the aforementioned handlers
33
+ * into case strings, and extracts any prepended arguments (for example,
34
+ * `plural` supports an `offset:n` argument used for populating the magic `#`
35
+ * variable).
36
+ *
37
+ * @param {string} string
38
+ * @return {ParseCasesResult}
39
+ */
40
+ function parseCases(string) {
41
+ const isWhitespace = ch => /\s/.test(ch);
42
+ const args = [];
43
+ const cases = {};
44
+ let currTermStart = 0;
45
+ let latestTerm = null;
46
+ let inTerm = false;
47
+ let i = 0;
48
+ while (i < string.length) {
49
+ // Term ended
50
+ if (inTerm && (isWhitespace(string[i]) || string[i] === '{')) {
51
+ inTerm = false;
52
+ latestTerm = string.slice(currTermStart, i);
53
+
54
+ // We want to process the opening char again so the case will be properly registered.
55
+ if (string[i] === '{') {
56
+ i--;
57
+ }
58
+ }
59
+
60
+ // New term
61
+ else if (!inTerm && !isWhitespace(string[i])) {
62
+ const caseBody = string[i] === '{';
63
+
64
+ // If there's a previous term, we can either handle a whole
65
+ // case, or add that as an argument.
66
+ if (latestTerm && caseBody) {
67
+ const branchEndIndex = findClosingBracket(string, i);
68
+ if (branchEndIndex === -1) {
69
+ throw new Error(`Unbalanced curly braces in string: "${string}"`);
70
+ }
71
+ cases[latestTerm] = string.slice(i + 1, branchEndIndex); // Don't include the braces
72
+
73
+ i = branchEndIndex; // Will be moved up where needed at end of loop.
74
+ latestTerm = null;
75
+ } else {
76
+ if (latestTerm) {
77
+ args.push(latestTerm);
78
+ latestTerm = null;
79
+ }
80
+ inTerm = true;
81
+ currTermStart = i;
82
+ }
83
+ }
84
+ i++;
85
+ }
86
+ if (inTerm) {
87
+ latestTerm = string.slice(currTermStart);
88
+ }
89
+ if (latestTerm) {
90
+ args.push(latestTerm);
91
+ }
92
+ return {
93
+ args,
94
+ cases
95
+ };
96
+ }
97
+
98
+ /**
99
+ * Finds the index of the matching closing curly bracket, including through
100
+ * strings that could have nested brackets.
101
+ *
102
+ * @param {string} string
103
+ * @param {number} fromIndex
104
+ * @return {number}
105
+ * The index of the matching closing bracket, or -1 if no closing bracket
106
+ * could be found.
107
+ */
108
+ function findClosingBracket(string, fromIndex) {
109
+ let depth = 0;
110
+ for (let i = fromIndex + 1; i < string.length; i++) {
111
+ let char = string.charAt(i);
112
+ if (char === '}') {
113
+ if (depth === 0) {
114
+ return i;
115
+ }
116
+ depth--;
117
+ } else if (char === '{') {
118
+ depth++;
119
+ }
120
+ }
121
+ return -1;
122
+ }
123
+
124
+ /**
125
+ * Split a `{key, type, format}` block into those 3 parts, taking into account
126
+ * nested message syntax that can exist in the `format` part.
127
+ *
128
+ * @param {string} block
129
+ * @return {string[]}
130
+ * An array with `key`, `type`, and `format` items in that order, if present
131
+ * in the formatted argument block.
132
+ */
133
+ function splitFormattedArgument(block) {
134
+ return split(block.slice(1, -1), ',', 3);
135
+ }
136
+
137
+ /**
138
+ * Like `String.prototype.split()` but where the limit parameter causes the
139
+ * remainder of the string to be grouped together in a final entry.
140
+ *
141
+ * @private
142
+ * @param {string} string
143
+ * @param {string} separator
144
+ * @param {number} limit
145
+ * @param {string[]} accumulator
146
+ * @return {string[]}
147
+ */
148
+ function split(string, separator, limit) {
149
+ let accumulator = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
150
+ if (!string) {
151
+ return accumulator;
152
+ }
153
+ if (limit === 1) {
154
+ accumulator.push(string);
155
+ return accumulator;
156
+ }
157
+ let indexOfDelimiter = string.indexOf(separator);
158
+ if (indexOfDelimiter === -1) {
159
+ accumulator.push(string);
160
+ return accumulator;
161
+ }
162
+ let head = string.substring(0, indexOfDelimiter).trim();
163
+ let tail = string.substring(indexOfDelimiter + separator.length + 1).trim();
164
+ accumulator.push(head);
165
+ return split(tail, separator, limit - 1, accumulator);
166
+ }
167
+
168
+ /*
169
+ * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)
170
+ *
171
+ * Licensed under the Apache License, Version 2.0 (the "License");
172
+ * you may not use this file except in compliance with the License.
173
+ * You may obtain a copy of the License at
174
+ *
175
+ * http://www.apache.org/licenses/LICENSE-2.0
176
+ *
177
+ * Unless required by applicable law or agreed to in writing, software
178
+ * distributed under the License is distributed on an "AS IS" BASIS,
179
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
180
+ * See the License for the specific language governing permissions and
181
+ * limitations under the License.
182
+ */
183
+
184
+
185
+ /**
186
+ * @typedef {Record<string,any>} FormatValues
187
+ */
188
+
189
+ /**
190
+ * @callback ProcessFunction
191
+ * @param {string} message
192
+ * @param {FormatValues} [values={}]
193
+ * @return {any[]}
194
+ */
195
+
196
+ /**
197
+ * @callback TypeHandler
198
+ * @param {any} value
199
+ * The object which matched the key of the block being processed.
200
+ * @param {string} matches
201
+ * Any format options associated with the block being processed.
202
+ * @param {string} locale
203
+ * The locale to use for formatting.
204
+ * @param {FormatValues} values
205
+ * The object of placeholder data given to the original `format`/`process`
206
+ * call.
207
+ * @param {ProcessFunction} process
208
+ * The `process` function itself so that sub-messages can be processed by type
209
+ * handlers.
210
+ * @return {any | any[]}
211
+ */
212
+
213
+ /**
214
+ * The main class for formatting messages.
215
+ *
216
+ * @author Emanuel Rabina
217
+ */
218
+ class MessageFormatter {
219
+ /**
220
+ * Creates a new formatter that can work using any of the custom type handlers
221
+ * you register.
222
+ *
223
+ * @param {string} locale
224
+ * @param {Record<string,TypeHandler>} [typeHandlers]
225
+ * Optional object where the keys are the names of the types to register,
226
+ * their values being the functions that will return a nicely formatted
227
+ * string for the data and locale they are given.
228
+ */
229
+ constructor(locale) {
230
+ let typeHandlers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
231
+ this.locale = locale;
232
+ this.typeHandlers = typeHandlers;
233
+ }
234
+
235
+ /**
236
+ * Formats an ICU message syntax string using `values` for placeholder data
237
+ * and any currently-registered type handlers.
238
+ *
239
+ * @type {(message: string, values?: FormatValues) => string}
240
+ */
241
+ format = memoize((() => {
242
+ var _this = this;
243
+ return function (message) {
244
+ let values = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
245
+ return _this.process(message, values).flat(Infinity).join('');
246
+ };
247
+ })());
248
+
249
+ /**
250
+ * Process an ICU message syntax string using `values` for placeholder data
251
+ * and any currently-registered type handlers. The result of this method is
252
+ * an array of the component parts after they have been processed in turn by
253
+ * their own type handlers. This raw output is useful for other renderers,
254
+ * eg: React where components can be used instead of being forced to return
255
+ * raw strings.
256
+ *
257
+ * This method is used by {@link MessageFormatter#format} where it acts as a
258
+ * string renderer.
259
+ *
260
+ * @param {string} message
261
+ * @param {FormatValues} [values]
262
+ * @return {any[]}
263
+ */
264
+ process(message) {
265
+ let values = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
266
+ if (!message) {
267
+ return [];
268
+ }
269
+ let blockStartIndex = message.indexOf('{');
270
+ if (blockStartIndex !== -1) {
271
+ let blockEndIndex = findClosingBracket(message, blockStartIndex);
272
+ if (blockEndIndex !== -1) {
273
+ let block = message.substring(blockStartIndex, blockEndIndex + 1);
274
+ if (block) {
275
+ let result = [];
276
+ let head = message.substring(0, blockStartIndex);
277
+ if (head) {
278
+ result.push(head);
279
+ }
280
+ let [key, type, format] = splitFormattedArgument(block);
281
+ let body = values[key];
282
+ if (body === null || body === undefined) {
283
+ body = '';
284
+ }
285
+ let typeHandler = type && this.typeHandlers[type];
286
+ result.push(typeHandler ? typeHandler(body, format, this.locale, values, this.process.bind(this)) : body);
287
+ let tail = message.substring(blockEndIndex + 1);
288
+ if (tail) {
289
+ result.push(this.process(tail, values));
290
+ }
291
+ return result;
292
+ }
293
+ } else {
294
+ throw new Error(`Unbalanced curly braces in string: "${message}"`);
295
+ }
296
+ }
297
+ return [message];
298
+ }
299
+ }
300
+
301
+ /*
302
+ * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)
303
+ *
304
+ * Licensed under the Apache License, Version 2.0 (the "License");
305
+ * you may not use this file except in compliance with the License.
306
+ * You may obtain a copy of the License at
307
+ *
308
+ * http://www.apache.org/licenses/LICENSE-2.0
309
+ *
310
+ * Unless required by applicable law or agreed to in writing, software
311
+ * distributed under the License is distributed on an "AS IS" BASIS,
312
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
313
+ * See the License for the specific language governing permissions and
314
+ * limitations under the License.
315
+ */
316
+
317
+ let pluralFormatter;
318
+ let keyCounter = 0;
319
+
320
+ // All the special keywords that can be used in `plural` blocks for the various branches
321
+ const ONE = 'one';
322
+ const OTHER$1 = 'other';
323
+
324
+ /**
325
+ * @private
326
+ * @param {string} caseBody
327
+ * @param {number} value
328
+ * @return {{caseBody: string, numberValues: object}}
329
+ */
330
+ function replaceNumberSign(caseBody, value) {
331
+ let i = 0;
332
+ let output = '';
333
+ let numBraces = 0;
334
+ const numberValues = {};
335
+ while (i < caseBody.length) {
336
+ if (caseBody[i] === '#' && !numBraces) {
337
+ let keyParam = `__hashToken${keyCounter++}`;
338
+ output += `{${keyParam}, number}`;
339
+ numberValues[keyParam] = value;
340
+ } else {
341
+ output += caseBody[i];
342
+ }
343
+ if (caseBody[i] === '{') {
344
+ numBraces++;
345
+ } else if (caseBody[i] === '}') {
346
+ numBraces--;
347
+ }
348
+ i++;
349
+ }
350
+ return {
351
+ caseBody: output,
352
+ numberValues
353
+ };
354
+ }
355
+
356
+ /**
357
+ * Handler for `plural` statements within ICU message syntax strings. Returns
358
+ * a formatted string for the branch that closely matches the current value.
359
+ *
360
+ * See https://formatjs.io/docs/core-concepts/icu-syntax#plural-format for more
361
+ * details on how the `plural` statement works.
362
+ *
363
+ * @param {string} value
364
+ * @param {string} matches
365
+ * @param {string} locale
366
+ * @param {Record<string,any>} values
367
+ * @param {(message: string, values?: Record<string,any>) => any[]} process
368
+ * @return {any | any[]}
369
+ */
370
+ function pluralTypeHandler(value) {
371
+ let matches = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
372
+ let locale = arguments.length > 2 ? arguments[2] : undefined;
373
+ let values = arguments.length > 3 ? arguments[3] : undefined;
374
+ let process = arguments.length > 4 ? arguments[4] : undefined;
375
+ const {
376
+ args,
377
+ cases
378
+ } = parseCases(matches);
379
+ let intValue = parseInt(value);
380
+ args.forEach(arg => {
381
+ if (arg.startsWith('offset:')) {
382
+ intValue -= parseInt(arg.slice('offset:'.length));
383
+ }
384
+ });
385
+ const keywordPossibilities = [];
386
+ if ('PluralRules' in Intl) {
387
+ // Effectively memoize because instantiation of `Int.*` objects is expensive.
388
+ if (pluralFormatter === undefined || pluralFormatter.resolvedOptions().locale !== locale) {
389
+ pluralFormatter = new Intl.PluralRules(locale);
390
+ }
391
+ const pluralKeyword = pluralFormatter.select(intValue);
392
+
393
+ // Other is always added last with least priority, so we don't want to add it here.
394
+ if (pluralKeyword !== OTHER$1) {
395
+ keywordPossibilities.push(pluralKeyword);
396
+ }
397
+ }
398
+ if (intValue === 1) {
399
+ keywordPossibilities.push(ONE);
400
+ }
401
+ keywordPossibilities.push(`=${intValue}`, OTHER$1);
402
+ for (let i = 0; i < keywordPossibilities.length; i++) {
403
+ const keyword = keywordPossibilities[i];
404
+ if (keyword in cases) {
405
+ const {
406
+ caseBody,
407
+ numberValues
408
+ } = replaceNumberSign(cases[keyword], intValue);
409
+ return process(caseBody, {
410
+ ...values,
411
+ ...numberValues
412
+ });
413
+ }
414
+ }
415
+ return value;
416
+ }
417
+
418
+ /*
419
+ * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)
420
+ *
421
+ * Licensed under the Apache License, Version 2.0 (the "License");
422
+ * you may not use this file except in compliance with the License.
423
+ * You may obtain a copy of the License at
424
+ *
425
+ * http://www.apache.org/licenses/LICENSE-2.0
426
+ *
427
+ * Unless required by applicable law or agreed to in writing, software
428
+ * distributed under the License is distributed on an "AS IS" BASIS,
429
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
430
+ * See the License for the specific language governing permissions and
431
+ * limitations under the License.
432
+ */
433
+
434
+ const OTHER = 'other';
435
+
436
+ /**
437
+ * Handler for `select` statements within ICU message syntax strings. Returns
438
+ * a formatted string for the branch that closely matches the current value.
439
+ *
440
+ * See https://formatjs.io/docs/core-concepts/icu-syntax#select-format for more
441
+ * details on how the `select` statement works.
442
+ *
443
+ * @param {string} value
444
+ * @param {string} matches
445
+ * @param {string} locale
446
+ * @param {Record<string,any>} values
447
+ * @param {(message: string, values?: Record<string,any>) => any[]} process
448
+ * @return {any | any[]}
449
+ */
450
+ function selectTypeHandler(value) {
451
+ let matches = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
452
+ let values = arguments.length > 3 ? arguments[3] : undefined;
453
+ let process = arguments.length > 4 ? arguments[4] : undefined;
454
+ const {
455
+ cases
456
+ } = parseCases(matches);
457
+ if (value in cases) {
458
+ return process(cases[value], values);
459
+ } else if (OTHER in cases) {
460
+ return process(cases[OTHER], values);
461
+ }
462
+ return value;
463
+ }
464
+
465
+ export { MessageFormatter, findClosingBracket, parseCases, pluralTypeHandler, selectTypeHandler, splitFormattedArgument };
466
+ //# sourceMappingURL=icu-message-formatter.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"icu-message-formatter.js","sources":["../source/utilities.js","../source/MessageFormatter.js","../source/pluralTypeHandler.js","../source/selectTypeHandler.js"],"sourcesContent":["/* \n * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)\n * \n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * \n * http://www.apache.org/licenses/LICENSE-2.0\n * \n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * @typedef ParseCasesResult\n * @property {string[]} args\n * A list of prepended arguments.\n * @property {Record<string,string>} cases\n * A map of all cases.\n */\n\n/**\n * Most branch-based type handlers are based around \"cases\". For example,\n * `select` and `plural` compare compare a value to \"case keys\" to choose a\n * subtranslation.\n * \n * This util splits \"matches\" portions provided to the aforementioned handlers\n * into case strings, and extracts any prepended arguments (for example,\n * `plural` supports an `offset:n` argument used for populating the magic `#`\n * variable).\n * \n * @param {string} string\n * @return {ParseCasesResult}\n */\nexport function parseCases(string) {\n\tconst isWhitespace = ch => /\\s/.test(ch);\n\n\tconst args = [];\n\tconst cases = {};\n\n\tlet currTermStart = 0;\n\tlet latestTerm = null;\n\tlet inTerm = false;\n\n\tlet i = 0;\n\twhile (i < string.length) {\n\t\t// Term ended\n\t\tif (inTerm && (isWhitespace(string[i]) || string[i] === '{')) {\n\t\t\tinTerm = false;\n\t\t\tlatestTerm = string.slice(currTermStart, i);\n\n\t\t\t// We want to process the opening char again so the case will be properly registered.\n\t\t\tif (string[i] === '{') {\n\t\t\t\ti--;\n\t\t\t}\n\t\t}\n\n\t\t// New term\n\t\telse if (!inTerm && !isWhitespace(string[i])) {\n\t\t\tconst caseBody = string[i] === '{';\n\n\t\t\t// If there's a previous term, we can either handle a whole\n\t\t\t// case, or add that as an argument.\n\t\t\tif (latestTerm && caseBody) {\n\t\t\t\tconst branchEndIndex = findClosingBracket(string, i);\n\n\t\t\t\tif (branchEndIndex === -1) {\n\t\t\t\t\tthrow new Error(`Unbalanced curly braces in string: \"${string}\"`);\n\t\t\t\t}\n\n\t\t\t\tcases[latestTerm] = string.slice(i + 1, branchEndIndex); // Don't include the braces\n\n\t\t\t\ti = branchEndIndex; // Will be moved up where needed at end of loop.\n\t\t\t\tlatestTerm = null;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tif (latestTerm) {\n\t\t\t\t\targs.push(latestTerm);\n\t\t\t\t\tlatestTerm = null;\n\t\t\t\t}\n\n\t\t\t\tinTerm = true;\n\t\t\t\tcurrTermStart = i;\n\t\t\t}\n\t\t}\n\t\ti++;\n\t}\n\n\tif (inTerm) {\n\t\tlatestTerm = string.slice(currTermStart);\n\t}\n\n\tif (latestTerm) {\n\t\targs.push(latestTerm);\n\t}\n\n\treturn {\n\t\targs,\n\t\tcases\n\t};\n}\n\n/**\n * Finds the index of the matching closing curly bracket, including through\n * strings that could have nested brackets.\n * \n * @param {string} string\n * @param {number} fromIndex\n * @return {number}\n * The index of the matching closing bracket, or -1 if no closing bracket\n * could be found.\n */\nexport function findClosingBracket(string, fromIndex) {\n\tlet depth = 0;\n\tfor (let i = fromIndex + 1; i < string.length; i++) {\n\t\tlet char = string.charAt(i);\n\t\tif (char === '}') {\n\t\t\tif (depth === 0) {\n\t\t\t\treturn i;\n\t\t\t}\n\t\t\tdepth--;\n\t\t}\n\t\telse if (char === '{') {\n\t\t\tdepth++;\n\t\t}\n\t}\n\treturn -1;\n}\n\n/**\n * Split a `{key, type, format}` block into those 3 parts, taking into account\n * nested message syntax that can exist in the `format` part.\n * \n * @param {string} block\n * @return {string[]}\n * An array with `key`, `type`, and `format` items in that order, if present\n * in the formatted argument block.\n */\nexport function splitFormattedArgument(block) {\n\treturn split(block.slice(1, -1), ',', 3);\n}\n\n/**\n * Like `String.prototype.split()` but where the limit parameter causes the\n * remainder of the string to be grouped together in a final entry.\n * \n * @private\n * @param {string} string\n * @param {string} separator\n * @param {number} limit\n * @param {string[]} accumulator\n * @return {string[]}\n */\nfunction split(string, separator, limit, accumulator = []) {\n\tif (!string) {\n\t\treturn accumulator;\n\t}\n\tif (limit === 1) {\n\t\taccumulator.push(string);\n\t\treturn accumulator;\n\t}\n\tlet indexOfDelimiter = string.indexOf(separator);\n\tif (indexOfDelimiter === -1) {\n\t\taccumulator.push(string);\n\t\treturn accumulator;\n\t}\n\tlet head = string.substring(0, indexOfDelimiter).trim();\n\tlet tail = string.substring(indexOfDelimiter + separator.length + 1).trim();\n\taccumulator.push(head);\n\treturn split(tail, separator, limit - 1, accumulator);\n}\n","/*\n * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {findClosingBracket, splitFormattedArgument} from './utilities.js';\n\nimport {memoize} from '@ultraq/function-utils';\n\n/**\n * @typedef {Record<string,any>} FormatValues\n */\n\n/**\n * @callback ProcessFunction\n * @param {string} message\n * @param {FormatValues} [values={}]\n * @return {any[]}\n */\n\n/**\n * @callback TypeHandler\n * @param {any} value\n * The object which matched the key of the block being processed.\n * @param {string} matches\n * Any format options associated with the block being processed.\n * @param {string} locale\n * The locale to use for formatting.\n * @param {FormatValues} values\n * The object of placeholder data given to the original `format`/`process`\n * call.\n * @param {ProcessFunction} process\n * The `process` function itself so that sub-messages can be processed by type\n * handlers.\n * @return {any | any[]}\n */\n\n/**\n * The main class for formatting messages.\n *\n * @author Emanuel Rabina\n */\nexport default class MessageFormatter {\n\n\t/**\n\t * Creates a new formatter that can work using any of the custom type handlers\n\t * you register.\n\t *\n\t * @param {string} locale\n\t * @param {Record<string,TypeHandler>} [typeHandlers]\n\t * Optional object where the keys are the names of the types to register,\n\t * their values being the functions that will return a nicely formatted\n\t * string for the data and locale they are given.\n\t */\n\tconstructor(locale, typeHandlers = {}) {\n\n\t\tthis.locale = locale;\n\t\tthis.typeHandlers = typeHandlers;\n\t}\n\n\t/**\n\t * Formats an ICU message syntax string using `values` for placeholder data\n\t * and any currently-registered type handlers.\n\t *\n\t * @type {(message: string, values?: FormatValues) => string}\n\t */\n\tformat = memoize((message, values = {}) => {\n\n\t\treturn this.process(message, values).flat(Infinity).join('');\n\t});\n\n\t/**\n\t * Process an ICU message syntax string using `values` for placeholder data\n\t * and any currently-registered type handlers. The result of this method is\n\t * an array of the component parts after they have been processed in turn by\n\t * their own type handlers. This raw output is useful for other renderers,\n\t * eg: React where components can be used instead of being forced to return\n\t * raw strings.\n\t *\n\t * This method is used by {@link MessageFormatter#format} where it acts as a\n\t * string renderer.\n\t *\n\t * @param {string} message\n\t * @param {FormatValues} [values]\n\t * @return {any[]}\n\t */\n\tprocess(message, values = {}) {\n\n\t\tif (!message) {\n\t\t\treturn [];\n\t\t}\n\n\t\tlet blockStartIndex = message.indexOf('{');\n\t\tif (blockStartIndex !== -1) {\n\t\t\tlet blockEndIndex = findClosingBracket(message, blockStartIndex);\n\t\t\tif (blockEndIndex !== -1) {\n\t\t\t\tlet block = message.substring(blockStartIndex, blockEndIndex + 1);\n\t\t\t\tif (block) {\n\t\t\t\t\tlet result = [];\n\t\t\t\t\tlet head = message.substring(0, blockStartIndex);\n\t\t\t\t\tif (head) {\n\t\t\t\t\t\tresult.push(head);\n\t\t\t\t\t}\n\t\t\t\t\tlet [key, type, format] = splitFormattedArgument(block);\n\t\t\t\t\tlet body = values[key];\n\t\t\t\t\tif (body === null || body === undefined) {\n\t\t\t\t\t\tbody = '';\n\t\t\t\t\t}\n\t\t\t\t\tlet typeHandler = type && this.typeHandlers[type];\n\t\t\t\t\tresult.push(typeHandler ?\n\t\t\t\t\t\ttypeHandler(body, format, this.locale, values, this.process.bind(this)) :\n\t\t\t\t\t\tbody);\n\t\t\t\t\tlet tail = message.substring(blockEndIndex + 1);\n\t\t\t\t\tif (tail) {\n\t\t\t\t\t\tresult.push(this.process(tail, values));\n\t\t\t\t\t}\n\t\t\t\t\treturn result;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthrow new Error(`Unbalanced curly braces in string: \"${message}\"`);\n\t\t\t}\n\t\t}\n\t\treturn [message];\n\t}\n}\n","/* \n * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)\n * \n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * \n * http://www.apache.org/licenses/LICENSE-2.0\n * \n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {parseCases} from './utilities.js';\n\nlet pluralFormatter;\n\nlet keyCounter = 0;\n\n// All the special keywords that can be used in `plural` blocks for the various branches\nconst ONE = 'one';\nconst OTHER = 'other';\n\n/**\n * @private\n * @param {string} caseBody\n * @param {number} value\n * @return {{caseBody: string, numberValues: object}}\n */\nfunction replaceNumberSign(caseBody, value) {\n\tlet i = 0;\n\tlet output = '';\n\tlet numBraces = 0;\n\tconst numberValues = {};\n\n\twhile (i < caseBody.length) {\n\t\tif (caseBody[i] === '#' && !numBraces) {\n\t\t\tlet keyParam = `__hashToken${keyCounter++}`;\n\t\t\toutput += `{${keyParam}, number}`;\n\t\t\tnumberValues[keyParam] = value;\n\t\t}\n\t\telse {\n\t\t\toutput += caseBody[i];\n\t\t}\n\n\t\tif (caseBody[i] === '{') {\n\t\t\tnumBraces++;\n\t\t}\n\t\telse if (caseBody[i] === '}') {\n\t\t\tnumBraces--;\n\t\t}\n\n\t\ti++;\n\t}\n\n\treturn {\n\t\tcaseBody: output,\n\t\tnumberValues\n\t};\n}\n\n/**\n * Handler for `plural` statements within ICU message syntax strings. Returns\n * a formatted string for the branch that closely matches the current value.\n * \n * See https://formatjs.io/docs/core-concepts/icu-syntax#plural-format for more\n * details on how the `plural` statement works.\n *\n * @param {string} value\n * @param {string} matches\n * @param {string} locale\n * @param {Record<string,any>} values\n * @param {(message: string, values?: Record<string,any>) => any[]} process\n * @return {any | any[]}\n */\nexport default function pluralTypeHandler(value, matches = '', locale, values, process) {\n\tconst {args, cases} = parseCases(matches);\n\n\tlet intValue = parseInt(value);\n\n\targs.forEach((arg) => {\n\t\tif (arg.startsWith('offset:')) {\n\t\t\tintValue -= parseInt(arg.slice('offset:'.length));\n\t\t}\n\t});\n\n\tconst keywordPossibilities = [];\n\n\tif ('PluralRules' in Intl) {\n\t\t// Effectively memoize because instantiation of `Int.*` objects is expensive.\n\t\tif (pluralFormatter === undefined || pluralFormatter.resolvedOptions().locale !== locale) {\n\t\t\tpluralFormatter = new Intl.PluralRules(locale);\n\t\t}\n\n\t\tconst pluralKeyword = pluralFormatter.select(intValue);\n\n\t\t// Other is always added last with least priority, so we don't want to add it here.\n\t\tif (pluralKeyword !== OTHER) {\n\t\t\tkeywordPossibilities.push(pluralKeyword);\n\t\t}\n\t}\n\tif (intValue === 1) {\n\t\tkeywordPossibilities.push(ONE);\n\t}\n\tkeywordPossibilities.push(`=${intValue}`, OTHER);\n\n\tfor (let i = 0; i < keywordPossibilities.length; i++) {\n\t\tconst keyword = keywordPossibilities[i];\n\t\tif (keyword in cases) {\n\t\t\tconst {caseBody, numberValues} = replaceNumberSign(cases[keyword], intValue);\n\t\t\treturn process(caseBody, {\n\t\t\t\t...values,\n\t\t\t\t...numberValues\n\t\t\t});\n\t\t}\n\t}\n\n\treturn value;\n}\n","/* \n * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)\n * \n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n * \n * http://www.apache.org/licenses/LICENSE-2.0\n * \n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {parseCases} from './utilities.js';\n\nconst OTHER = 'other';\n\n/**\n * Handler for `select` statements within ICU message syntax strings. Returns\n * a formatted string for the branch that closely matches the current value.\n * \n * See https://formatjs.io/docs/core-concepts/icu-syntax#select-format for more\n * details on how the `select` statement works.\n * \n * @param {string} value\n * @param {string} matches\n * @param {string} locale\n * @param {Record<string,any>} values\n * @param {(message: string, values?: Record<string,any>) => any[]} process\n * @return {any | any[]}\n */\nexport default function selectTypeHandler(value, matches = '', locale, values, process) {\n\tconst {cases} = parseCases(matches);\n\n\tif (value in cases) {\n\t\treturn process(cases[value], values);\n\t}\n\telse if (OTHER in cases) {\n\t\treturn process(cases[OTHER], values);\n\t}\n\n\treturn value;\n}\n"],"names":["parseCases","string","isWhitespace","ch","test","args","cases","currTermStart","latestTerm","inTerm","i","length","slice","caseBody","branchEndIndex","findClosingBracket","Error","push","fromIndex","depth","char","charAt","splitFormattedArgument","block","split","separator","limit","accumulator","arguments","undefined","indexOfDelimiter","indexOf","head","substring","trim","tail","MessageFormatter","constructor","locale","typeHandlers","format","memoize","_this","message","values","process","flat","Infinity","join","blockStartIndex","blockEndIndex","result","key","type","body","typeHandler","bind","pluralFormatter","keyCounter","ONE","OTHER","replaceNumberSign","value","output","numBraces","numberValues","keyParam","pluralTypeHandler","matches","intValue","parseInt","forEach","arg","startsWith","keywordPossibilities","Intl","resolvedOptions","PluralRules","pluralKeyword","select","keyword","selectTypeHandler"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,UAAUA,CAACC,MAAM,EAAE;EAClC,MAAMC,YAAY,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AAIN,KAAK,IAAIxD,KAAK,EAAE;IACnB,OAAOuC,OAAO,CAACvC,KAAK,CAACwD,KAAK,CAAC,EAAElB,MAAM,CAAC,CAAA;AACrC,GAAC,MACI,IAAIgB,KAAK,IAAItD,KAAK,EAAE;IACxB,OAAOuC,OAAO,CAACvC,KAAK,CAACsD,KAAK,CAAC,EAAEhB,MAAM,CAAC,CAAA;AACrC,GAAA;AAEA,EAAA,OAAOkB,KAAK,CAAA;AACb;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultraq/icu-message-formatter",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "description": "Format ICU message syntax strings from supplied parameters and your own configurable types",
5
5
  "author": "Emanuel Rabina <emanuelrabina@gmail.com> (http://www.ultraq.net.nz/)",
6
6
  "license": "Apache-2.0",
@@ -15,41 +15,58 @@
15
15
  "intl",
16
16
  "i18n"
17
17
  ],
18
- "module": "lib/icu-message-formatter.es.js",
19
- "main": "lib/icu-message-formatter.cjs.js",
18
+ "type": "module",
19
+ "module": "dist/icu-message-formatter.js",
20
+ "main": "dist/icu-message-formatter.cjs",
21
+ "types": "dist/icu-message-formatter.d.ts",
22
+ "exports": {
23
+ "import": {
24
+ "default": "./dist/icu-message-formatter.js",
25
+ "types": "./dist/icu-message-formatter.d.ts"
26
+ },
27
+ "require": {
28
+ "default": "./dist/icu-message-formatter.cjs",
29
+ "types": "./dist/icu-message-formatter.d.cts"
30
+ }
31
+ },
32
+ "files": [
33
+ "dist",
34
+ "CHANGELOG.md"
35
+ ],
20
36
  "sideEffects": false,
21
37
  "scripts": {
22
- "format": "eslint --fix \"**/*.js\"",
23
38
  "lint": "eslint \"**/*.js\"",
24
39
  "test": "jest",
25
- "build": "rollup --config && rollup --config rollup.config.dist.js",
40
+ "build": "npm run build:dist && npm run build:browser && npm run build:dts",
41
+ "build:dist": "rollup --config",
42
+ "build:browser": "rollup --config rollup.config.browser.js",
43
+ "build:dts": "tsc --allowJs --declaration --emitDeclarationOnly dist/icu-message-formatter.js dist/icu-message-formatter.cjs",
26
44
  "prepublishOnly": "npm run build"
27
45
  },
28
46
  "dependencies": {
29
- "@babel/runtime": "^7.11.2",
30
- "@ultraq/array-utils": "^2.1.0",
31
- "@ultraq/function-utils": "^0.3.0"
47
+ "@babel/runtime": "^7.22.15",
48
+ "@ultraq/function-utils": "^0.5.1"
32
49
  },
33
50
  "devDependencies": {
34
- "@babel/core": "^7.14.2",
35
- "@babel/plugin-proposal-class-properties": "^7.13.0",
36
- "@babel/plugin-transform-runtime": "^7.14.2",
37
- "@babel/preset-env": "^7.14.2",
38
- "@formatjs/intl-locale": "^2.4.14",
39
- "@formatjs/intl-numberformat": "^6.1.4",
40
- "@rollup/plugin-babel": "^5.3.0",
41
- "@rollup/plugin-commonjs": "^20.0.0",
42
- "@rollup/plugin-node-resolve": "^13.0.5",
43
- "@types/jest": "^27.0.2",
44
- "babel-eslint": "^10.1.0",
45
- "eslint": "^7.32.0",
46
- "eslint-config-ultraq": "^2.4.0",
47
- "eslint-plugin-compat": "^3.13.0",
48
- "jest": "^27.2.1",
49
- "rollup": "^2.57.0",
50
- "rollup-plugin-terser": "^7.0.2"
51
+ "@babel/core": "^7.22.15",
52
+ "@babel/plugin-transform-runtime": "^7.22.15",
53
+ "@babel/preset-env": "^7.22.15",
54
+ "@formatjs/intl-locale": "^3.3.2",
55
+ "@formatjs/intl-numberformat": "^8.7.0",
56
+ "@rollup/plugin-babel": "^6.0.3",
57
+ "@rollup/plugin-commonjs": "^25.0.4",
58
+ "@rollup/plugin-node-resolve": "^15.2.1",
59
+ "@rollup/plugin-terser": "^0.4.3",
60
+ "@types/jest": "^29.5.4",
61
+ "eslint": "^8.48.0",
62
+ "eslint-config-ultraq": "^3.1.0",
63
+ "eslint-plugin-import": "^2.28.1",
64
+ "eslint-plugin-jsdoc": "^46.5.1",
65
+ "jest": "^29.6.4",
66
+ "rollup": "^4.9.1",
67
+ "typescript": "^5.2.2"
51
68
  },
52
69
  "engines": {
53
- "node": ">=12"
70
+ "node": ">=18"
54
71
  }
55
72
  }
@@ -1,2 +0,0 @@
1
- function r(r,n){var t=Object.keys(r);if(Object.getOwnPropertySymbols){var e=Object.getOwnPropertySymbols(r);n&&(e=e.filter((function(n){return Object.getOwnPropertyDescriptor(r,n).enumerable}))),t.push.apply(t,e)}return t}function n(n){for(var t=1;t<arguments.length;t++){var e=null!=arguments[t]?arguments[t]:{};t%2?r(Object(e),!0).forEach((function(r){o(n,r,e[r])})):Object.getOwnPropertyDescriptors?Object.defineProperties(n,Object.getOwnPropertyDescriptors(e)):r(Object(e)).forEach((function(r){Object.defineProperty(n,r,Object.getOwnPropertyDescriptor(e,r))}))}return n}function t(r,n){if(!(r instanceof n))throw new TypeError("Cannot call a class as a function")}function e(r,n){for(var t=0;t<n.length;t++){var e=n[t];e.enumerable=e.enumerable||!1,e.configurable=!0,"value"in e&&(e.writable=!0),Object.defineProperty(r,e.key,e)}}function o(r,n,t){return n in r?Object.defineProperty(r,n,{value:t,enumerable:!0,configurable:!0,writable:!0}):r[n]=t,r}function i(r,n){return function(r){if(Array.isArray(r))return r}(r)||function(r,n){var t=null==r?null:"undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(null==t)return;var e,o,i=[],a=!0,u=!1;try{for(t=t.call(r);!(a=(e=t.next()).done)&&(i.push(e.value),!n||i.length!==n);a=!0);}catch(r){u=!0,o=r}finally{try{a||null==t.return||t.return()}finally{if(u)throw o}}return i}(r,n)||function(r,n){if(!r)return;if("string"==typeof r)return a(r,n);var t=Object.prototype.toString.call(r).slice(8,-1);"Object"===t&&r.constructor&&(t=r.constructor.name);if("Map"===t||"Set"===t)return Array.from(r);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return a(r,n)}(r,n)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(r,n){(null==n||n>r.length)&&(n=r.length);for(var t=0,e=new Array(n);t<n;t++)e[t]=r[t];return e}function u(r){for(var n=function(r){return/\s/.test(r)},t=[],e={},o=0,i=null,a=!1,u=0;u<r.length;){if(a&&(n(r[u])||"{"===r[u]))a=!1,i=r.slice(o,u),"{"===r[u]&&u--;else if(!a&&!n(r[u])){var c="{"===r[u];if(i&&c){var s=l(r,u);if(-1===s)throw new Error('Unbalanced curly braces in string: "'.concat(r,'"'));e[i]=r.slice(u+1,s),u=s,i=null}else i&&(t.push(i),i=null),a=!0,o=u}u++}return a&&(i=r.slice(o)),i&&t.push(i),{args:t,cases:e}}function l(r,n){for(var t=0,e=n+1;e<r.length;e++){var o=r.charAt(e);if("}"===o){if(0===t)return e;t--}else"{"===o&&t++}return-1}function c(r){return s(r.slice(1,-1),",",3)}function s(r,n,t){var e=arguments.length>3&&void 0!==arguments[3]?arguments[3]:[];if(!r)return e;if(1===t)return e.push(r),e;var o=r.indexOf(n);if(-1===o)return e.push(r),e;var i=r.substring(0,o).trim(),a=r.substring(o+n.length+1).trim();return e.push(i),s(a,n,t-1,e)}function f(r){return r.reduce((function(r,n){return r.concat(Array.isArray(n)?f(n):n)}),[])}function h(r){var n={};return function(){for(var t=arguments.length,e=new Array(t),o=0;o<t;o++)e[o]=arguments[o];var i=e.length?e.map((function(r){return null===r?"null":void 0===r?"undefined":"function"==typeof r?r.toString():r instanceof Date?r.toISOString():JSON.stringify(r)})).join("|"):"_(no-args)_";if(Object.prototype.hasOwnProperty.call(n,i))return n[i];var a=r.apply(void 0,e);return n[i]=a,a}}var v,p=function(){function r(n){var e=this,i=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};t(this,r),o(this,"format",h((function(r){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return f(e.process(r,n)).join("")}))),this.locale=n,this.typeHandlers=i}var n,a,u;return n=r,a=[{key:"process",value:function(r){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!r)return[];var t=r.indexOf("{");if(-1!==t){var e=l(r,t);if(-1===e)throw new Error('Unbalanced curly braces in string: "'.concat(r,'"'));var o=r.substring(t,e+1);if(o){var a=[],u=r.substring(0,t);u&&a.push(u);var s=c(o),f=i(s,3),h=f[0],v=f[1],p=f[2],g=n[h];null==g&&(g="");var y=v&&this.typeHandlers[v];a.push(y?y(g,p,this.locale,n,this.process.bind(this)):g);var b=r.substring(e+1);return b&&a.push(this.process(b,n)),a}}return[r]}}],a&&e(n.prototype,a),u&&e(n,u),r}(),g=0;function y(r,n){for(var t=0,e="",o=0,i={};t<r.length;){if("#"!==r[t]||o)e+=r[t];else{var a="__hashToken".concat(g++);e+="{".concat(a,", number}"),i[a]=n}"{"===r[t]?o++:"}"===r[t]&&o--,t++}return{caseBody:e,numberValues:i}}function b(r){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",e=arguments.length>2?arguments[2]:void 0,o=arguments.length>3?arguments[3]:void 0,i=arguments.length>4?arguments[4]:void 0,a=u(t),l=a.args,c=a.cases,s=parseInt(r);l.forEach((function(r){r.startsWith("offset:")&&(s-=parseInt(r.slice("offset:".length)))}));var f=[];if("PluralRules"in Intl){void 0!==v&&v.resolvedOptions().locale===e||(v=new Intl.PluralRules(e));var h=v.select(s);"other"!==h&&f.push(h)}1===s&&f.push("one"),f.push("=".concat(s),"other");for(var p=0;p<f.length;p++){var g=f[p];if(g in c){var b=y(c[g],s),d=b.caseBody,O=b.numberValues;return i(d,n(n({},o),O))}}return r}function d(r){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",t=arguments.length>3?arguments[3]:void 0,e=arguments.length>4?arguments[4]:void 0,o=u(n),i=o.cases;return r in i?e(i[r],t):"other"in i?e(i.other,t):r}export{p as MessageFormatter,l as findClosingBracket,u as parseCases,b as pluralTypeHandler,d as selectTypeHandler,c as splitFormattedArgument};
2
- //# sourceMappingURL=icu-message-formatter.es.min.js.map