@ultraq/icu-message-formatter 0.13.0 → 0.14.1

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 (37) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +2 -4
  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/{lib/icu-message-formatter.cjs.js → dist/icu-message-formatter.cjs} +65 -52
  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/{lib/icu-message-formatter.es.js → dist/icu-message-formatter.js} +65 -52
  12. package/dist/icu-message-formatter.js.map +1 -0
  13. package/package.json +25 -16
  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/index.d.ts +0 -5
  19. package/lib/icu-message-formatter.cjs.js.map +0 -1
  20. package/lib/icu-message-formatter.es.js.map +0 -1
  21. package/rollup.config.dist.js +0 -36
  22. package/rollup.config.js +0 -35
  23. package/source/IcuMessageFormatter.js +0 -20
  24. package/source/MessageFormatter.js +0 -138
  25. package/source/MessageFormatter.test.js +0 -153
  26. package/source/pluralTypeHandler.js +0 -122
  27. package/source/pluralTypeHandler.test.js +0 -188
  28. package/source/selectTypeHandler.js +0 -46
  29. package/source/selectTypeHandler.test.js +0 -59
  30. package/source/utilities.js +0 -174
  31. package/source/utilities.test.js +0 -123
  32. package/types/IcuMessageFormatter.d.ts +0 -4
  33. package/types/MessageFormatter.d.ts +0 -71
  34. package/types/pluralTypeHandler.d.ts +0 -15
  35. package/types/selectTypeHandler.d.ts +0 -15
  36. package/types/typedefs.d.ts +0 -3
  37. package/types/utilities.d.ts +0 -52
