@internationalized/string-compiler 3.0.0-nightly-641446f65-240905

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/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @internationalized/string-compiler
2
+
3
+ This package is part of [react-spectrum](https://github.com/adobe/react-spectrum). See the repo for more details.
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@internationalized/string-compiler",
3
+ "version": "3.0.0-nightly-641446f65-240905",
4
+ "description": "Localized string compiler for @internationalized/string",
5
+ "license": "Apache-2.0",
6
+ "main": "src/stringCompiler.js",
7
+ "types": "src/stringCompiler.d.ts",
8
+ "files": [
9
+ "src"
10
+ ],
11
+ "sideEffects": false,
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "https://github.com/adobe/react-spectrum"
15
+ },
16
+ "dependencies": {
17
+ "@formatjs/icu-messageformat-parser": "^2.4.0"
18
+ },
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "stableVersion": "3.2.4"
23
+ }
@@ -0,0 +1,17 @@
1
+ /*
2
+ * Copyright 2022 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ /** Compiles an object containing ICU message strings to a JavaScript module. */
14
+ export function compileStrings(messages: Record<string, string>): string;
15
+
16
+ /** Compiles a single ICU message string to JavaScript source code. */
17
+ export function compileString(message: string): string;
@@ -0,0 +1,105 @@
1
+ /*
2
+ * Copyright 2022 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ const {parse, TYPE} = require('@formatjs/icu-messageformat-parser');
14
+
15
+ function compileStrings(messages) {
16
+ let res = 'module.exports = {';
17
+ for (let key in messages) {
18
+ res += ' ' + JSON.stringify(key) + ': ' + compileString(messages[key]) + ',\n';
19
+ }
20
+
21
+ res += '}';
22
+ return res;
23
+ }
24
+
25
+ exports.compileStrings = compileStrings;
26
+
27
+ function compileString(message) {
28
+ let parts = parse(message);
29
+ return compileParts(parts);
30
+ }
31
+
32
+ exports.compileString = compileString;
33
+
34
+ function compileParts(parts, inline = false, pluralValue = '') {
35
+ let hasArgs = false;
36
+ let usesFormatter = false;
37
+ let res = '`';
38
+ for (let part of parts) {
39
+ if (part.type !== TYPE.literal) {
40
+ hasArgs = true;
41
+ }
42
+
43
+ switch (part.type) {
44
+ case TYPE.literal:
45
+ res += escape(part.value);
46
+ break;
47
+ case TYPE.argument:
48
+ res += '${args.' + part.value + '}';
49
+ break;
50
+ case TYPE.plural: {
51
+ let pluralValue = 'args.' + part.value + (part.offset ? ' - ' + part.offset : '');
52
+ res += '${formatter.plural('
53
+ + pluralValue
54
+ + ', ' + compileOptions(part.options, pluralValue)
55
+ + (part.pluralType !== 'cardinal' ? ', "ordinal"' : '')
56
+ + ')}';
57
+ usesFormatter = true;
58
+ break;
59
+ }
60
+ case TYPE.select:
61
+ res += '${formatter.select(' + compileOptions(part.options, pluralValue) + ', args.' + part.value + ')}';
62
+ usesFormatter = true;
63
+ break;
64
+ case TYPE.pound:
65
+ res += '${formatter.number(' + pluralValue + ')}';
66
+ usesFormatter = true;
67
+ break;
68
+ case TYPE.number:
69
+ // TODO: Eventually this will need to support the formatting options that are possible here.
70
+ // They're in part.parsedOptions. Would require changes to the formatter to support it though.
71
+ res += '${formatter.number(args.' + part.value + ')}';
72
+ usesFormatter = true;
73
+ break;
74
+ default:
75
+ throw new Error('Unsupported message type: ' + part.type);
76
+ }
77
+ }
78
+
79
+ res += '`';
80
+
81
+ if (hasArgs) {
82
+ res = (inline ? '() => ' : '(args' + (usesFormatter ? ', formatter' : '') + ') => ') + res;
83
+ }
84
+
85
+ return res;
86
+ }
87
+
88
+ function compileOptions(options, pluralValue) {
89
+ let res = '{';
90
+ let keys = Object.keys(options);
91
+ for (let i = 0; i < keys.length; i++) {
92
+ let key = keys[i];
93
+ res += key.startsWith('=') ? JSON.stringify(key) : key;
94
+ res += ': ' + compileParts(options[key].value, true, pluralValue);
95
+ if (i < keys.length - 1) {
96
+ res += ', ';
97
+ }
98
+ }
99
+ res += '}';
100
+ return res;
101
+ }
102
+
103
+ function escape(string) {
104
+ return string.replace(/([$`])/g, '\\$1');
105
+ }