@adecore/intelligent-ui 0.39.0 → 0.40.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/dist/compiler.js +10 -12
- package/dist/expression.js +18 -23
- package/dist/inputs.d.ts +1 -0
- package/dist/inputs.js +23 -20
- package/dist/links.js +6 -12
- package/dist/places.js +6 -12
- package/dist/rethrow.d.ts +1 -0
- package/dist/rethrow.js +7 -0
- package/dist/runtime.js +12 -12
- package/dist/syntax.js +5 -12
- package/package.json +4 -4
- package/src/actions.ts +7 -0
- package/src/budget.ts +9 -0
- package/src/compiler.ts +111 -15
- package/src/event-time.ts +14 -0
- package/src/event-warning.ts +2 -0
- package/src/expression.ts +146 -23
- package/src/fallback.ts +6 -0
- package/src/inputs.ts +37 -20
- package/src/links.ts +10 -13
- package/src/places.ts +9 -12
- package/src/prop-alias.ts +1 -0
- package/src/prop-list.ts +2 -0
- package/src/query.ts +15 -0
- package/src/rethrow.ts +8 -0
- package/src/runtime.ts +95 -19
- package/src/stream.ts +9 -0
- package/src/syntax.ts +97 -12
- package/src/text.ts +9 -0
package/src/expression.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { safeKey, UiBudget, UiFailure } from './budget.ts';
|
|
2
2
|
|
|
3
3
|
export type UiValue = null | boolean | number | string | UiValue[] | { [key: string]: UiValue };
|
|
4
|
+
|
|
4
5
|
export type UiExpression =
|
|
5
6
|
| { kind: 'literal'; value: UiValue }
|
|
6
7
|
| { kind: 'reference'; name: string }
|
|
@@ -33,7 +34,9 @@ const PRECEDENCE: Readonly<Record<string, number>> = {
|
|
|
33
34
|
'/': 6,
|
|
34
35
|
'%': 6
|
|
35
36
|
};
|
|
37
|
+
|
|
36
38
|
const HELPERS = new Set(['Count', 'Filter', 'Sum', 'Join', 'Round', 'Query', 'Set', 'Reset']);
|
|
39
|
+
const ESCAPES: Readonly<Record<string, string>> = { n: '\n', r: '\r', t: '\t', b: '\b', f: '\f', '\\': '\\', '"': '"', "'": "'", '/': '/' };
|
|
37
40
|
|
|
38
41
|
class ExpressionParser {
|
|
39
42
|
private position = 0;
|
|
@@ -44,26 +47,33 @@ class ExpressionParser {
|
|
|
44
47
|
constructor(source: string, budget: UiBudget) {
|
|
45
48
|
this.source = source;
|
|
46
49
|
this.budget = budget;
|
|
50
|
+
|
|
47
51
|
if (source.length > budget.limits.stringLength) {
|
|
48
52
|
throw new UiFailure('budget_exceeded', 'The expression is too long.');
|
|
49
53
|
}
|
|
54
|
+
|
|
50
55
|
this.token = this.read();
|
|
51
56
|
}
|
|
52
57
|
|
|
53
58
|
parse(): UiExpression {
|
|
54
59
|
const expression = this.expression(0, 0);
|
|
60
|
+
|
|
55
61
|
if (this.token.type !== 'end') {
|
|
56
62
|
throw new UiFailure('invalid_expression', `Unexpected token ${this.token.value}.`);
|
|
57
63
|
}
|
|
64
|
+
|
|
58
65
|
return expression;
|
|
59
66
|
}
|
|
60
67
|
|
|
61
68
|
private take(value?: string): Token {
|
|
62
69
|
const token = this.token;
|
|
70
|
+
|
|
63
71
|
if (value !== undefined && token.value !== value) {
|
|
64
72
|
throw new UiFailure('invalid_expression', `Expected ${value}.`);
|
|
65
73
|
}
|
|
74
|
+
|
|
66
75
|
this.token = this.read();
|
|
76
|
+
|
|
67
77
|
return token;
|
|
68
78
|
}
|
|
69
79
|
|
|
@@ -71,27 +81,34 @@ class ExpressionParser {
|
|
|
71
81
|
this.budget.depth(depth);
|
|
72
82
|
this.budget.node();
|
|
73
83
|
let left = this.primary(depth + 1);
|
|
84
|
+
|
|
74
85
|
while (true) {
|
|
75
86
|
const operator = this.token.value;
|
|
76
87
|
const precedence = Object.hasOwn(PRECEDENCE, operator) ? PRECEDENCE[operator] : 0;
|
|
88
|
+
|
|
77
89
|
if (precedence === 0 || precedence < minimum) {
|
|
78
90
|
break;
|
|
79
91
|
}
|
|
92
|
+
|
|
80
93
|
this.take();
|
|
81
94
|
this.budget.node();
|
|
82
95
|
left = { kind: 'binary', operator, left, right: this.expression(precedence + 1, depth + 1) };
|
|
83
96
|
}
|
|
97
|
+
|
|
84
98
|
return left;
|
|
85
99
|
}
|
|
86
100
|
|
|
87
101
|
private primary(depth: number): UiExpression {
|
|
88
102
|
const token = this.take();
|
|
89
103
|
let result: UiExpression;
|
|
104
|
+
|
|
90
105
|
if (token.type === 'number') {
|
|
91
106
|
const value = Number(token.value);
|
|
107
|
+
|
|
92
108
|
if (!Number.isFinite(value)) {
|
|
93
109
|
throw new UiFailure('invalid_expression', 'Numbers must be finite.');
|
|
94
110
|
}
|
|
111
|
+
|
|
95
112
|
result = { kind: 'literal', value };
|
|
96
113
|
} else if (token.type === 'string') {
|
|
97
114
|
result = { kind: 'literal', value: token.value };
|
|
@@ -101,54 +118,46 @@ class ExpressionParser {
|
|
|
101
118
|
result = this.expression(0, depth);
|
|
102
119
|
this.take(')');
|
|
103
120
|
} else if (token.value === '[') {
|
|
104
|
-
|
|
105
|
-
while (this.token.value !== ']') {
|
|
106
|
-
items.push(this.expression(0, depth));
|
|
107
|
-
if (this.token.value !== ',') {
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
this.take(',');
|
|
111
|
-
}
|
|
112
|
-
this.take(']');
|
|
113
|
-
result = { kind: 'array', items };
|
|
121
|
+
result = { kind: 'array', items: this.list(']', depth) };
|
|
114
122
|
} else if (token.value === '{') {
|
|
115
123
|
const entries: { key: string; value: UiExpression }[] = [];
|
|
116
124
|
const keys = new Set<string>();
|
|
125
|
+
|
|
117
126
|
while (this.token.value !== '}') {
|
|
118
127
|
const key = this.take();
|
|
128
|
+
|
|
119
129
|
if (key.type !== 'name' && key.type !== 'string') {
|
|
120
130
|
throw new UiFailure('invalid_expression', 'Expected an object key.');
|
|
121
131
|
}
|
|
132
|
+
|
|
122
133
|
safeKey(key.value);
|
|
134
|
+
|
|
123
135
|
if (keys.has(key.value)) {
|
|
124
136
|
throw new UiFailure('invalid_expression', 'Duplicate object key.');
|
|
125
137
|
}
|
|
138
|
+
|
|
126
139
|
keys.add(key.value);
|
|
127
140
|
this.take(':');
|
|
128
141
|
entries.push({ key: key.value, value: this.expression(0, depth) });
|
|
142
|
+
|
|
129
143
|
if (this.token.value !== ',') {
|
|
130
144
|
break;
|
|
131
145
|
}
|
|
146
|
+
|
|
132
147
|
this.take(',');
|
|
133
148
|
}
|
|
149
|
+
|
|
134
150
|
this.take('}');
|
|
135
151
|
result = { kind: 'object', entries };
|
|
136
152
|
} else if (token.type === 'name' && token.value.startsWith('@')) {
|
|
137
153
|
const name = token.value.slice(1);
|
|
154
|
+
|
|
138
155
|
if (!HELPERS.has(name)) {
|
|
139
156
|
throw new UiFailure('refused_call', `Unknown helper @${name}.`);
|
|
140
157
|
}
|
|
158
|
+
|
|
141
159
|
this.take('(');
|
|
142
|
-
|
|
143
|
-
while (this.token.value !== ')') {
|
|
144
|
-
args.push(this.expression(0, depth));
|
|
145
|
-
if (this.token.value !== ',') {
|
|
146
|
-
break;
|
|
147
|
-
}
|
|
148
|
-
this.take(',');
|
|
149
|
-
}
|
|
150
|
-
this.take(')');
|
|
151
|
-
result = { kind: 'call', name, args };
|
|
160
|
+
result = { kind: 'call', name, args: this.list(')', depth) };
|
|
152
161
|
} else if (token.type === 'name' && ['true', 'false', 'null'].includes(token.value)) {
|
|
153
162
|
result = { kind: 'literal', value: token.value === 'null' ? null : token.value === 'true' };
|
|
154
163
|
} else if (token.type === 'name') {
|
|
@@ -156,14 +165,18 @@ class ExpressionParser {
|
|
|
156
165
|
} else {
|
|
157
166
|
throw new UiFailure('invalid_expression', `Unexpected token ${token.value}.`);
|
|
158
167
|
}
|
|
168
|
+
|
|
159
169
|
while (this.token.value === '.' || this.token.value === '[') {
|
|
160
170
|
this.budget.depth(++depth);
|
|
161
171
|
this.budget.node();
|
|
172
|
+
|
|
162
173
|
if (this.take().value === '.') {
|
|
163
174
|
const key = this.take();
|
|
175
|
+
|
|
164
176
|
if (key.type !== 'name' || key.value.startsWith('$') || key.value.startsWith('@')) {
|
|
165
177
|
throw new UiFailure('invalid_expression', 'Expected a field name.');
|
|
166
178
|
}
|
|
179
|
+
|
|
167
180
|
result = { kind: 'member', object: result, key: { kind: 'literal', value: safeKey(key.value) } };
|
|
168
181
|
} else {
|
|
169
182
|
const key = this.expression(0, depth);
|
|
@@ -171,43 +184,73 @@ class ExpressionParser {
|
|
|
171
184
|
result = { kind: 'member', object: result, key };
|
|
172
185
|
}
|
|
173
186
|
}
|
|
187
|
+
|
|
174
188
|
return result;
|
|
175
189
|
}
|
|
176
190
|
|
|
191
|
+
/* Expressions separated by commas up to `close`, which it takes too. */
|
|
192
|
+
private list(close: string, depth: number): UiExpression[] {
|
|
193
|
+
const items: UiExpression[] = [];
|
|
194
|
+
|
|
195
|
+
while (this.token.value !== close) {
|
|
196
|
+
items.push(this.expression(0, depth));
|
|
197
|
+
|
|
198
|
+
if (this.token.value !== ',') {
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
this.take(',');
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
this.take(close);
|
|
206
|
+
|
|
207
|
+
return items;
|
|
208
|
+
}
|
|
209
|
+
|
|
177
210
|
private read(): Token {
|
|
178
211
|
this.budget.step();
|
|
212
|
+
|
|
179
213
|
while (/\s/.test(this.source[this.position] ?? '') && this.position < this.source.length) {
|
|
180
214
|
this.position += 1;
|
|
181
215
|
this.budget.step();
|
|
182
216
|
}
|
|
217
|
+
|
|
183
218
|
if (this.position === this.source.length) {
|
|
184
219
|
return { type: 'end', value: '' };
|
|
185
220
|
}
|
|
221
|
+
|
|
186
222
|
const rest = this.source.slice(this.position);
|
|
187
223
|
const first = rest[0];
|
|
224
|
+
|
|
188
225
|
if (first === '"' || first === "'") {
|
|
189
226
|
this.position += 1;
|
|
190
227
|
let value = '';
|
|
191
228
|
let closed = false;
|
|
229
|
+
|
|
192
230
|
while (this.position < this.source.length) {
|
|
193
231
|
this.budget.step();
|
|
194
232
|
let character = this.source[this.position++];
|
|
233
|
+
|
|
195
234
|
if (character === first) {
|
|
196
235
|
closed = true;
|
|
236
|
+
|
|
197
237
|
break;
|
|
198
238
|
}
|
|
239
|
+
|
|
199
240
|
if (character === '\\') {
|
|
200
241
|
character = this.source[this.position++];
|
|
201
|
-
|
|
242
|
+
|
|
202
243
|
if (character === 'u') {
|
|
203
244
|
const hex = this.source.slice(this.position, this.position + 4);
|
|
245
|
+
|
|
204
246
|
if (!/^[a-f\d]{4}$/i.test(hex)) {
|
|
205
247
|
throw new UiFailure('invalid_expression', 'Invalid Unicode escape.');
|
|
206
248
|
}
|
|
249
|
+
|
|
207
250
|
value += String.fromCharCode(Number.parseInt(hex, 16));
|
|
208
251
|
this.position += 4;
|
|
209
|
-
} else if (Object.hasOwn(
|
|
210
|
-
value +=
|
|
252
|
+
} else if (Object.hasOwn(ESCAPES, character)) {
|
|
253
|
+
value += ESCAPES[character];
|
|
211
254
|
} else {
|
|
212
255
|
throw new UiFailure('invalid_expression', 'Invalid string escape.');
|
|
213
256
|
}
|
|
@@ -215,26 +258,38 @@ class ExpressionParser {
|
|
|
215
258
|
value += character;
|
|
216
259
|
}
|
|
217
260
|
}
|
|
261
|
+
|
|
218
262
|
if (!closed) {
|
|
219
263
|
throw new UiFailure('incomplete_expression', 'The string is not closed.');
|
|
220
264
|
}
|
|
265
|
+
|
|
221
266
|
return { type: 'string', value: this.budget.string(value) };
|
|
222
267
|
}
|
|
268
|
+
|
|
223
269
|
const numeric = /^(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/.exec(rest);
|
|
270
|
+
|
|
224
271
|
if (numeric) {
|
|
225
272
|
this.position += numeric[0].length;
|
|
273
|
+
|
|
226
274
|
return { type: 'number', value: numeric[0] };
|
|
227
275
|
}
|
|
276
|
+
|
|
228
277
|
const name = /^[$@]?[A-Za-z_][A-Za-z_\d]*/.exec(rest);
|
|
278
|
+
|
|
229
279
|
if (name) {
|
|
230
280
|
this.position += name[0].length;
|
|
281
|
+
|
|
231
282
|
return { type: 'name', value: name[0] };
|
|
232
283
|
}
|
|
284
|
+
|
|
233
285
|
const symbol = /^(?:===|!==|==|!=|<=|>=|&&|\|\||[()[\]{},.:!+*/%<>-])/.exec(rest);
|
|
286
|
+
|
|
234
287
|
if (symbol) {
|
|
235
288
|
this.position += symbol[0].length;
|
|
289
|
+
|
|
236
290
|
return { type: 'symbol', value: symbol[0] };
|
|
237
291
|
}
|
|
292
|
+
|
|
238
293
|
throw new UiFailure('invalid_expression', `Unexpected character ${first}.`);
|
|
239
294
|
}
|
|
240
295
|
}
|
|
@@ -248,54 +303,72 @@ export function sameUiValue(first: UiValue, second: UiValue): boolean {
|
|
|
248
303
|
if (first === second) {
|
|
249
304
|
return true;
|
|
250
305
|
}
|
|
306
|
+
|
|
251
307
|
if (first === null || second === null || typeof first !== 'object' || typeof second !== 'object' || Array.isArray(first) !== Array.isArray(second)) {
|
|
252
308
|
return false;
|
|
253
309
|
}
|
|
310
|
+
|
|
254
311
|
if (Array.isArray(first) && Array.isArray(second)) {
|
|
255
312
|
return first.length === second.length && first.every((item, i) => sameUiValue(item, second[i]));
|
|
256
313
|
}
|
|
314
|
+
|
|
257
315
|
const left = first as Record<string, UiValue>;
|
|
258
316
|
const right = second as Record<string, UiValue>;
|
|
259
317
|
const keys = Object.keys(left);
|
|
318
|
+
|
|
260
319
|
return keys.length === Object.keys(right).length && keys.every((key) => Object.hasOwn(right, key) && sameUiValue(left[key], right[key]));
|
|
261
320
|
}
|
|
262
321
|
|
|
263
322
|
export function copyUiValue(value: unknown, budget = new UiBudget(), depth = 0): UiValue {
|
|
264
323
|
budget.depth(depth);
|
|
324
|
+
|
|
265
325
|
if (value === null || typeof value === 'boolean') {
|
|
266
326
|
return value;
|
|
267
327
|
}
|
|
328
|
+
|
|
268
329
|
if (typeof value === 'number' && Number.isFinite(value)) {
|
|
269
330
|
return value;
|
|
270
331
|
}
|
|
332
|
+
|
|
271
333
|
if (typeof value === 'string') {
|
|
272
334
|
return budget.string(value);
|
|
273
335
|
}
|
|
336
|
+
|
|
274
337
|
if (Array.isArray(value)) {
|
|
275
338
|
const result: UiValue[] = [];
|
|
339
|
+
|
|
276
340
|
for (let i = 0; i < value.length; i++) {
|
|
277
341
|
budget.iteration();
|
|
278
342
|
const descriptor = Object.getOwnPropertyDescriptor(value, String(i));
|
|
343
|
+
|
|
279
344
|
if (!descriptor || !Object.hasOwn(descriptor, 'value')) {
|
|
280
345
|
throw new UiFailure('refused_access', 'Sparse arrays and accessors are refused.');
|
|
281
346
|
}
|
|
347
|
+
|
|
282
348
|
result.push(copyUiValue(descriptor.value, budget, depth + 1));
|
|
283
349
|
}
|
|
350
|
+
|
|
284
351
|
return result;
|
|
285
352
|
}
|
|
353
|
+
|
|
286
354
|
if (typeof value === 'object' && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)) {
|
|
287
355
|
const result: Record<string, UiValue> = Object.create(null);
|
|
356
|
+
|
|
288
357
|
for (const key of Object.keys(value)) {
|
|
289
358
|
budget.iteration();
|
|
290
359
|
safeKey(key);
|
|
291
360
|
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
361
|
+
|
|
292
362
|
if (!descriptor || !Object.hasOwn(descriptor, 'value')) {
|
|
293
363
|
throw new UiFailure('refused_access', 'Accessors are refused.');
|
|
294
364
|
}
|
|
365
|
+
|
|
295
366
|
result[key] = copyUiValue(descriptor.value, budget, depth + 1);
|
|
296
367
|
}
|
|
368
|
+
|
|
297
369
|
return result;
|
|
298
370
|
}
|
|
371
|
+
|
|
299
372
|
throw new UiFailure('invalid_value', 'Only finite JSON values are allowed.');
|
|
300
373
|
}
|
|
301
374
|
|
|
@@ -303,6 +376,7 @@ function scalarText(value: UiValue): string {
|
|
|
303
376
|
if (value !== null && typeof value === 'object') {
|
|
304
377
|
throw new UiFailure('invalid_value', 'Expected a scalar.');
|
|
305
378
|
}
|
|
379
|
+
|
|
306
380
|
return value === null ? '' : String(value);
|
|
307
381
|
}
|
|
308
382
|
|
|
@@ -310,6 +384,7 @@ function numeric(value: UiValue): number {
|
|
|
310
384
|
if (typeof value !== 'number') {
|
|
311
385
|
throw new UiFailure('invalid_value', 'Expected a number.');
|
|
312
386
|
}
|
|
387
|
+
|
|
313
388
|
return value;
|
|
314
389
|
}
|
|
315
390
|
|
|
@@ -317,6 +392,7 @@ function list(value: UiValue): UiValue[] {
|
|
|
317
392
|
if (!Array.isArray(value)) {
|
|
318
393
|
throw new UiFailure('invalid_value', 'Expected a list.');
|
|
319
394
|
}
|
|
395
|
+
|
|
320
396
|
return value;
|
|
321
397
|
}
|
|
322
398
|
|
|
@@ -324,27 +400,35 @@ function ownValue(value: UiValue, key: UiValue): UiValue {
|
|
|
324
400
|
if (typeof key !== 'string' && typeof key !== 'number') {
|
|
325
401
|
throw new UiFailure('refused_access', 'Expected a field name or an index.');
|
|
326
402
|
}
|
|
403
|
+
|
|
327
404
|
const name = safeKey(String(key));
|
|
405
|
+
|
|
328
406
|
if (value === null || typeof value !== 'object') {
|
|
329
407
|
throw new UiFailure('invalid_value', 'Expected a record or a list.');
|
|
330
408
|
}
|
|
409
|
+
|
|
331
410
|
const descriptor = Object.getOwnPropertyDescriptor(value, name);
|
|
411
|
+
|
|
332
412
|
if (!descriptor || !Object.hasOwn(descriptor, 'value')) {
|
|
333
413
|
throw new UiFailure('refused_access', `Unknown own field ${name}.`);
|
|
334
414
|
}
|
|
415
|
+
|
|
335
416
|
return descriptor.value;
|
|
336
417
|
}
|
|
337
418
|
|
|
338
419
|
export function evaluateUiExpression(expression: UiExpression, variables: Readonly<Record<string, UiValue>> = {}, budget = new UiBudget()): UiValue {
|
|
339
420
|
// Copy at the boundary so host objects and getters never enter the interpreter.
|
|
340
421
|
const scope = copyUiValue(variables, budget) as Record<string, UiValue>;
|
|
422
|
+
|
|
341
423
|
return evaluate(expression, scope, budget, 0);
|
|
342
424
|
}
|
|
343
425
|
|
|
344
426
|
function evaluate(expression: UiExpression, scope: Readonly<Record<string, UiValue>>, budget: UiBudget, depth: number): UiValue {
|
|
345
427
|
budget.depth(depth);
|
|
346
428
|
budget.step();
|
|
429
|
+
|
|
347
430
|
const child = (item: UiExpression, nextScope = scope) => evaluate(item, nextScope, budget, depth + 1);
|
|
431
|
+
|
|
348
432
|
switch (expression.kind) {
|
|
349
433
|
case 'literal':
|
|
350
434
|
return copyUiValue(expression.value, budget);
|
|
@@ -352,63 +436,80 @@ function evaluate(expression: UiExpression, scope: Readonly<Record<string, UiVal
|
|
|
352
436
|
return ownValue(scope, expression.name);
|
|
353
437
|
case 'array': {
|
|
354
438
|
const result: UiValue[] = [];
|
|
439
|
+
|
|
355
440
|
for (const item of expression.items) {
|
|
356
441
|
budget.iteration();
|
|
357
442
|
result.push(child(item));
|
|
358
443
|
}
|
|
444
|
+
|
|
359
445
|
return result;
|
|
360
446
|
}
|
|
361
447
|
case 'object': {
|
|
362
448
|
const result: Record<string, UiValue> = Object.create(null);
|
|
449
|
+
|
|
363
450
|
for (const entry of expression.entries) {
|
|
364
451
|
budget.iteration();
|
|
365
452
|
result[safeKey(entry.key)] = child(entry.value);
|
|
366
453
|
}
|
|
454
|
+
|
|
367
455
|
return result;
|
|
368
456
|
}
|
|
369
457
|
case 'member':
|
|
370
458
|
return ownValue(child(expression.object), child(expression.key));
|
|
371
459
|
case 'unary': {
|
|
372
460
|
const value = child(expression.value);
|
|
461
|
+
|
|
373
462
|
if (expression.operator === '!') {
|
|
374
463
|
return !value;
|
|
375
464
|
}
|
|
465
|
+
|
|
376
466
|
if (expression.operator === '-') {
|
|
377
467
|
return -numeric(value);
|
|
378
468
|
}
|
|
469
|
+
|
|
379
470
|
if (expression.operator === '+') {
|
|
380
471
|
return numeric(value);
|
|
381
472
|
}
|
|
473
|
+
|
|
382
474
|
throw new UiFailure('invalid_expression', 'Unknown unary operator.');
|
|
383
475
|
}
|
|
384
476
|
case 'binary': {
|
|
385
477
|
const left = child(expression.left);
|
|
478
|
+
|
|
386
479
|
if (expression.operator === '&&') {
|
|
387
480
|
return left ? child(expression.right) : left;
|
|
388
481
|
}
|
|
482
|
+
|
|
389
483
|
if (expression.operator === '||') {
|
|
390
484
|
return left ? left : child(expression.right);
|
|
391
485
|
}
|
|
486
|
+
|
|
392
487
|
const right = child(expression.right);
|
|
393
488
|
let result: UiValue;
|
|
489
|
+
|
|
394
490
|
switch (expression.operator) {
|
|
395
491
|
case '+':
|
|
396
492
|
result =
|
|
397
493
|
typeof left === 'string' || typeof right === 'string'
|
|
398
494
|
? budget.string(scalarText(left) + scalarText(right))
|
|
399
495
|
: numeric(left) + numeric(right);
|
|
496
|
+
|
|
400
497
|
break;
|
|
401
498
|
case '-':
|
|
402
499
|
result = numeric(left) - numeric(right);
|
|
500
|
+
|
|
403
501
|
break;
|
|
404
502
|
case '*':
|
|
405
503
|
result = numeric(left) * numeric(right);
|
|
504
|
+
|
|
406
505
|
break;
|
|
407
506
|
case '/':
|
|
408
507
|
result = numeric(left) / numeric(right);
|
|
508
|
+
|
|
409
509
|
break;
|
|
410
510
|
case '%':
|
|
411
511
|
result = numeric(left) % numeric(right);
|
|
512
|
+
|
|
412
513
|
break;
|
|
413
514
|
case '==':
|
|
414
515
|
case '===':
|
|
@@ -427,47 +528,60 @@ function evaluate(expression: UiExpression, scope: Readonly<Record<string, UiVal
|
|
|
427
528
|
default:
|
|
428
529
|
throw new UiFailure('invalid_expression', 'Unknown binary operator.');
|
|
429
530
|
}
|
|
531
|
+
|
|
430
532
|
if (typeof result === 'number' && !Number.isFinite(result)) {
|
|
431
533
|
throw new UiFailure('invalid_value', 'The result must be finite.');
|
|
432
534
|
}
|
|
535
|
+
|
|
433
536
|
return result;
|
|
434
537
|
}
|
|
435
538
|
case 'call': {
|
|
436
539
|
const args = expression.args;
|
|
540
|
+
|
|
437
541
|
const requireArgs = (minimum: number, maximum = minimum) => {
|
|
438
542
|
if (args.length < minimum || args.length > maximum) {
|
|
439
543
|
throw new UiFailure('invalid_expression', `Invalid arguments for @${expression.name}.`);
|
|
440
544
|
}
|
|
441
545
|
};
|
|
546
|
+
|
|
442
547
|
switch (expression.name) {
|
|
443
548
|
case 'Count': {
|
|
444
549
|
requireArgs(1);
|
|
445
550
|
const value = child(args[0]);
|
|
551
|
+
|
|
446
552
|
if (typeof value === 'string') {
|
|
447
553
|
budget.string(value);
|
|
554
|
+
|
|
448
555
|
return value.length;
|
|
449
556
|
}
|
|
557
|
+
|
|
450
558
|
return list(value).length;
|
|
451
559
|
}
|
|
452
560
|
case 'Filter': {
|
|
453
561
|
requireArgs(3);
|
|
454
562
|
const items = list(child(args[0]));
|
|
455
563
|
const variable = args[1];
|
|
564
|
+
|
|
456
565
|
if (variable.kind !== 'reference' || variable.name.startsWith('$') || variable.name.startsWith('@')) {
|
|
457
566
|
throw new UiFailure('invalid_expression', '@Filter needs a local name as its second argument.');
|
|
458
567
|
}
|
|
568
|
+
|
|
459
569
|
safeKey(variable.name);
|
|
460
570
|
const result: UiValue[] = [];
|
|
571
|
+
|
|
461
572
|
for (const item of items) {
|
|
462
573
|
budget.iteration();
|
|
463
574
|
const match = child(args[2], { ...scope, [variable.name]: item });
|
|
575
|
+
|
|
464
576
|
if (typeof match !== 'boolean') {
|
|
465
577
|
throw new UiFailure('invalid_value', '@Filter needs a boolean predicate.');
|
|
466
578
|
}
|
|
579
|
+
|
|
467
580
|
if (match) {
|
|
468
581
|
result.push(item);
|
|
469
582
|
}
|
|
470
583
|
}
|
|
584
|
+
|
|
471
585
|
return result;
|
|
472
586
|
}
|
|
473
587
|
case 'Sum': {
|
|
@@ -475,13 +589,16 @@ function evaluate(expression: UiExpression, scope: Readonly<Record<string, UiVal
|
|
|
475
589
|
const items = list(child(args[0]));
|
|
476
590
|
const key = args.length === 2 ? child(args[1]) : null;
|
|
477
591
|
let total = 0;
|
|
592
|
+
|
|
478
593
|
for (const item of items) {
|
|
479
594
|
budget.iteration();
|
|
480
595
|
total += numeric(key === null ? item : ownValue(item, key));
|
|
596
|
+
|
|
481
597
|
if (!Number.isFinite(total)) {
|
|
482
598
|
throw new UiFailure('invalid_value', 'The sum must be finite.');
|
|
483
599
|
}
|
|
484
600
|
}
|
|
601
|
+
|
|
485
602
|
return total;
|
|
486
603
|
}
|
|
487
604
|
case 'Join': {
|
|
@@ -489,24 +606,30 @@ function evaluate(expression: UiExpression, scope: Readonly<Record<string, UiVal
|
|
|
489
606
|
const items = list(child(args[0]));
|
|
490
607
|
const separator = args.length === 2 ? scalarText(child(args[1])) : ', ';
|
|
491
608
|
let result = '';
|
|
609
|
+
|
|
492
610
|
for (let i = 0; i < items.length; i++) {
|
|
493
611
|
budget.iteration();
|
|
494
612
|
result = budget.string(result + (i === 0 ? '' : separator) + scalarText(items[i]));
|
|
495
613
|
}
|
|
614
|
+
|
|
496
615
|
return result;
|
|
497
616
|
}
|
|
498
617
|
case 'Round': {
|
|
499
618
|
requireArgs(1, 2);
|
|
500
619
|
const value = numeric(child(args[0]));
|
|
501
620
|
const digits = args.length === 2 ? numeric(child(args[1])) : 0;
|
|
621
|
+
|
|
502
622
|
if (!Number.isInteger(digits) || digits < 0 || digits > 6) {
|
|
503
623
|
throw new UiFailure('invalid_value', 'Precision must be an integer from zero to six.');
|
|
504
624
|
}
|
|
625
|
+
|
|
505
626
|
const factor = 10 ** digits;
|
|
506
627
|
const result = Math.round(value * factor) / factor;
|
|
628
|
+
|
|
507
629
|
if (!Number.isFinite(result)) {
|
|
508
630
|
throw new UiFailure('invalid_value', 'The rounded value must be finite.');
|
|
509
631
|
}
|
|
632
|
+
|
|
510
633
|
return result;
|
|
511
634
|
}
|
|
512
635
|
case 'Query':
|
package/src/fallback.ts
CHANGED
|
@@ -13,6 +13,7 @@ function given(value: unknown): boolean {
|
|
|
13
13
|
*/
|
|
14
14
|
export function uiNodeFallback(type: string, props: Readonly<Record<string, unknown>>, children: string): string {
|
|
15
15
|
const label = props.label ?? props.title;
|
|
16
|
+
|
|
16
17
|
switch (type) {
|
|
17
18
|
case '$text':
|
|
18
19
|
return uiPlainText(props.text);
|
|
@@ -27,6 +28,7 @@ export function uiNodeFallback(type: string, props: Readonly<Record<string, unkn
|
|
|
27
28
|
case 'Stat': {
|
|
28
29
|
const unit = given(props.unit) ? ` ${uiPlainText(props.unit)}` : '';
|
|
29
30
|
const previous = given(props.previous) ? ` (was ${uiPlainText(props.previous)}${unit})` : '';
|
|
31
|
+
|
|
30
32
|
return `${uiPlainText(label)}: ${uiPlainText(props.value)}${unit}${previous}\n`;
|
|
31
33
|
}
|
|
32
34
|
case 'Source':
|
|
@@ -38,6 +40,7 @@ export function uiNodeFallback(type: string, props: Readonly<Record<string, unkn
|
|
|
38
40
|
.split('\n')
|
|
39
41
|
.map((column) => column.trim())
|
|
40
42
|
.filter(Boolean);
|
|
43
|
+
|
|
41
44
|
return `${columns.length > 0 ? `Columns: ${columns.join(', ')}\n` : ''}${uiPlainText(props.rows ?? [])}\n`;
|
|
42
45
|
}
|
|
43
46
|
case 'Chart':
|
|
@@ -48,6 +51,7 @@ export function uiNodeFallback(type: string, props: Readonly<Record<string, unkn
|
|
|
48
51
|
const point = given(props.lat) && given(props.lon) ? ` (${uiPlainText(props.lat)}, ${uiPlainText(props.lon)})` : '';
|
|
49
52
|
const address = given(props.address) ? `, ${uiPlainText(props.address)}` : '';
|
|
50
53
|
const detail = given(props.detail) ? `: ${uiPlainText(props.detail)}` : '';
|
|
54
|
+
|
|
51
55
|
// One line per place, so the numbering of its map counts places and not the lines of a name.
|
|
52
56
|
return `${children.trim()}${point}${address}${detail}`.replace(/\s+/g, ' ').trim() + '\n';
|
|
53
57
|
}
|
|
@@ -59,9 +63,11 @@ export function uiNodeFallback(type: string, props: Readonly<Record<string, unkn
|
|
|
59
63
|
case 'Model':
|
|
60
64
|
return `[Model: ${uiPlainText(props.alt ?? props.attachment ?? 'unavailable')}]${children.trim() ? ` ${children.trim()}` : ''}\n`;
|
|
61
65
|
}
|
|
66
|
+
|
|
62
67
|
const path = given(props.path) && given(props.line) ? `${uiPlainText(props.path)}:${uiPlainText(props.line)}` : props.path;
|
|
63
68
|
const target = path ?? props.sha ?? (type === 'Node' ? props.id : undefined);
|
|
64
69
|
const context = props.context ? `\n${uiPlainText(props.context)}` : '';
|
|
65
70
|
const text = `${label !== undefined ? `${uiPlainText(label)}: ` : ''}${target !== undefined ? `${uiPlainText(target)} ` : ''}${children}${context}`;
|
|
71
|
+
|
|
66
72
|
return type === 'Tag' || text.endsWith('\n') ? text : `${text}\n`;
|
|
67
73
|
}
|