@@ -1,123 +0,0 @@
1
- /*
2
- * Copyright 2019, Emanuel Rabina (http://www.ultraq.net.nz/)
3
- *
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
-
17
- /* eslint-env jest */
18
- import {parseCases} from './utilities';
19
-
20
- /**
21
- * Tests for the `parseCases` util.
22
- */
23
- describe('parseCases', function() {
24
- describe('empty', function() {
25
- test('empty string', function() {
26
- let result = parseCases('');
27
- expect(result.args).toStrictEqual([]);
28
- expect(result.cases).toStrictEqual({});
29
- });
30
- });
31
-
32
- describe('basic case support', function() {
33
- test('single case', function() {
34
- let result = parseCases('key {case string!}');
35
- expect(result.args).toStrictEqual([]);
36
- expect(result.cases).toStrictEqual({key: 'case string!'});
37
- });
38
-
39
- test('fails if cant close case', function() {
40
- expect(() => {
41
- parseCases('key1 {{case1}');
42
- }).toThrowError();
43
- });
44
-
45
- test('multiple cases', function() {
46
- let result = parseCases('key1 {case1} key2 {case2} key3 {case3}');
47
- expect(result.args).toStrictEqual([]);
48
- expect(result.cases).toStrictEqual({key1: 'case1', key2: 'case2', key3: 'case3'});
49
- });
50
-
51
- test('multiple cases with symbols', function() {
52
- let result = parseCases('=key1 {case1} &key2 {case2} key3 {case3}');
53
- expect(result.args).toStrictEqual([]);
54
- expect(result.cases).toStrictEqual({'=key1': 'case1', '&key2': 'case2', key3: 'case3'});
55
- });
56
-
57
- test('multiple cases with inconsistent whitespace', function() {
58
- let result = parseCases(`key1 {case1}
59
-
60
-
61
- key2 {case2}
62
- key3 {case3}`);
63
- expect(result.args).toStrictEqual([]);
64
- expect(result.cases).toStrictEqual({key1: 'case1', key2: 'case2', key3: 'case3'});
65
- });
66
-
67
- test('multiple cases with minimal whitespace', function() {
68
- let result = parseCases(`key1{case1}key2{case2}key3{case3}`);
69
- expect(result.args).toStrictEqual([]);
70
- expect(result.cases).toStrictEqual({key1: 'case1', key2: 'case2', key3: 'case3'});
71
- });
72
-
73
- test('multiple cases with complex bodies', function() {
74
- let result = parseCases(`key1 {{}{}{}{{{{}}}}}
75
-
76
-
77
- key2 {=key1 {case1} &key2 {case2} key3 {case3}}
78
- key3 {}`);
79
- expect(result.args).toStrictEqual([]);
80
- expect(result.cases).toStrictEqual({key1: '{}{}{}{{{{}}}}', key2: '=key1 {case1} &key2 {case2} key3 {case3}', key3: ''});
81
- });
82
- });
83
-
84
- describe('basic arg support', function() {
85
- test('single arg', function() {
86
- let result = parseCases('arg1');
87
- expect(result.args).toStrictEqual(['arg1']);
88
- });
89
-
90
- test('multiple args', function() {
91
- let result = parseCases('arg1 arg2 arg3');
92
- expect(result.args).toStrictEqual(['arg1', 'arg2', 'arg3']);
93
- });
94
-
95
- test('multiple args with inconsistent whitespace', function() {
96
- let result = parseCases(`arg1
97
-
98
-
99
- arg2 arg3`);
100
- expect(result.args).toStrictEqual(['arg1', 'arg2', 'arg3']);
101
- });
102
- });
103
-
104
- describe('arg and cases support', function() {
105
- test('multiple args and cases', function() {
106
- let result = parseCases(`
107
- offset:1 something:else
108
- =0 {{host} does not give a party.}
109
- =1 {{host} invites {guest} to her party.}
110
- =2 {{host} invites {guest} and one other person to her party.}
111
- other {{host} invites {guest} and # other people to her party.}
112
- `);
113
-
114
- expect(result.args).toStrictEqual(['offset:1', 'something:else']);
115
- expect(result.cases).toStrictEqual({
116
- '=0': '{host} does not give a party.',
117
- '=1': '{host} invites {guest} to her party.',
118
- '=2': '{host} invites {guest} and one other person to her party.',
119
- other: '{host} invites {guest} and # other people to her party.'
120
- });
121
- });
122
- });
123
- });
@@ -1,4 +0,0 @@
1
- export { default as MessageFormatter } from "./MessageFormatter.js";
2
- export { default as pluralTypeHandler } from "./pluralTypeHandler.js";
3
- export { default as selectTypeHandler } from "./selectTypeHandler.js";
4
- export { findClosingBracket, parseCases, splitFormattedArgument } from "./utilities.js";
@@ -1,71 +0,0 @@
1
- /**
2
- * @typedef {Record<string,any>} FormatValues
3
- */
4
- /**
5
- * @callback ProcessFunction
6
- * @param {string} message
7
- * @param {FormatValues} [values={}]
8
- * @return {any[]}
9
- */
10
- /**
11
- * @callback TypeHandler
12
- * @param {any} value
13
- * The object which matched the key of the block being processed.
14
- * @param {string} matches
15
- * Any format options associated with the block being processed.
16
- * @param {string} locale
17
- * The locale to use for formatting.
18
- * @param {FormatValues} values
19
- * The object of placeholder data given to the original `format`/`process`
20
- * call.
21
- * @param {ProcessFunction} process
22
- * The `process` function itself so that sub-messages can be processed by type
23
- * handlers.
24
- * @return {any | any[]}
25
- */
26
- /**
27
- * The main class for formatting messages.
28
- *
29
- * @author Emanuel Rabina
30
- */
31
- export default class MessageFormatter {
32
- /**
33
- * Creates a new formatter that can work using any of the custom type handlers
34
- * you register.
35
- *
36
- * @param {string} locale
37
- * @param {Record<string,TypeHandler>} [typeHandlers]
38
- * Optional object where the keys are the names of the types to register,
39
- * their values being the functions that will return a nicely formatted
40
- * string for the data and locale they are given.
41
- */
42
- constructor(locale: string, typeHandlers?: Record<string, TypeHandler>);
43
- locale: string;
44
- typeHandlers: Record<string, TypeHandler>;
45
- /**
46
- * Formats an ICU message syntax string using `values` for placeholder data
47
- * and any currently-registered type handlers.
48
- *
49
- * @type {(message: string, values?: FormatValues) => string}
50
- */
51
- format: (message: string, values?: FormatValues) => string;
52
- /**
53
- * Process an ICU message syntax string using `values` for placeholder data
54
- * and any currently-registered type handlers. The result of this method is
55
- * an array of the component parts after they have been processed in turn by
56
- * their own type handlers. This raw output is useful for other renderers,
57
- * eg: React where components can be used instead of being forced to return
58
- * raw strings.
59
- *
60
- * This method is used by {@link MessageFormatter#format} where it acts as a
61
- * string renderer.
62
- *
63
- * @param {string} message
64
- * @param {FormatValues} [values]
65
- * @return {any[]}
66
- */
67
- process(message: string, values?: FormatValues): any[];
68
- }
69
- export type FormatValues = Record<string, any>;
70
- export type ProcessFunction = (message: string, values?: FormatValues) => any[];
71
- export type TypeHandler = (value: any, matches: string, locale: string, values: FormatValues, process: ProcessFunction) => any | any[];
@@ -1,15 +0,0 @@
1
- /**
2
- * Handler for `plural` statements within ICU message syntax strings. Returns
3
- * a formatted string for the branch that closely matches the current value.
4
- *
5
- * See https://formatjs.io/docs/core-concepts/icu-syntax#plural-format for more
6
- * details on how the `plural` statement works.
7
- *
8
- * @param {string} value
9
- * @param {string} matches
10
- * @param {string} locale
11
- * @param {Record<string,any>} values
12
- * @param {(message: string, values?: Record<string,any>) => any[]} process
13
- * @return {any | any[]}
14
- */
15
- export default function pluralTypeHandler(value: string, matches: string, locale: string, values: Record<string, any>, process: (message: string, values?: Record<string, any>) => any[]): any | any[];
@@ -1,15 +0,0 @@
1
- /**
2
- * Handler for `select` statements within ICU message syntax strings. Returns
3
- * a formatted string for the branch that closely matches the current value.
4
- *
5
- * See https://formatjs.io/docs/core-concepts/icu-syntax#select-format for more
6
- * details on how the `select` statement works.
7
- *
8
- * @param {string} value
9
- * @param {string} matches
10
- * @param {string} locale
11
- * @param {Record<string,any>} values
12
- * @param {(message: string, values?: Record<string,any>) => any[]} process
13
- * @return {any | any[]}
14
- */
15
- export default function selectTypeHandler(value: string, matches: string, locale: string, values: Record<string, any>, process: (message: string, values?: Record<string, any>) => any[]): any | any[];
@@ -1,3 +0,0 @@
1
- type FormatValues = Record<string, any>;
2
- type ProcessFunction = (message: string, values?: FormatValues) => any[];
3
- type TypeHandler = (value: any, matches: string, locale: string, values: FormatValues, process: ProcessFunction) => any | any[];
@@ -1,52 +0,0 @@
1
- /**
2
- * @typedef ParseCasesResult
3
- * @property {string[]} args
4
- * A list of prepended arguments.
5
- * @property {Record<string,string>} cases
6
- * A map of all cases.
7
- */
8
- /**
9
- * Most branch-based type handlers are based around "cases". For example,
10
- * `select` and `plural` compare compare a value to "case keys" to choose a
11
- * subtranslation.
12
- *
13
- * This util splits "matches" portions provided to the aforementioned handlers
14
- * into case strings, and extracts any prepended arguments (for example,
15
- * `plural` supports an `offset:n` argument used for populating the magic `#`
16
- * variable).
17
- *
18
- * @param {string} string
19
- * @return {ParseCasesResult}
20
- */
21
- export function parseCases(string: string): ParseCasesResult;
22
- /**
23
- * Finds the index of the matching closing curly bracket, including through
24
- * strings that could have nested brackets.
25
- *
26
- * @param {string} string
27
- * @param {number} fromIndex
28
- * @return {number}
29
- * The index of the matching closing bracket, or -1 if no closing bracket
30
- * could be found.
31
- */
32
- export function findClosingBracket(string: string, fromIndex: number): number;
33
- /**
34
- * Split a `{key, type, format}` block into those 3 parts, taking into account
35
- * nested message syntax that can exist in the `format` part.
36
- *
37
- * @param {string} block
38
- * @return {string[]}
39
- * An array with `key`, `type`, and `format` items in that order, if present
40
- * in the formatted argument block.
41
- */
42
- export function splitFormattedArgument(block: string): string[];
43
- export type ParseCasesResult = {
44
- /**
45
- * A list of prepended arguments.
46
- */
47
- args: string[];
48
- /**
49
- * A map of all cases.
50
- */
51
- cases: Record<string, string>;
52
- };