@jarenjs/core 0.67.0 → 0.72.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/README.md +1 -0
- package/dist/types/message.d.ts +4 -2
- package/package.json +1 -1
- package/src/message.js +22 -19
package/README.md
CHANGED
|
@@ -179,6 +179,7 @@ The kernel is measured against one-pass loops written for one question and again
|
|
|
179
179
|
import { compileMessageTemplate, compileMessageCatalog } from '@jarenjs/core/message';
|
|
180
180
|
|
|
181
181
|
const t = compileMessageTemplate('must be {comparison} {limit}');
|
|
182
|
+
// t.parameters is the frozen unique list ['comparison', 'limit'] from this parser.
|
|
182
183
|
t({ comparison: '>=', limit: 18 }); // 'must be >= 18'
|
|
183
184
|
```
|
|
184
185
|
|
package/dist/types/message.d.ts
CHANGED
|
@@ -38,9 +38,11 @@ export declare function formatMessageValue(value: unknown): string;
|
|
|
38
38
|
* instead of rendering as `undefined`); `{{` escapes a literal `{`.
|
|
39
39
|
*
|
|
40
40
|
* @param {string} template - The template text
|
|
41
|
-
* @returns {(params: object, error?: object) => string} The compiled render closure
|
|
41
|
+
* @returns {((params: object, error?: object) => string) & { readonly parameters: readonly string[] }} The compiled render closure and its unique placeholder names
|
|
42
42
|
*/
|
|
43
|
-
export declare function compileMessageTemplate(template: string): (params: object, error?: object) => string
|
|
43
|
+
export declare function compileMessageTemplate(template: string): ((params: object, error?: object) => string) & {
|
|
44
|
+
readonly parameters: readonly string[];
|
|
45
|
+
};
|
|
44
46
|
/**
|
|
45
47
|
* Compile a catalog-like object into a functions-only frozen catalog.
|
|
46
48
|
* Entries may be render closures (kept as-is) or template strings
|
package/package.json
CHANGED
package/src/message.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
//@ts-check
|
|
2
|
+
import { setObjectMember } from './object.js';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Human-message templating: the shared half of every message catalog in
|
|
@@ -49,7 +50,7 @@ export function formatMessageValue(value) {
|
|
|
49
50
|
* instead of rendering as `undefined`); `{{` escapes a literal `{`.
|
|
50
51
|
*
|
|
51
52
|
* @param {string} template - The template text
|
|
52
|
-
* @returns {(params: object, error?: object) => string} The compiled render closure
|
|
53
|
+
* @returns {((params: object, error?: object) => string) & { readonly parameters: readonly string[] }} The compiled render closure and its unique placeholder names
|
|
53
54
|
*/
|
|
54
55
|
export function compileMessageTemplate(template) {
|
|
55
56
|
/** @type {string[]} literal parts between placeholders */
|
|
@@ -79,22 +80,24 @@ export function compileMessageTemplate(template) {
|
|
|
79
80
|
}
|
|
80
81
|
parts.push(literal);
|
|
81
82
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
83
|
+
const render = names.length === 0
|
|
84
|
+
? function renderLiteralTemplate() { return parts[0]; }
|
|
85
|
+
: function renderMessageTemplate(params) {
|
|
86
|
+
let out = parts[0];
|
|
87
|
+
for (let i = 0; i < names.length; ++i) {
|
|
88
|
+
const name = names[i];
|
|
89
|
+
out += (params != null && name in params)
|
|
90
|
+
? formatTemplateParam(params[name])
|
|
91
|
+
: `{${name}}`;
|
|
92
|
+
out += parts[i + 1];
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
};
|
|
96
|
+
// Metadata comes from the parser that renders the message; consumers must
|
|
97
|
+
// never parse placeholders with a second, subtly different grammar.
|
|
98
|
+
return Object.defineProperty(render, 'parameters', {
|
|
99
|
+
value: Object.freeze([...new Set(names)]), enumerable: true,
|
|
100
|
+
});
|
|
98
101
|
}
|
|
99
102
|
|
|
100
103
|
/**
|
|
@@ -111,9 +114,9 @@ export function compileMessageCatalog(catalogLike) {
|
|
|
111
114
|
const keys = Object.keys(catalogLike);
|
|
112
115
|
for (let i = 0; i < keys.length; ++i) {
|
|
113
116
|
const entry = catalogLike[keys[i]];
|
|
114
|
-
compiled
|
|
117
|
+
setObjectMember(compiled, keys[i], typeof entry === 'function'
|
|
115
118
|
? entry
|
|
116
|
-
: compileMessageTemplate(String(entry));
|
|
119
|
+
: compileMessageTemplate(String(entry)));
|
|
117
120
|
}
|
|
118
121
|
return Object.freeze(compiled);
|
|
119
122
|
}
|