@orkestrel/interpret 0.0.4 → 0.0.5
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/dist/src/core/index.cjs
CHANGED
|
@@ -2,6 +2,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
let _orkestrel_contract = require("@orkestrel/contract");
|
|
3
3
|
let _orkestrel_reason = require("@orkestrel/reason");
|
|
4
4
|
let _orkestrel_emitter = require("@orkestrel/emitter");
|
|
5
|
+
let _orkestrel_template = require("@orkestrel/template");
|
|
5
6
|
//#region src/core/constants.ts
|
|
6
7
|
/**
|
|
7
8
|
* Default `similarity` for `createInterpret` / `matchAlias` — the fuzzy
|
|
@@ -88,8 +89,8 @@ var DEFAULT_VERBS = Object.freeze({});
|
|
|
88
89
|
* `{table}.{reasoning}` for the four reasons kinds, `result.quantitative.failed`
|
|
89
90
|
* for the quantitative-result failure suffix, and `subject.fields` /
|
|
90
91
|
* `subject.empty` for `describeSubject`. Every string is a plain
|
|
91
|
-
* `
|
|
92
|
-
* against the caller-supplied `values` record.
|
|
92
|
+
* @orkestrel/template `fillTemplate` template — `{{name}}`-style placeholders
|
|
93
|
+
* resolved against the caller-supplied `values` record.
|
|
93
94
|
*/
|
|
94
95
|
var DEFAULT_LEXICON = Object.freeze({
|
|
95
96
|
phrases: Object.freeze({}),
|
|
@@ -368,38 +369,6 @@ function setField(subject, field, value) {
|
|
|
368
369
|
};
|
|
369
370
|
}
|
|
370
371
|
/**
|
|
371
|
-
* Interpolate `{{dotted.path}}` tokens in a message template against a record.
|
|
372
|
-
*
|
|
373
|
-
* @remarks
|
|
374
|
-
* Each token is split on `.` into a {@link FieldPath} array and resolved with
|
|
375
|
-
* the contracts `resolveField` (a plain string field is ONE key, never
|
|
376
|
-
* dot-split — the split here is the token-to-path bridge). A finite number
|
|
377
|
-
* renders with `en-US` thousand grouping (`5010` → `5,010`); any other
|
|
378
|
-
* resolved value String-coerces. An UNRESOLVED path (the resolved value is
|
|
379
|
-
* `undefined`) renders as the empty string — the deterministic "nothing to
|
|
380
|
-
* show" rule.
|
|
381
|
-
*
|
|
382
|
-
* @param template - The message template carrying `{{dotted.path}}` tokens
|
|
383
|
-
* @param record - The record tokens resolve against
|
|
384
|
-
* @returns The template with every token replaced
|
|
385
|
-
*
|
|
386
|
-
* @example
|
|
387
|
-
* ```ts
|
|
388
|
-
* import { interpolateMessage } from '@src/core'
|
|
389
|
-
*
|
|
390
|
-
* interpolateMessage('Limit is {{limit}}', { limit: 5010 }) // 'Limit is 5,010'
|
|
391
|
-
* interpolateMessage('Missing {{gone}}', {}) // 'Missing '
|
|
392
|
-
* ```
|
|
393
|
-
*/
|
|
394
|
-
function interpolateMessage(template, record) {
|
|
395
|
-
return template.replace(/\{\{\s*([^}]+?)\s*\}\}/g, (_match, path) => {
|
|
396
|
-
const value = (0, _orkestrel_contract.resolveField)(record, path.split("."));
|
|
397
|
-
if (value === void 0) return "";
|
|
398
|
-
if ((0, _orkestrel_contract.isFiniteNumber)(value)) return value.toLocaleString("en-US");
|
|
399
|
-
return String(value);
|
|
400
|
-
});
|
|
401
|
-
}
|
|
402
|
-
/**
|
|
403
372
|
* Replace every whole-word occurrence of a map's keys with their values.
|
|
404
373
|
*
|
|
405
374
|
* @remarks
|
|
@@ -1517,7 +1486,7 @@ var Narrator = class {
|
|
|
1517
1486
|
line(id, values) {
|
|
1518
1487
|
if (!Object.hasOwn(this.#lexicon.templates, id)) return "";
|
|
1519
1488
|
const template = this.#lexicon.templates[id];
|
|
1520
|
-
return typeof template === "string" ?
|
|
1489
|
+
return typeof template === "string" ? (0, _orkestrel_template.fillTemplate)(template, values, { missing: "empty" }) : "";
|
|
1521
1490
|
}
|
|
1522
1491
|
value(unit, raw) {
|
|
1523
1492
|
if (Object.hasOwn(this.#formatters, unit)) {
|
|
@@ -2686,7 +2655,6 @@ exports.describeSubject = describeSubject;
|
|
|
2686
2655
|
exports.digestValue = digestValue;
|
|
2687
2656
|
exports.escapeRegExp = escapeRegExp;
|
|
2688
2657
|
exports.extractNumbers = extractNumbers;
|
|
2689
|
-
exports.interpolateMessage = interpolateMessage;
|
|
2690
2658
|
exports.isComputedField = isComputedField;
|
|
2691
2659
|
exports.isEntityMapping = isEntityMapping;
|
|
2692
2660
|
exports.isFieldDefault = isFieldDefault;
|