@turingcode/cli 0.2.0 → 0.2.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.
- package/README.md +51 -24
- package/dist/cli/create-cli.js +1 -1
- package/dist/core/query.js +226 -0
- package/dist/features/contract/command.js +28 -9
- package/dist/features/contract/describe.js +11 -18
- package/dist/features/contract/diagnostics.js +1 -0
- package/dist/features/contract/load.js +74 -86
- package/dist/features/contract/model.js +8 -10
- package/dist/features/contract/rules.js +273 -172
- package/dist/features/table/check.js +130 -84
- package/package.json +2 -1
|
@@ -1,197 +1,298 @@
|
|
|
1
1
|
import { BoundError } from "../../core/bound-error.js";
|
|
2
2
|
import { AstError, evaluate, namesIn, parseExpression } from "../../core/exact/expression.js";
|
|
3
3
|
import { Fraction } from "../../core/exact/fraction.js";
|
|
4
|
-
|
|
4
|
+
import { compileQuery, prepareQuery } from "../../core/query.js";
|
|
5
|
+
/** Largest expression a `$calc` may use. */
|
|
5
6
|
const MAX_EXPRESSION_NODES = 256;
|
|
7
|
+
const CALC_OPS = ["<=", ">=", "==", "!=", "<", ">"];
|
|
6
8
|
const code = (v) => `\`${String(v)}\``;
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
/**
|
|
11
|
-
const
|
|
9
|
+
const plain = (v) => String(v);
|
|
10
|
+
const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
11
|
+
const NUMERIC_TEXT = { $gt: ">", $gte: ">=", $lt: "<", $lte: "<=" };
|
|
12
|
+
/** Splits a list cell into trimmed, non-empty items. */
|
|
13
|
+
export const listItems = (cell, separator = ",") => cell.split(separator).map(s => s.trim()).filter(Boolean);
|
|
14
|
+
/** A value as text: list items joined, missing as empty. */
|
|
15
|
+
export const valueText = (v) => (Array.isArray(v) ? v.join(", ") : v === null || v === undefined ? "" : String(v));
|
|
12
16
|
/**
|
|
13
|
-
*
|
|
17
|
+
* Whether `word` occurs in `text` as a whole token (not inside a longer word or ID).
|
|
14
18
|
*
|
|
15
|
-
* @param {string} text -
|
|
16
|
-
* @param {string}
|
|
17
|
-
* @
|
|
18
|
-
|
|
19
|
+
* @param {string} text - Text to search.
|
|
20
|
+
* @param {string} word - The value.
|
|
21
|
+
* @returns {boolean} True when mentioned.
|
|
22
|
+
*/
|
|
23
|
+
export function mentions(text, word) {
|
|
24
|
+
const escaped = word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
25
|
+
return new RegExp(`(?<![A-Za-z0-9_])${escaped}(?![A-Za-z0-9_]|-\\d)`).test(text);
|
|
26
|
+
}
|
|
27
|
+
/** Number columns of a table: the names a `$calc` expression may use. */
|
|
28
|
+
export const numberColumns = (t) => new Set(t.columns.filter(c => c.type === "integer" || c.type === "number").map(c => c.name));
|
|
29
|
+
/**
|
|
30
|
+
* Parses a `$calc` parameter over the given number columns.
|
|
31
|
+
*
|
|
32
|
+
* @param {unknown} param - The parameter.
|
|
33
|
+
* @param {Set<string>} allowed - Number columns the expression may use.
|
|
34
|
+
* @returns {Calc | string} The parsed comparison, or what is wrong with it.
|
|
19
35
|
*/
|
|
20
|
-
function
|
|
21
|
-
const
|
|
36
|
+
export function parseCalc(param, allowed) {
|
|
37
|
+
const obj = isObject(param) ? param : { expr: param };
|
|
38
|
+
if (typeof obj.expr !== "string")
|
|
39
|
+
return "$calc must be \"<expr> <op> <expr>\" or {\"expr\": ..., \"tolerance\": ...}";
|
|
40
|
+
const unknownKey = Object.keys(obj).find(k => k !== "expr" && k !== "tolerance");
|
|
41
|
+
if (unknownKey)
|
|
42
|
+
return `$calc has an unknown field '${unknownKey}'`;
|
|
43
|
+
const text = obj.expr;
|
|
44
|
+
const op = CALC_OPS.find(o => text.includes(o));
|
|
45
|
+
if (!op)
|
|
46
|
+
return `'${text}' has no comparison; use one of ${CALC_OPS.join(" ")}`;
|
|
47
|
+
const [l, r] = [text.slice(0, text.indexOf(op)), text.slice(text.indexOf(op) + op.length)];
|
|
48
|
+
const tolerance = obj.tolerance === undefined ? new Fraction(0n) : Fraction.parseDecimal(String(obj.tolerance));
|
|
49
|
+
if (!tolerance || tolerance.cmp(new Fraction(0n)) < 0)
|
|
50
|
+
return "tolerance must be a number >= 0";
|
|
22
51
|
try {
|
|
23
52
|
// Parse with every identifier allowed, then say which ones are not number columns.
|
|
24
53
|
const names = new Set(text.match(/[A-Za-z_][A-Za-z0-9_]*/g) ?? []);
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return
|
|
30
|
-
}
|
|
31
|
-
return node;
|
|
54
|
+
const left = parseExpression(l, MAX_EXPRESSION_NODES, names);
|
|
55
|
+
const right = parseExpression(r, MAX_EXPRESSION_NODES, names);
|
|
56
|
+
const bad = [...namesIn(left), ...namesIn(right)].filter(n => !allowed.has(n));
|
|
57
|
+
if (bad.length)
|
|
58
|
+
return `uses ${bad.join(", ")}, which ${bad.length > 1 ? "are" : "is"} not an integer/number column`;
|
|
59
|
+
return { text: text.trim(), left, op, right, tolerance };
|
|
32
60
|
}
|
|
33
61
|
catch (e) {
|
|
34
62
|
if (!(e instanceof AstError || e instanceof BoundError))
|
|
35
63
|
throw e;
|
|
36
|
-
|
|
37
|
-
return null;
|
|
64
|
+
return e.message;
|
|
38
65
|
}
|
|
39
66
|
}
|
|
40
|
-
/** Splits a list cell into trimmed, non-empty items. */
|
|
41
|
-
export const listItems = (cell, separator = ",") => cell.split(separator).map(s => s.trim()).filter(Boolean);
|
|
42
67
|
/**
|
|
43
|
-
*
|
|
68
|
+
* Evaluates a `$calc` on a row.
|
|
44
69
|
*
|
|
45
|
-
* @param {
|
|
46
|
-
* @param {
|
|
47
|
-
* @
|
|
70
|
+
* @param {Calc} c - The parsed comparison.
|
|
71
|
+
* @param {Doc} doc - The row.
|
|
72
|
+
* @param {Set<string>} numbers - The table's number columns.
|
|
73
|
+
* @returns {{left: Fraction, right: Fraction, holds: boolean} | null} The sides and the result; null when a
|
|
74
|
+
* value is missing or not a number, or the arithmetic fails (the column check reports those).
|
|
48
75
|
*/
|
|
49
|
-
export function
|
|
50
|
-
const
|
|
51
|
-
|
|
76
|
+
export function evaluateCalc(c, doc, numbers) {
|
|
77
|
+
const env = {};
|
|
78
|
+
for (const n of numbers)
|
|
79
|
+
env[n] = typeof doc[n] === "string" ? Fraction.parseDecimal(doc[n]) : null;
|
|
80
|
+
try {
|
|
81
|
+
const left = evaluate(c.left, env);
|
|
82
|
+
const right = evaluate(c.right, env);
|
|
83
|
+
if (!left || !right)
|
|
84
|
+
return null;
|
|
85
|
+
const within = left.sub(right).abs().cmp(c.tolerance) <= 0;
|
|
86
|
+
const cmp = left.cmp(right);
|
|
87
|
+
const holds = { "<": cmp < 0, "<=": cmp <= 0, "==": within, "!=": !within, ">=": cmp >= 0, ">": cmp > 0 }[c.op];
|
|
88
|
+
return { left, right, holds };
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return null; // e.g. division by zero with these values: nothing to compare
|
|
92
|
+
}
|
|
52
93
|
}
|
|
53
|
-
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Validates a query of the contract (`match`, `when`, `require`, `count`):
|
|
96
|
+
* operators, values, columns, `$ref`/`$mentioned` targets, `$calc` and `$regex`.
|
|
97
|
+
*
|
|
98
|
+
* @param {unknown} q - The query, as a row query.
|
|
99
|
+
* @param {string} field - Contract field, for the report.
|
|
100
|
+
* @param {ValidateContext} v - Validation context.
|
|
101
|
+
* @returns {void}
|
|
102
|
+
*/
|
|
103
|
+
export function checkQuery(q, field, v) {
|
|
104
|
+
const p = prepareQuery(q);
|
|
105
|
+
for (const message of p.problems) {
|
|
106
|
+
v.problem("CONTRACT.BAD_FIELD", message, field, JSON.stringify(q), "a query (see README: Queries)");
|
|
107
|
+
}
|
|
108
|
+
if (p.problems.length || !v.requireColumns(p.fields, field))
|
|
109
|
+
return;
|
|
110
|
+
for (const ref of p.refs)
|
|
111
|
+
v.resolve(ref, field);
|
|
112
|
+
for (const target of p.mentions.filter(t => t !== "*"))
|
|
113
|
+
v.resolve(target, field);
|
|
114
|
+
for (const calc of p.calcs) {
|
|
115
|
+
const c = parseCalc(calc, numberColumns(v.table));
|
|
116
|
+
if (typeof c === "string")
|
|
117
|
+
v.problem("CONTRACT.BAD_EXPRESSION", c, field, JSON.stringify(calc), "an exact comparison over number columns");
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
compileQuery(p.query, { ref: () => false, mentioned: () => false, calc: () => true });
|
|
121
|
+
}
|
|
122
|
+
catch (e) {
|
|
123
|
+
v.problem("CONTRACT.BAD_REGEX", e.message, field, JSON.stringify(q), "a valid $regex");
|
|
124
|
+
}
|
|
69
125
|
}
|
|
70
|
-
/**
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
126
|
+
/**
|
|
127
|
+
* Splits one column's condition into parts that are checked (and reported) one by one.
|
|
128
|
+
*
|
|
129
|
+
* @param {string} column - The column.
|
|
130
|
+
* @param {unknown} cond - Its condition.
|
|
131
|
+
* @returns {Query[]} Row queries, one per operator (`$regex` keeps its `$options`).
|
|
132
|
+
*/
|
|
133
|
+
function splitCondition(column, cond) {
|
|
134
|
+
if (!isObject(cond))
|
|
135
|
+
return [{ [column]: cond }];
|
|
136
|
+
return Object.entries(cond).filter(([op]) => op !== "$options").map(([op, arg]) => ({ [column]: op === "$regex" && cond.$options !== undefined ? { $regex: arg, $options: cond.$options } : { [op]: arg } }));
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Splits a row query into parts that are checked (and reported) one by one.
|
|
140
|
+
*
|
|
141
|
+
* @param {Query} q - The query.
|
|
142
|
+
* @returns {Query[]} One row query per column operator, logical operator, `$calc` or `$unique`.
|
|
143
|
+
*/
|
|
144
|
+
export function splitQuery(q) {
|
|
145
|
+
return Object.entries(q).flatMap(([key, v]) => (key.startsWith("$") ? [{ [key]: v }] : splitCondition(key, v)));
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Explains why a row fails one part of a query.
|
|
149
|
+
*
|
|
150
|
+
* @param {Query} part - A part from `splitQuery`.
|
|
151
|
+
* @param {Doc} doc - The row.
|
|
152
|
+
* @param {CheckHooks} hooks - The hooks the query ran with.
|
|
153
|
+
* @returns {Failure} Code, message, column, found and expected.
|
|
154
|
+
*/
|
|
155
|
+
export function explain(part, doc, hooks) {
|
|
156
|
+
const [key, arg] = Object.entries(part)[0];
|
|
157
|
+
const match = { code: "MATCH", message: "row does not match", column: "-", found: "-", expected: renderQuery(part, plain) };
|
|
158
|
+
if (key === "$calc") {
|
|
159
|
+
const values = hooks.calcValues(arg, doc);
|
|
160
|
+
const text = isObject(arg) ? String(arg.expr) : String(arg);
|
|
161
|
+
const found = values ? `${values.left.toDecimal()} vs ${values.right.toDecimal()}` : "-";
|
|
162
|
+
return { ...match, code: "COMPARE", message: `${text} is false (${found})`, found, expected: text };
|
|
163
|
+
}
|
|
164
|
+
if (key === "$unique") {
|
|
165
|
+
const cols = arg;
|
|
166
|
+
return { code: "UNIQUE_TOGETHER", message: `${cols.join(" + ")} is used twice`, column: cols.join("+"),
|
|
167
|
+
found: cols.map(c => valueText(doc[c])).join(" / "), expected: "a unique combination" };
|
|
168
|
+
}
|
|
169
|
+
if (key.startsWith("$"))
|
|
170
|
+
return match;
|
|
171
|
+
return explainCondition(key, arg, doc[key] ?? null, hooks);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Explains why one column's value fails a single-operator condition.
|
|
175
|
+
*
|
|
176
|
+
* @param {string} column - The column.
|
|
177
|
+
* @param {unknown} cond - The condition (one operator, or a plain value).
|
|
178
|
+
* @param {Value} value - The value.
|
|
179
|
+
* @param {CheckHooks} hooks - The hooks the query ran with.
|
|
180
|
+
* @returns {Failure} The explanation.
|
|
181
|
+
*/
|
|
182
|
+
function explainCondition(column, cond, value, hooks) {
|
|
183
|
+
const text = valueText(value);
|
|
184
|
+
const expected = renderCondition(cond, plain);
|
|
185
|
+
const fail = (c, message, found = text, exp = expected) => ({ code: c, message, column, found, expected: exp });
|
|
186
|
+
if (cond === null || (isObject(cond) && "$eq" in cond && cond.$eq === null))
|
|
187
|
+
return fail("EMPTY_IF", `${column} must be empty`, text, "empty");
|
|
188
|
+
if (!isObject(cond))
|
|
189
|
+
return fail("BAD_VALUE", `'${text}' is not allowed`);
|
|
190
|
+
const [op, arg] = Object.entries(cond)[0];
|
|
191
|
+
const items = Array.isArray(value) ? value : value === null ? [] : [String(value)];
|
|
192
|
+
switch (op) {
|
|
193
|
+
case "$ne":
|
|
194
|
+
return arg === null ? fail("REQUIRED_IF", `${column} is empty`, text, "a value") : fail("BAD_VALUE", `'${text}' is not allowed`);
|
|
195
|
+
case "$eq":
|
|
196
|
+
case "$in":
|
|
197
|
+
case "$nin":
|
|
198
|
+
return fail("BAD_VALUE", `'${text}' is not allowed`);
|
|
199
|
+
case "$regex":
|
|
200
|
+
return fail("PATTERN", `'${text}' does not match the pattern`);
|
|
201
|
+
case "$gt":
|
|
202
|
+
case "$gte":
|
|
203
|
+
case "$lt":
|
|
204
|
+
case "$lte":
|
|
205
|
+
return fail("OUT_OF_RANGE", `${text} is out of range`);
|
|
206
|
+
case "$length":
|
|
207
|
+
return fail("BAD_LENGTH", `length ${Array.isArray(value) ? value.length : [...text].length} is out of range`);
|
|
208
|
+
case "$unique":
|
|
209
|
+
return fail("DUPLICATE_KEY", `${column} '${text}' is used twice`, text, "a unique value");
|
|
210
|
+
case "$ref": {
|
|
211
|
+
const bad = items.filter(x => !hooks.ref(String(arg), x));
|
|
212
|
+
return fail("UNKNOWN_REF", `${bad.join(", ") || "''"} ${bad.length > 1 ? "are not values" : "is not a value"} of ${arg}`, bad.join(", ") || text);
|
|
213
|
+
}
|
|
214
|
+
case "$mentioned": {
|
|
215
|
+
const targets = [arg].flat();
|
|
216
|
+
const where = targets.includes("*") ? "the document" : targets.join(", ");
|
|
217
|
+
const bad = items.filter(x => !hooks.mentioned(targets, x));
|
|
218
|
+
return fail("NOT_MENTIONED", `${bad.join(", ")} ${bad.length > 1 ? "are" : "is"} not mentioned in ${where}`, bad.join(", ") || text, `a mention in ${where}`);
|
|
219
|
+
}
|
|
220
|
+
case "$every": {
|
|
221
|
+
// Explain with the first failing item, against the item condition.
|
|
222
|
+
const inner = isObject(arg) && Object.keys(arg).length === 1 ? arg : null;
|
|
223
|
+
if (!inner)
|
|
224
|
+
break;
|
|
225
|
+
const test = compileQuery(prepareQuery({ _: arg }).query, hooks);
|
|
226
|
+
const badItems = items.filter(x => !test({ _: x }));
|
|
227
|
+
const f = explainCondition(column, inner, badItems.length === 1 ? badItems[0] : badItems, hooks);
|
|
228
|
+
return { ...f, expected: `every item ${f.expected}` };
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return fail("MATCH", `'${text}' does not match`);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* One column condition in words, e.g. "one of `a`, `b`".
|
|
235
|
+
*
|
|
236
|
+
* @param {unknown} cond - The condition.
|
|
237
|
+
* @param {(v: unknown) => string} c - Formats a value (backticks for describe, plain for diagnostics).
|
|
238
|
+
* @returns {string} The text.
|
|
239
|
+
*/
|
|
240
|
+
export function renderCondition(cond, c = code) {
|
|
241
|
+
if (cond === null)
|
|
242
|
+
return "empty";
|
|
243
|
+
if (!isObject(cond))
|
|
244
|
+
return `= ${c(cond)}`;
|
|
245
|
+
const list = (vs) => vs.map(v => (v === null ? "empty" : c(v))).join(", ");
|
|
246
|
+
const sub = (vs) => vs.map(v => `(${renderCondition(v, c)})`);
|
|
247
|
+
return Object.entries(cond).filter(([op]) => op !== "$options").map(([op, arg]) => {
|
|
248
|
+
switch (op) {
|
|
249
|
+
case "$eq": return arg === null ? "empty" : `= ${c(arg)}`;
|
|
250
|
+
case "$ne": return arg === null ? "filled" : `not ${c(arg)}`;
|
|
251
|
+
case "$in": return `one of ${list(arg)}`;
|
|
252
|
+
case "$nin": return `none of ${list(arg)}`;
|
|
253
|
+
case "$regex": return `matches ${c(arg)}${cond.$options ? ` (flags ${cond.$options})` : ""}`;
|
|
254
|
+
case "$gt":
|
|
255
|
+
case "$gte":
|
|
256
|
+
case "$lt":
|
|
257
|
+
case "$lte": return `${NUMERIC_TEXT[op]} ${arg}`;
|
|
258
|
+
case "$length": return `length ${renderCondition(arg, c)}`;
|
|
259
|
+
case "$every": return `every item ${renderCondition(arg, c)}`;
|
|
260
|
+
case "$ref": return `a value of ${c(arg)}`;
|
|
261
|
+
case "$unique": return "unique";
|
|
262
|
+
case "$mentioned": {
|
|
263
|
+
const targets = [arg].flat();
|
|
264
|
+
return targets.includes("*") ? "mentioned elsewhere in the document" : `mentioned in ${list(targets)}`;
|
|
166
265
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
266
|
+
case "$not": return `not (${renderCondition(arg, c)})`;
|
|
267
|
+
case "$or": return `either ${sub(arg).join(" or ")}`;
|
|
268
|
+
case "$and": return sub(arg).join(" and ");
|
|
269
|
+
case "$nor": return `none of ${sub(arg).join(", ")}`;
|
|
270
|
+
default: return `${op} ${JSON.stringify(arg)}`;
|
|
271
|
+
}
|
|
272
|
+
}).join(", ");
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* A row query in words, e.g. "`Status` = `shipped` and `Tracking` filled".
|
|
276
|
+
*
|
|
277
|
+
* @param {unknown} q - The query.
|
|
278
|
+
* @param {(v: unknown) => string} c - Formats a value.
|
|
279
|
+
* @returns {string} The text.
|
|
280
|
+
*/
|
|
281
|
+
export function renderQuery(q, c = code) {
|
|
282
|
+
if (!isObject(q))
|
|
283
|
+
return JSON.stringify(q);
|
|
284
|
+
const sub = (vs) => vs.map(v => `(${renderQuery(v, c)})`);
|
|
285
|
+
return Object.entries(q).map(([key, arg]) => {
|
|
286
|
+
switch (key) {
|
|
287
|
+
case "$or": return `either ${sub(arg).join(" or ")}`;
|
|
288
|
+
case "$and": return sub(arg).join(" and ");
|
|
289
|
+
case "$nor": return `none of ${sub(arg).join(", ")}`;
|
|
290
|
+
case "$calc": {
|
|
291
|
+
const obj = isObject(arg) ? arg : { expr: arg };
|
|
292
|
+
return `${c(obj.expr)} holds${obj.tolerance !== undefined ? ` (within ${obj.tolerance})` : ""}`;
|
|
184
293
|
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
+ (targets.includes("*") ? "elsewhere in the document." : `in ${list(targets)}.`);
|
|
190
|
-
},
|
|
191
|
-
},
|
|
192
|
-
};
|
|
193
|
-
/** One plain-language line for a rule's `when`, e.g. "When `Kind` is `EXTRACTED`: ". */
|
|
194
|
-
export function whenText(rule) {
|
|
195
|
-
const parts = Object.entries(rule.when ?? {}).map(([col, v]) => `${code(col)} is ${Array.isArray(v) ? `one of ${list(v)}` : code(v)}`);
|
|
196
|
-
return parts.length ? `When ${parts.join(" and ")}: ` : "";
|
|
294
|
+
case "$unique": return `the combination of ${arg.map(c).join(", ")} is unique`;
|
|
295
|
+
default: return `${c(key)} ${renderCondition(arg, c)}`;
|
|
296
|
+
}
|
|
297
|
+
}).join(" and ");
|
|
197
298
|
}
|