@niscorp/prism 0.3.3 → 0.4.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/CHANGELOG.md +113 -0
- package/DESIGN.md +24 -13
- package/DOCS.md +104 -15
- package/dist/agent.cjs +4 -4
- package/dist/agent.js +2 -2
- package/dist/{chunk-OL4CCYWX.js → chunk-E44DNCG3.js} +40 -23
- package/dist/chunk-E44DNCG3.js.map +1 -0
- package/dist/{chunk-ROYHYYIL.cjs → chunk-NBU7EBGN.cjs} +5 -5
- package/dist/{chunk-ROYHYYIL.cjs.map → chunk-NBU7EBGN.cjs.map} +1 -1
- package/dist/{chunk-QUI2X5JB.cjs → chunk-VXU22DMV.cjs} +41 -22
- package/dist/chunk-VXU22DMV.cjs.map +1 -0
- package/dist/{chunk-VNGBVDUM.js → chunk-ZMMUIMAK.js} +3 -3
- package/dist/{chunk-VNGBVDUM.js.map → chunk-ZMMUIMAK.js.map} +1 -1
- package/dist/examples/index.cjs +1 -1
- package/dist/examples/index.cjs.map +1 -1
- package/dist/examples/index.d.cts +1 -1
- package/dist/examples/index.d.ts +1 -1
- package/dist/examples/index.js +1 -1
- package/dist/examples/index.js.map +1 -1
- package/dist/index.cjs +417 -377
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +225 -185
- package/dist/index.js.map +1 -1
- package/dist/migrations/index.cjs +6 -2
- package/dist/migrations/index.cjs.map +1 -1
- package/dist/migrations/index.js +5 -1
- package/dist/migrations/index.js.map +1 -1
- package/dist/{profiles-CPVfysxC.d.cts → profiles-BhAR8Ti3.d.cts} +1 -0
- package/dist/{profiles-CPVfysxC.d.ts → profiles-BhAR8Ti3.d.ts} +1 -0
- package/package.json +7 -7
- package/dist/chunk-OL4CCYWX.js.map +0 -1
- package/dist/chunk-QUI2X5JB.cjs.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { MAPPING_OPS, getConfigJsonSchema, getNodeJsonSchema, getProfileJsonSchema } from './chunk-
|
|
2
|
-
import { PrismError, ErrorCode, ConfigSchema, isWithNode, isRenameKeysNode, isConstNode, isSumNode, isAvgNode, isCountNode, isMinNode, isMaxNode, isPluckNode, isTakeNode, isDropNode, isMatchNode, isFlatMapNode, isJsonObject, takeOrigin, formOf, isRefNode, parseJsonPath, isVarNode, isPlainObject, OPTIONAL_FIELDS_KEY, isGetNode, isMapNode, isFilterNode, isReduceNode, isSliceNode, isFlattenNode, isUniqueNode, isSortByNode, isAddNode, isSubNode, isMulNode, isDivNode, isRoundNode, isModNode, isToNumberNode, isJoinNode, isToStringNode, isInterpolateNode, isFillNode, isTrimNode, isLowerNode, isUpperNode, isSplitNode, isReplaceNode, isEqNode, isNeqNode, isGtNode, isGteNode, isLtNode, isLteNode, isEmptyNode, isStartsWithNode, isEndsWithNode, isContainsNode, isNotNode, isAndNode, isOrNode, isMergeNode, isCoalesceNode, isCaseNode, isEntriesOfNode, isKeyByNode, isGroupByNode, isKeysNode, isValuesNode, isFromEntriesNode, isPickNode, isOmitNode, isTypeNode, isLengthNode, isHasNode, isUpdateNode, isAssertNode, isWalkNode, isDateNode, isDateAddNode, isDateDiffNode, isLocaleDateNode, isLocaleMoneyNode, isLocaleNumberNode,
|
|
3
|
-
export { ConfigSchema, ErrorCode, NodeSchema, OP_KEYS, PrismError } from './chunk-
|
|
1
|
+
export { MAPPING_OPS, getConfigJsonSchema, getNodeJsonSchema, getProfileJsonSchema } from './chunk-ZMMUIMAK.js';
|
|
2
|
+
import { PrismError, ErrorCode, ConfigSchema, isWithNode, isRenameKeysNode, isConstNode, isSumNode, isAvgNode, isCountNode, isMinNode, isMaxNode, isPluckNode, isTakeNode, isDropNode, isMatchNode, isFlatMapNode, isJsonObject, takeOrigin, formOf, isRefNode, parseJsonPath, isVarNode, isPlainObject, OPTIONAL_FIELDS_KEY, writeOwnKey, failAt, clip, isGetNode, isMapNode, isFilterNode, isReduceNode, isSliceNode, isFlattenNode, isUniqueNode, isSortByNode, isAddNode, isSubNode, isMulNode, isDivNode, isRoundNode, isModNode, isToNumberNode, isJoinNode, isToStringNode, isInterpolateNode, isFillNode, isTrimNode, isLowerNode, isUpperNode, isSplitNode, isReplaceNode, isEqNode, isNeqNode, isGtNode, isGteNode, isLtNode, isLteNode, isEmptyNode, isStartsWithNode, isEndsWithNode, isContainsNode, isNotNode, isAndNode, isOrNode, isMergeNode, isCoalesceNode, isCaseNode, isEntriesOfNode, isKeyByNode, isGroupByNode, isKeysNode, isValuesNode, isFromEntriesNode, isPickNode, isOmitNode, isTypeNode, isLengthNode, isHasNode, isUpdateNode, isAssertNode, isWalkNode, isDateNode, isDateAddNode, isDateDiffNode, isLocaleDateNode, isLocaleMoneyNode, isLocaleNumberNode, parseJsonPathCached, getByPath, whereStopped, jsonPathUpTo, readOwnKey, isJsonArray, INDEX_TEXT, describeValue } from './chunk-E44DNCG3.js';
|
|
3
|
+
export { ConfigSchema, ErrorCode, NodeSchema, OP_KEYS, PrismError } from './chunk-E44DNCG3.js';
|
|
4
4
|
import { exceedsDepth, depthRefusal } from '@niscorp/strata';
|
|
5
5
|
import dayjs from 'dayjs';
|
|
6
6
|
import utc from 'dayjs/plugin/utc.js';
|
|
@@ -103,7 +103,7 @@ var rewriteMin = (node, recurse) => ({
|
|
|
103
103
|
$case: {
|
|
104
104
|
branches: [
|
|
105
105
|
{ when: { $eq: [{ $var: "__acc" }, null] }, then: { $var: "__x" } },
|
|
106
|
-
{ when: { $lt: [{ $var: "__x" }, { $var: "__acc" }] }, then: { $var: "__x" } }
|
|
106
|
+
{ when: { $lt: [{ $coalesce: [{ $var: "__x" }, { $var: "__acc" }] }, { $var: "__acc" }] }, then: { $var: "__x" } }
|
|
107
107
|
],
|
|
108
108
|
else: { $var: "__acc" }
|
|
109
109
|
}
|
|
@@ -120,7 +120,7 @@ var rewriteMax = (node, recurse) => ({
|
|
|
120
120
|
$case: {
|
|
121
121
|
branches: [
|
|
122
122
|
{ when: { $eq: [{ $var: "__acc" }, null] }, then: { $var: "__x" } },
|
|
123
|
-
{ when: { $gt: [{ $var: "__x" }, { $var: "__acc" }] }, then: { $var: "__x" } }
|
|
123
|
+
{ when: { $gt: [{ $coalesce: [{ $var: "__x" }, { $var: "__acc" }] }, { $var: "__acc" }] }, then: { $var: "__x" } }
|
|
124
124
|
],
|
|
125
125
|
else: { $var: "__acc" }
|
|
126
126
|
}
|
|
@@ -209,8 +209,8 @@ var refuse = (what, limit, node) => {
|
|
|
209
209
|
if (typeof node === "object" && node !== null) return failAt(message, ErrorCode.BUDGET, { details: { limit } }, { node, form: false });
|
|
210
210
|
throw new PrismError(message, ErrorCode.BUDGET, { details: { limit } });
|
|
211
211
|
};
|
|
212
|
-
var
|
|
213
|
-
budget.steps +=
|
|
212
|
+
var spendSteps = (budget, node, steps = 1) => {
|
|
213
|
+
budget.steps += steps;
|
|
214
214
|
if (budget.steps > budget.limits.maxSteps) refuse("it evaluated too many nodes", budget.limits.maxSteps, node);
|
|
215
215
|
};
|
|
216
216
|
var sizeOf = (value, budget) => {
|
|
@@ -225,6 +225,8 @@ var sizeOf = (value, budget) => {
|
|
|
225
225
|
budget.sizes.set(value, size);
|
|
226
226
|
return size;
|
|
227
227
|
};
|
|
228
|
+
var STACK_OVERFLOW = /call stack/i;
|
|
229
|
+
var stackRefusal = (error) => error instanceof RangeError && STACK_OVERFLOW.test(error.message) ? new PrismError("Evaluation stopped: a value nests deeper than the call stack reaches, or refers back to itself.", ErrorCode.BUDGET) : error;
|
|
228
230
|
var measureString = (value, budget, node) => {
|
|
229
231
|
if (typeof value === "string" && value.length > budget.limits.maxStringLength) refuse(`a string of ${value.length} characters`, budget.limits.maxStringLength, node);
|
|
230
232
|
};
|
|
@@ -258,12 +260,17 @@ var copyOf = (value) => {
|
|
|
258
260
|
if (value === null || typeof value !== "object") return value;
|
|
259
261
|
if (Array.isArray(value)) return value.map(copyOf);
|
|
260
262
|
const copy = {};
|
|
261
|
-
for (const [key, inner] of Object.entries(value)) copy
|
|
263
|
+
for (const [key, inner] of Object.entries(value)) writeOwnKey(copy, key, copyOf(inner));
|
|
262
264
|
return copy;
|
|
263
265
|
};
|
|
264
|
-
var
|
|
266
|
+
var COST_KEY = "__cost";
|
|
267
|
+
var opConst = (node, context, _evaluate) => {
|
|
268
|
+
const cost = Reflect.get(node, COST_KEY);
|
|
269
|
+
if (typeof cost === "number" && context.budget !== void 0) spendSteps(context.budget, node, cost);
|
|
270
|
+
return context.kept === true ? copyOf(node.$const) : node.$const;
|
|
271
|
+
};
|
|
265
272
|
var opVar = (node, context, _evaluate) => {
|
|
266
|
-
const value = context.vars
|
|
273
|
+
const value = readOwnKey(context.vars, node.$var);
|
|
267
274
|
if (value === void 0) {
|
|
268
275
|
const names = Object.keys(context.vars).filter((name) => !name.startsWith("__"));
|
|
269
276
|
const scope = names.length === 0 ? "No variable is in scope here." : `In scope: ${names.join(", ")}.`;
|
|
@@ -279,6 +286,7 @@ var getStopped = (node, root, count, context, evaluate2) => {
|
|
|
279
286
|
});
|
|
280
287
|
return `$get.path ${JSON.stringify(steps)} ${whereStopped(root, steps, (upTo) => upTo === 0 ? "from" : JSON.stringify(steps.slice(0, upTo)), true)}`;
|
|
281
288
|
};
|
|
289
|
+
var absentAt = (current2) => current2 === null || current2 === void 0 ? { details: { absent: true } } : {};
|
|
282
290
|
var opGet = (node, context, evaluate2) => {
|
|
283
291
|
const { from, path, fallback } = node.$get;
|
|
284
292
|
const root = evaluate2(from, context);
|
|
@@ -289,7 +297,7 @@ var opGet = (node, context, evaluate2) => {
|
|
|
289
297
|
if (typeof evaluated === "number") {
|
|
290
298
|
if (!isJsonArray(current2)) {
|
|
291
299
|
if (fallback !== void 0) return evaluate2(fallback, context);
|
|
292
|
-
return failAt(getStopped(node, root, i + 1, context, evaluate2), ErrorCode.TYPE, { op: "$get" }, { node, op: "$get", field: "path", form: true });
|
|
300
|
+
return failAt(getStopped(node, root, i + 1, context, evaluate2), ErrorCode.TYPE, { op: "$get", ...absentAt(current2) }, { node, op: "$get", field: "path", form: true });
|
|
293
301
|
}
|
|
294
302
|
current2 = current2[evaluated];
|
|
295
303
|
} else {
|
|
@@ -300,9 +308,9 @@ var opGet = (node, context, evaluate2) => {
|
|
|
300
308
|
}
|
|
301
309
|
if (!isJsonObject(current2)) {
|
|
302
310
|
if (fallback !== void 0) return evaluate2(fallback, context);
|
|
303
|
-
return failAt(getStopped(node, root, i + 1, context, evaluate2), ErrorCode.TYPE, { op: "$get" }, { node, op: "$get", field: "path", form: true });
|
|
311
|
+
return failAt(getStopped(node, root, i + 1, context, evaluate2), ErrorCode.TYPE, { op: "$get", ...absentAt(current2) }, { node, op: "$get", field: "path", form: true });
|
|
304
312
|
}
|
|
305
|
-
current2 = current2
|
|
313
|
+
current2 = readOwnKey(current2, key);
|
|
306
314
|
}
|
|
307
315
|
}
|
|
308
316
|
if (current2 === void 0) {
|
|
@@ -315,8 +323,9 @@ var opWith = (node, context, evaluate2) => {
|
|
|
315
323
|
const { let: declarations, value } = node.$with;
|
|
316
324
|
const extendedVars = { ...context.vars };
|
|
317
325
|
for (const [name, expr] of Object.entries(declarations)) {
|
|
318
|
-
|
|
319
|
-
|
|
326
|
+
const bound = evaluate2(expr, context);
|
|
327
|
+
writeOwnKey(extendedVars, name, bound);
|
|
328
|
+
measure(bound, context.budget, node);
|
|
320
329
|
}
|
|
321
330
|
return evaluate2(value, { ...context, vars: extendedVars });
|
|
322
331
|
};
|
|
@@ -362,7 +371,7 @@ var opMap = (node, context, evaluate2) => {
|
|
|
362
371
|
const result = [];
|
|
363
372
|
const { scope, vars } = loopScope(context);
|
|
364
373
|
for (const item of input) {
|
|
365
|
-
vars
|
|
374
|
+
writeOwnKey(vars, as, item);
|
|
366
375
|
result.push(evaluate2(body, scope));
|
|
367
376
|
}
|
|
368
377
|
return result;
|
|
@@ -373,7 +382,7 @@ var opFilter = (node, context, evaluate2) => {
|
|
|
373
382
|
const result = [];
|
|
374
383
|
const { scope, vars } = loopScope(context);
|
|
375
384
|
for (const item of input) {
|
|
376
|
-
vars
|
|
385
|
+
writeOwnKey(vars, as, item);
|
|
377
386
|
const condition = evaluate2(when, scope);
|
|
378
387
|
if (condition) result.push(item);
|
|
379
388
|
}
|
|
@@ -385,17 +394,30 @@ var opReduce = (node, context, evaluate2) => {
|
|
|
385
394
|
let accumulator = evaluate2(init, context);
|
|
386
395
|
const { scope, vars } = loopScope(context);
|
|
387
396
|
for (const item of input) {
|
|
388
|
-
vars
|
|
389
|
-
vars
|
|
397
|
+
writeOwnKey(vars, as, item);
|
|
398
|
+
writeOwnKey(vars, accName, accumulator);
|
|
390
399
|
accumulator = evaluate2(body, scope);
|
|
391
400
|
measure(accumulator, context.budget, node);
|
|
392
401
|
}
|
|
393
402
|
return accumulator;
|
|
394
403
|
};
|
|
404
|
+
var isLowHalf = (text, at) => {
|
|
405
|
+
const code = text.charCodeAt(at);
|
|
406
|
+
const before = text.charCodeAt(at - 1);
|
|
407
|
+
return code >= 56320 && code <= 57343 && before >= 55296 && before <= 56319;
|
|
408
|
+
};
|
|
409
|
+
var unitAt = (text, index) => index < 0 ? Math.max(text.length + index, 0) : Math.min(index, text.length);
|
|
410
|
+
var sliceText = (text, start, end) => {
|
|
411
|
+
let from = unitAt(text, start ?? 0);
|
|
412
|
+
let to = end === void 0 ? text.length : unitAt(text, end);
|
|
413
|
+
if (from > 0 && from < text.length && isLowHalf(text, from)) from += 1;
|
|
414
|
+
if (to > 0 && to < text.length && isLowHalf(text, to)) to -= 1;
|
|
415
|
+
return from < to ? text.slice(from, to) : "";
|
|
416
|
+
};
|
|
395
417
|
var opSlice = (node, context, evaluate2) => {
|
|
396
418
|
const { from, start, end } = node.$slice;
|
|
397
419
|
const input = evaluate2(from, context);
|
|
398
|
-
if (typeof input === "string") return input
|
|
420
|
+
if (typeof input === "string") return sliceText(input, start, end);
|
|
399
421
|
if (isJsonArray(input)) return input.slice(start, end);
|
|
400
422
|
return refuseType({ node, op: "$slice", field: "from", needs: "a list or text", got: input, written: from, contextOp: "$slice" });
|
|
401
423
|
};
|
|
@@ -439,7 +461,7 @@ var opSortBy = (node, context, evaluate2) => {
|
|
|
439
461
|
const input = requireArray(evaluate2(over, context), node, "$sortBy", "over", over);
|
|
440
462
|
const { scope, vars } = loopScope(context);
|
|
441
463
|
const keyed = input.map((item) => {
|
|
442
|
-
vars
|
|
464
|
+
writeOwnKey(vars, as, item);
|
|
443
465
|
return { item, key: sortKey(evaluate2(by, scope), node) };
|
|
444
466
|
});
|
|
445
467
|
keyed.sort((a, b) => {
|
|
@@ -468,22 +490,23 @@ var requireNumbers = (node, pair, context, evaluate2, op) => {
|
|
|
468
490
|
return [a, b];
|
|
469
491
|
};
|
|
470
492
|
var refuseZero = (node, op) => failAt(`Division by zero: the second value of ${op} is 0.`, ErrorCode.DIVISION_BY_ZERO, { op }, { node, op, field: "1", form: false });
|
|
493
|
+
var finite = (result, node, op) => Number.isFinite(result) ? result : failAt(`${op} gave ${result}, which is not a JSON number.`, ErrorCode.TYPE, { op }, { node, op, form: false });
|
|
471
494
|
var opAdd = (node, context, evaluate2) => {
|
|
472
495
|
const [a, b] = requireNumbers(node, node.$add, context, evaluate2, "$add");
|
|
473
|
-
return a + b;
|
|
496
|
+
return finite(a + b, node, "$add");
|
|
474
497
|
};
|
|
475
498
|
var opSub = (node, context, evaluate2) => {
|
|
476
499
|
const [a, b] = requireNumbers(node, node.$sub, context, evaluate2, "$sub");
|
|
477
|
-
return a - b;
|
|
500
|
+
return finite(a - b, node, "$sub");
|
|
478
501
|
};
|
|
479
502
|
var opMul = (node, context, evaluate2) => {
|
|
480
503
|
const [a, b] = requireNumbers(node, node.$mul, context, evaluate2, "$mul");
|
|
481
|
-
return a * b;
|
|
504
|
+
return finite(a * b, node, "$mul");
|
|
482
505
|
};
|
|
483
506
|
var opDiv = (node, context, evaluate2) => {
|
|
484
507
|
const [a, b] = requireNumbers(node, node.$div, context, evaluate2, "$div");
|
|
485
508
|
if (b === 0) return refuseZero(node, "$div");
|
|
486
|
-
return a / b;
|
|
509
|
+
return finite(a / b, node, "$div");
|
|
487
510
|
};
|
|
488
511
|
var opMod = (node, context, evaluate2) => {
|
|
489
512
|
const [a, b] = requireNumbers(node, node.$mod, context, evaluate2, "$mod");
|
|
@@ -493,12 +516,17 @@ var opMod = (node, context, evaluate2) => {
|
|
|
493
516
|
return remainder < 0 !== b < 0 ? remainder + b : remainder;
|
|
494
517
|
};
|
|
495
518
|
var ROUNDING = { nearest: Math.round, floor: Math.floor, ceil: Math.ceil };
|
|
519
|
+
var shifted = (value, digits) => {
|
|
520
|
+
const [mantissa, exponent = "0"] = String(value).split("e");
|
|
521
|
+
return Number(`${mantissa}e${Number(exponent) + digits}`);
|
|
522
|
+
};
|
|
496
523
|
var opRound = (node, context, evaluate2) => {
|
|
497
524
|
const value = evaluate2(node.$round.value, context);
|
|
498
525
|
if (typeof value !== "number") return refuseType({ node, op: "$round", field: "value", needs: "a number", got: value, written: node.$round.value });
|
|
499
526
|
const digits = node.$round.digits ?? 0;
|
|
500
|
-
const
|
|
501
|
-
|
|
527
|
+
const moved = shifted(value, digits);
|
|
528
|
+
if (!Number.isFinite(moved)) return value;
|
|
529
|
+
return shifted(ROUNDING[node.$round.mode ?? "nearest"](moved), -digits);
|
|
502
530
|
};
|
|
503
531
|
var NUMERIC_TEXT = /^[+-]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/;
|
|
504
532
|
var opToNumber = (node, context, evaluate2) => {
|
|
@@ -538,8 +566,8 @@ var opInterpolate = (node, context, evaluate2) => {
|
|
|
538
566
|
const { template, values: valuesNode } = node.$interpolate;
|
|
539
567
|
const values = evaluate2(valuesNode, context);
|
|
540
568
|
if (!isJsonObject(values)) return refuseType({ node, op: "$interpolate", field: "values", needs: "an object", got: values, written: valuesNode });
|
|
541
|
-
return template.replace(/\{\{(\
|
|
542
|
-
const value = values
|
|
569
|
+
return template.replace(/\{\{([\p{L}\p{M}\p{N}_]+)\}\}/gu, (_match, key) => {
|
|
570
|
+
const value = readOwnKey(values, key) ?? "";
|
|
543
571
|
if (!isTextLike(value))
|
|
544
572
|
return failAt(
|
|
545
573
|
`$interpolate.values needs ${TEXT_LIKE} for {{${key}}}, but "${key}" is ${describeValue(value)}.`,
|
|
@@ -550,18 +578,20 @@ var opInterpolate = (node, context, evaluate2) => {
|
|
|
550
578
|
return String(value);
|
|
551
579
|
});
|
|
552
580
|
};
|
|
553
|
-
var fillOne = (value) => {
|
|
581
|
+
var fillOne = (value, budget, node) => {
|
|
554
582
|
if (value === null || typeof value !== "object" || Array.isArray(value)) return value;
|
|
555
583
|
const { phrase, slots } = value;
|
|
556
584
|
if (typeof phrase !== "string" || !isJsonObject(slots)) return value;
|
|
557
|
-
|
|
558
|
-
const slot = slots
|
|
585
|
+
const filled = phrase.replace(/\{([A-Za-z_][A-Za-z0-9_]*)\}/g, (hole, name) => {
|
|
586
|
+
const slot = readOwnKey(slots, name);
|
|
559
587
|
if (slot === void 0 || slot === null) return hole;
|
|
560
|
-
const
|
|
561
|
-
return typeof
|
|
588
|
+
const inner = fillOne(slot, budget, node);
|
|
589
|
+
return typeof inner === "object" ? hole : String(inner);
|
|
562
590
|
});
|
|
591
|
+
if (budget !== void 0) measureString(filled, budget, node);
|
|
592
|
+
return filled;
|
|
563
593
|
};
|
|
564
|
-
var opFill = (node, context, evaluate2) => fillOne(evaluate2(node.$fill, context));
|
|
594
|
+
var opFill = (node, context, evaluate2) => fillOne(evaluate2(node.$fill, context), context.budget, node);
|
|
565
595
|
var opTrim = (node, context, evaluate2) => {
|
|
566
596
|
const value = evaluate2(node.$trim, context);
|
|
567
597
|
if (typeof value !== "string") return refuseType({ node, op: "$trim", needs: "text", got: value, written: node.$trim });
|
|
@@ -580,13 +610,14 @@ var opUpper = (node, context, evaluate2) => {
|
|
|
580
610
|
var opSplit = (node, context, evaluate2) => {
|
|
581
611
|
const value = evaluate2(node.$split.value, context);
|
|
582
612
|
if (typeof value !== "string") return refuseType({ node, op: "$split", field: "value", needs: "text", got: value, written: node.$split.value });
|
|
583
|
-
return value.split(node.$split.sep);
|
|
613
|
+
return node.$split.sep === "" ? Array.from(value) : value.split(node.$split.sep);
|
|
584
614
|
};
|
|
585
615
|
var opReplace = (node, context, evaluate2) => {
|
|
586
616
|
const value = evaluate2(node.$replace.value, context);
|
|
587
617
|
if (typeof value !== "string") return refuseType({ node, op: "$replace", field: "value", needs: "text", got: value, written: node.$replace.value });
|
|
588
618
|
const { search, replacement, all } = node.$replace;
|
|
589
|
-
|
|
619
|
+
const written = () => replacement;
|
|
620
|
+
return all === true ? value.replaceAll(search, written) : value.replace(search, written);
|
|
590
621
|
};
|
|
591
622
|
|
|
592
623
|
// src/ops/predicate.ops.ts
|
|
@@ -675,7 +706,8 @@ var opMerge = (node, context, evaluate2) => {
|
|
|
675
706
|
const part = node.$merge[index];
|
|
676
707
|
const evaluated = evaluate2(part, context);
|
|
677
708
|
if (!isJsonObject(evaluated)) return refuseType({ node, op: "$merge", field: String(index), needs: "an object", got: evaluated, written: part, contextOp: "$merge" });
|
|
678
|
-
Object.
|
|
709
|
+
if (Object.hasOwn(evaluated, "__proto__")) for (const [key, value] of Object.entries(evaluated)) writeOwnKey(result, key, value);
|
|
710
|
+
else Object.assign(result, evaluated);
|
|
679
711
|
}
|
|
680
712
|
return result;
|
|
681
713
|
};
|
|
@@ -706,10 +738,10 @@ var opKeyBy = (node, context, evaluate2) => {
|
|
|
706
738
|
const result = {};
|
|
707
739
|
const { scope, vars } = loopScope(context);
|
|
708
740
|
for (const item of input) {
|
|
709
|
-
vars
|
|
741
|
+
writeOwnKey(vars, as, item);
|
|
710
742
|
const key = evaluate2(keyExpr, scope);
|
|
711
743
|
if (!isTextLike(key)) return refuseType({ node, op: "$keyBy", field: "key", needs: TEXT_LIKE, got: key, written: keyExpr, contextOp: "$keyBy" });
|
|
712
|
-
result
|
|
744
|
+
writeOwnKey(result, String(key), item);
|
|
713
745
|
}
|
|
714
746
|
return result;
|
|
715
747
|
};
|
|
@@ -720,12 +752,13 @@ var opGroupBy = (node, context, evaluate2) => {
|
|
|
720
752
|
const result = {};
|
|
721
753
|
const { scope, vars } = loopScope(context);
|
|
722
754
|
for (const item of input) {
|
|
723
|
-
vars
|
|
755
|
+
writeOwnKey(vars, as, item);
|
|
724
756
|
const value = evaluate2(keyExpr, scope);
|
|
725
757
|
if (!isTextLike(value)) return refuseType({ node, op: "$groupBy", field: "key", needs: TEXT_LIKE, got: value, written: keyExpr, contextOp: "$groupBy" });
|
|
726
758
|
const key = String(value);
|
|
727
|
-
|
|
728
|
-
result[
|
|
759
|
+
const group = readOwnKey(result, key);
|
|
760
|
+
if (group === void 0) writeOwnKey(result, key, [item]);
|
|
761
|
+
else group.push(item);
|
|
729
762
|
}
|
|
730
763
|
return result;
|
|
731
764
|
};
|
|
@@ -749,7 +782,7 @@ var opFromEntries = (node, context, evaluate2) => {
|
|
|
749
782
|
const entry = value[index];
|
|
750
783
|
if (!isJsonArray(entry) || entry.length < 2)
|
|
751
784
|
return failAt(`$fromEntries needs a list of [key, value] pairs, but item ${index} is ${describeValue(entry)}.`, ErrorCode.TYPE, { op: "$fromEntries" }, { node, op: "$fromEntries", form: true });
|
|
752
|
-
result
|
|
785
|
+
writeOwnKey(result, String(entry[0]), entry[1] ?? null);
|
|
753
786
|
}
|
|
754
787
|
return result;
|
|
755
788
|
};
|
|
@@ -758,7 +791,8 @@ var opPick = (node, context, evaluate2) => {
|
|
|
758
791
|
if (!isJsonObject(value)) return refuseType({ node, op: "$pick", field: "from", needs: "an object", got: value, written: node.$pick.from, contextOp: "$pick" });
|
|
759
792
|
const result = {};
|
|
760
793
|
for (const key of node.$pick.keys) {
|
|
761
|
-
|
|
794
|
+
const picked = readOwnKey(value, key);
|
|
795
|
+
if (picked !== void 0) writeOwnKey(result, key, picked);
|
|
762
796
|
}
|
|
763
797
|
return result;
|
|
764
798
|
};
|
|
@@ -768,7 +802,7 @@ var opOmit = (node, context, evaluate2) => {
|
|
|
768
802
|
const keysToOmit = new Set(node.$omit.keys);
|
|
769
803
|
const result = {};
|
|
770
804
|
for (const [k, v] of Object.entries(value)) {
|
|
771
|
-
if (!keysToOmit.has(k)) result
|
|
805
|
+
if (!keysToOmit.has(k)) writeOwnKey(result, k, v);
|
|
772
806
|
}
|
|
773
807
|
return result;
|
|
774
808
|
};
|
|
@@ -786,9 +820,11 @@ var opLength = (node, context, evaluate2) => {
|
|
|
786
820
|
};
|
|
787
821
|
|
|
788
822
|
// src/ops/transform.ops.ts
|
|
823
|
+
var asIndex = (at, segment) => typeof segment === "string" && isJsonArray(at) && INDEX_TEXT.test(segment) ? Number(segment) : segment;
|
|
789
824
|
var has = (value, path) => {
|
|
790
825
|
let at = value;
|
|
791
|
-
for (const
|
|
826
|
+
for (const written of path) {
|
|
827
|
+
const segment = asIndex(at, written);
|
|
792
828
|
if (typeof segment === "number") {
|
|
793
829
|
if (!isJsonArray(at) || segment >= at.length) return false;
|
|
794
830
|
at = at[segment] ?? null;
|
|
@@ -807,31 +843,40 @@ var opRenameKeys = (node, context, evaluate2) => {
|
|
|
807
843
|
const renamedTo = new Set(Object.entries(map).filter(([from]) => Object.prototype.hasOwnProperty.call(value, from)).map(([, to]) => to));
|
|
808
844
|
const result = {};
|
|
809
845
|
for (const [key, v] of Object.entries(value)) {
|
|
810
|
-
const target = map
|
|
811
|
-
if (target !== void 0) result
|
|
812
|
-
else if (!renamedTo.has(key)) result
|
|
846
|
+
const target = readOwnKey(map, key);
|
|
847
|
+
if (target !== void 0) writeOwnKey(result, target, v);
|
|
848
|
+
else if (!renamedTo.has(key)) writeOwnKey(result, key, v);
|
|
813
849
|
}
|
|
814
850
|
return result;
|
|
815
851
|
};
|
|
816
852
|
var current = (value, path) => {
|
|
817
853
|
let at = value;
|
|
818
|
-
for (const
|
|
854
|
+
for (const written of path) {
|
|
855
|
+
const segment = asIndex(at, written);
|
|
819
856
|
if (typeof segment === "number") at = isJsonArray(at) ? at[segment] ?? null : null;
|
|
820
|
-
else at = isJsonObject(at) ? at
|
|
857
|
+
else at = isJsonObject(at) ? readOwnKey(at, segment) ?? null : null;
|
|
821
858
|
}
|
|
822
859
|
return at;
|
|
823
860
|
};
|
|
824
861
|
var setIn = (value, path, next, full, node) => {
|
|
825
|
-
const [
|
|
826
|
-
if (
|
|
862
|
+
const [written, ...rest] = path;
|
|
863
|
+
if (written === void 0) return next;
|
|
864
|
+
const head = asIndex(value, written);
|
|
827
865
|
if (typeof head === "number") {
|
|
828
866
|
if (!isJsonArray(value) || head >= value.length) {
|
|
829
867
|
return failAt(`No array item at ${full.join(".")}: the value there is ${describeValue(value)}.`, ErrorCode.MISSING_PATH, { op: "$update", path: full.join(".") }, { node, op: "$update", field: "path", form: false });
|
|
830
868
|
}
|
|
831
869
|
return value.map((item, i) => i === head ? setIn(item, rest, next, full, node) : item);
|
|
832
870
|
}
|
|
871
|
+
if (value !== null && !isJsonObject(value))
|
|
872
|
+
return failAt(
|
|
873
|
+
`No key ${JSON.stringify(head)} at ${full.join(".")}: the value there is ${describeValue(value)}, and $update does not replace it.`,
|
|
874
|
+
ErrorCode.TYPE,
|
|
875
|
+
{ op: "$update", path: full.join(".") },
|
|
876
|
+
{ node, op: "$update", field: "path", form: false }
|
|
877
|
+
);
|
|
833
878
|
const base = isJsonObject(value) ? value : {};
|
|
834
|
-
return { ...base, [head]: setIn(base
|
|
879
|
+
return { ...base, [head]: setIn(readOwnKey(base, head) ?? null, rest, next, full, node) };
|
|
835
880
|
};
|
|
836
881
|
var opUpdate = (node, context, evaluate2) => {
|
|
837
882
|
const { from, path, value, as } = node.$update;
|
|
@@ -864,6 +909,9 @@ var opWalk = (node, context, evaluate2) => {
|
|
|
864
909
|
dayjs.extend(utc);
|
|
865
910
|
var DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/;
|
|
866
911
|
var isDateOnly = (value) => typeof value === "string" && DATE_ONLY.test(value);
|
|
912
|
+
var notAheadOfTime = (context) => {
|
|
913
|
+
if (context.folding === true) throw new Error("answered where it runs");
|
|
914
|
+
};
|
|
867
915
|
var parseDate = (value, node, op, field, written, contextOp) => {
|
|
868
916
|
if (typeof value !== "string" && typeof value !== "number")
|
|
869
917
|
return refuseType({ node, op, field, needs: "a date, as text or a number", got: value, written, contextOp });
|
|
@@ -873,6 +921,7 @@ var parseDate = (value, node, op, field, written, contextOp) => {
|
|
|
873
921
|
return d;
|
|
874
922
|
};
|
|
875
923
|
var opDate = (node, context, evaluate2) => {
|
|
924
|
+
notAheadOfTime(context);
|
|
876
925
|
const value = evaluate2(node.$date.value, context);
|
|
877
926
|
const d = parseDate(value, node, "$date", "value", node.$date.value, "$date");
|
|
878
927
|
const instance = node.$date.utc ? d.utc() : d;
|
|
@@ -880,12 +929,14 @@ var opDate = (node, context, evaluate2) => {
|
|
|
880
929
|
return instance.toISOString();
|
|
881
930
|
};
|
|
882
931
|
var opDateAdd = (node, context, evaluate2) => {
|
|
932
|
+
notAheadOfTime(context);
|
|
883
933
|
const value = evaluate2(node.$dateAdd.date, context);
|
|
884
934
|
const d = parseDate(value, node, "$dateAdd", "date", node.$dateAdd.date, "$dateAdd");
|
|
885
935
|
const moved = d.add(node.$dateAdd.amount, node.$dateAdd.unit);
|
|
886
936
|
return isDateOnly(value) ? moved.format("YYYY-MM-DD") : moved.toISOString();
|
|
887
937
|
};
|
|
888
938
|
var opDateDiff = (node, context, evaluate2) => {
|
|
939
|
+
notAheadOfTime(context);
|
|
889
940
|
const from = evaluate2(node.$dateDiff.from, context);
|
|
890
941
|
const to = evaluate2(node.$dateDiff.to, context);
|
|
891
942
|
const fromDate = parseDate(from, node, "$dateDiff", "from", node.$dateDiff.from, "$dateDiff.from");
|
|
@@ -896,12 +947,55 @@ var opDateDiff = (node, context, evaluate2) => {
|
|
|
896
947
|
// src/ops/intl.ops.ts
|
|
897
948
|
var DATE_FORMATTERS = /* @__PURE__ */ new Map();
|
|
898
949
|
var NUMBER_FORMATTERS = /* @__PURE__ */ new Map();
|
|
950
|
+
var MINOR_DIGITS = /* @__PURE__ */ new Map();
|
|
951
|
+
var MAX_CACHED_FORMATTERS = 1e3;
|
|
952
|
+
var cached = (cache, key, make) => {
|
|
953
|
+
const known = cache.get(key);
|
|
954
|
+
if (known !== void 0) return known;
|
|
955
|
+
const made = make();
|
|
956
|
+
if (cache.size >= MAX_CACHED_FORMATTERS) cache.clear();
|
|
957
|
+
cache.set(key, made);
|
|
958
|
+
return made;
|
|
959
|
+
};
|
|
960
|
+
var built = (make, node, op) => {
|
|
961
|
+
try {
|
|
962
|
+
return make();
|
|
963
|
+
} catch (error) {
|
|
964
|
+
if (!(error instanceof RangeError || error instanceof TypeError)) throw error;
|
|
965
|
+
return failAt(`${op}: Intl refused its options: ${error.message}`, ErrorCode.SCHEMA, { op }, { node, op, form: true });
|
|
966
|
+
}
|
|
967
|
+
};
|
|
899
968
|
var DATE_ONLY2 = /^\d{4}-\d{2}-\d{2}$/;
|
|
900
969
|
var localeOf = (value, node, op, written) => {
|
|
901
970
|
if (typeof value !== "string" || value.trim() === "")
|
|
902
971
|
return refuseType({ node, op, field: "locale", needs: 'a locale tag such as "de-AT"', got: value, written, contextOp: op });
|
|
903
972
|
return value;
|
|
904
973
|
};
|
|
974
|
+
var KNOWN_ZONES = /* @__PURE__ */ new Set();
|
|
975
|
+
var isKnownZone = (zone) => {
|
|
976
|
+
if (KNOWN_ZONES.has(zone)) return true;
|
|
977
|
+
try {
|
|
978
|
+
new Intl.DateTimeFormat("en-US", { timeZone: zone });
|
|
979
|
+
} catch {
|
|
980
|
+
return false;
|
|
981
|
+
}
|
|
982
|
+
KNOWN_ZONES.add(zone);
|
|
983
|
+
return true;
|
|
984
|
+
};
|
|
985
|
+
var zoneOf = (written, node, context, evaluate2) => {
|
|
986
|
+
if (written === void 0) return void 0;
|
|
987
|
+
const zone = typeof written === "string" ? written : evaluate2(written, context);
|
|
988
|
+
if (typeof zone === "string" && zone !== "" && isKnownZone(zone)) return zone;
|
|
989
|
+
return refuseType({ node, op: "$localeDate", field: "options.timeZone", needs: 'an IANA time zone that Intl knows, such as "Europe/Vienna"', got: zone, written, contextOp: "$localeDate" });
|
|
990
|
+
};
|
|
991
|
+
var readableLocale = (locale, node, op, written) => {
|
|
992
|
+
try {
|
|
993
|
+
Intl.getCanonicalLocales(locale);
|
|
994
|
+
} catch {
|
|
995
|
+
return refuseType({ node, op, field: "locale", needs: 'a locale tag Intl can read, such as "de-AT"', got: locale, written, contextOp: op });
|
|
996
|
+
}
|
|
997
|
+
return locale;
|
|
998
|
+
};
|
|
905
999
|
var isBlank = (value) => value === null || value === void 0 || value === "";
|
|
906
1000
|
var fallbackOf = (raw, context, evaluate2) => {
|
|
907
1001
|
if (raw === void 0) return "";
|
|
@@ -909,46 +1003,44 @@ var fallbackOf = (raw, context, evaluate2) => {
|
|
|
909
1003
|
return resolved === null || resolved === void 0 ? "" : resolved;
|
|
910
1004
|
};
|
|
911
1005
|
var numberOf = (value, node, op, written) => {
|
|
912
|
-
const
|
|
1006
|
+
const text = typeof value === "string" ? value.trim() : void 0;
|
|
1007
|
+
const parsed = typeof value === "number" ? value : text !== void 0 && NUMERIC_TEXT.test(text) ? Number(text) : Number.NaN;
|
|
913
1008
|
if (!Number.isFinite(parsed)) return refuseType({ node, op, field: "value", needs: "a number or numeric text", got: value, written, contextOp: op });
|
|
914
1009
|
return parsed;
|
|
915
1010
|
};
|
|
916
1011
|
var opLocaleDate = (node, context, evaluate2) => {
|
|
1012
|
+
notAheadOfTime(context);
|
|
917
1013
|
const spec = node.$localeDate;
|
|
918
1014
|
const value = evaluate2(spec.value, context);
|
|
919
1015
|
if (isBlank(value)) return fallbackOf(spec.fallback, context, evaluate2);
|
|
920
1016
|
const locale = localeOf(evaluate2(spec.locale, context), node, "$localeDate", spec.locale);
|
|
921
|
-
const authored = spec.options ?? { dateStyle: "medium" };
|
|
1017
|
+
const { timeZone: writtenZone, ...authored } = spec.options ?? { dateStyle: "medium" };
|
|
922
1018
|
const dateOnly = typeof value === "string" && DATE_ONLY2.test(value);
|
|
923
|
-
const options = {
|
|
924
|
-
...authored,
|
|
925
|
-
...authored.timeZone === void 0 && dateOnly ? { timeZone: "UTC" } : {}
|
|
926
|
-
};
|
|
927
1019
|
const parsed = typeof value === "number" ? new Date(value) : new Date(String(value));
|
|
928
1020
|
if (Number.isNaN(parsed.getTime()))
|
|
929
1021
|
return failAt(`Invalid date for $localeDate: ${clip(String(value), 60)}`, ErrorCode.DATE_INVALID, { op: "$localeDate" }, { node, op: "$localeDate", field: "value", form: false });
|
|
930
|
-
const
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
}
|
|
1022
|
+
const zone = zoneOf(writtenZone, node, context, evaluate2);
|
|
1023
|
+
const timeZone = zone ?? (dateOnly ? "UTC" : void 0);
|
|
1024
|
+
const options = {
|
|
1025
|
+
...authored,
|
|
1026
|
+
...timeZone === void 0 ? {} : { timeZone }
|
|
1027
|
+
};
|
|
1028
|
+
const formatter = cached(
|
|
1029
|
+
DATE_FORMATTERS,
|
|
1030
|
+
`${locale}\0${JSON.stringify(options)}`,
|
|
1031
|
+
() => built(() => new Intl.DateTimeFormat(readableLocale(locale, node, "$localeDate", spec.locale), options), node, "$localeDate")
|
|
1032
|
+
);
|
|
936
1033
|
return formatter.format(parsed);
|
|
937
1034
|
};
|
|
938
|
-
var
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
formatter = new Intl.NumberFormat(locale, { style: "currency", currency });
|
|
944
|
-
} catch {
|
|
945
|
-
return 100;
|
|
946
|
-
}
|
|
947
|
-
NUMBER_FORMATTERS.set(key, formatter);
|
|
1035
|
+
var minorDigits = (currency) => cached(MINOR_DIGITS, currency, () => {
|
|
1036
|
+
try {
|
|
1037
|
+
return new Intl.NumberFormat("en", { style: "currency", currency }).resolvedOptions().maximumFractionDigits ?? 2;
|
|
1038
|
+
} catch {
|
|
1039
|
+
return 2;
|
|
948
1040
|
}
|
|
949
|
-
|
|
950
|
-
};
|
|
1041
|
+
});
|
|
951
1042
|
var opLocaleMoney = (node, context, evaluate2) => {
|
|
1043
|
+
notAheadOfTime(context);
|
|
952
1044
|
const spec = node.$localeMoney;
|
|
953
1045
|
const value = evaluate2(spec.value, context);
|
|
954
1046
|
if (isBlank(value)) return fallbackOf(spec.fallback, context, evaluate2);
|
|
@@ -958,25 +1050,23 @@ var opLocaleMoney = (node, context, evaluate2) => {
|
|
|
958
1050
|
return refuseType({ node, op: "$localeMoney", field: "currency", needs: 'an ISO-4217 currency code such as "EUR"', got: rawCurrency, written: spec.currency, contextOp: "$localeMoney" });
|
|
959
1051
|
const currency = rawCurrency.toUpperCase();
|
|
960
1052
|
const amount = numberOf(value, node, "$localeMoney", spec.value);
|
|
961
|
-
const
|
|
1053
|
+
const digits = minorDigits(currency);
|
|
1054
|
+
const major = spec.minorUnits === false ? amount : amount / 10 ** digits;
|
|
962
1055
|
const options = {
|
|
963
1056
|
style: "currency",
|
|
964
1057
|
currency,
|
|
965
1058
|
...spec.digits === void 0 ? {} : { minimumFractionDigits: spec.digits, maximumFractionDigits: spec.digits }
|
|
966
1059
|
};
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
} catch {
|
|
973
|
-
return `${currency} ${major.toFixed(spec.digits ?? 2)}`;
|
|
974
|
-
}
|
|
975
|
-
NUMBER_FORMATTERS.set(key, formatter);
|
|
1060
|
+
let formatter;
|
|
1061
|
+
try {
|
|
1062
|
+
formatter = cached(NUMBER_FORMATTERS, `${locale}\0${JSON.stringify(options)}`, () => new Intl.NumberFormat(locale, options));
|
|
1063
|
+
} catch {
|
|
1064
|
+
return `${currency} ${major.toFixed(spec.digits ?? digits)}`;
|
|
976
1065
|
}
|
|
977
1066
|
return formatter.format(major);
|
|
978
1067
|
};
|
|
979
1068
|
var opLocaleNumber = (node, context, evaluate2) => {
|
|
1069
|
+
notAheadOfTime(context);
|
|
980
1070
|
const spec = node.$localeNumber;
|
|
981
1071
|
const value = evaluate2(spec.value, context);
|
|
982
1072
|
if (isBlank(value)) return fallbackOf(spec.fallback, context, evaluate2);
|
|
@@ -987,18 +1077,16 @@ var opLocaleNumber = (node, context, evaluate2) => {
|
|
|
987
1077
|
...spec.minDigits === void 0 ? {} : { minimumFractionDigits: spec.minDigits },
|
|
988
1078
|
...spec.compact === true ? { notation: "compact" } : {}
|
|
989
1079
|
};
|
|
990
|
-
const
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
}
|
|
1080
|
+
const formatter = cached(
|
|
1081
|
+
NUMBER_FORMATTERS,
|
|
1082
|
+
`${locale}\0${JSON.stringify(options)}`,
|
|
1083
|
+
() => built(() => new Intl.NumberFormat(readableLocale(locale, node, "$localeNumber", spec.locale), options), node, "$localeNumber")
|
|
1084
|
+
);
|
|
996
1085
|
return formatter.format(numberOf(value, node, "$localeNumber", spec.value));
|
|
997
1086
|
};
|
|
998
1087
|
|
|
999
1088
|
// src/engine/optimize.ts
|
|
1000
1089
|
var HANDLER_KEY = "__op";
|
|
1001
|
-
var SEGMENTS_KEY2 = "__segments";
|
|
1002
1090
|
var eraseOp = (op) => op;
|
|
1003
1091
|
var attachNonEnumerable = (target, key, value) => {
|
|
1004
1092
|
Object.defineProperty(target, key, {
|
|
@@ -1091,12 +1179,12 @@ var isFoldableLiteral = (node) => {
|
|
|
1091
1179
|
if (Object.keys(node).some((k) => k.startsWith("$"))) return false;
|
|
1092
1180
|
return Object.values(node).every(isFoldableLiteral);
|
|
1093
1181
|
};
|
|
1094
|
-
var optimizeNode = (node, stats, evaluate2) => {
|
|
1182
|
+
var optimizeNode = (node, stats, evaluate2, options) => {
|
|
1095
1183
|
if (node === null || node === void 0) return node;
|
|
1096
1184
|
if (typeof node !== "object") return node;
|
|
1097
1185
|
if (Array.isArray(node)) {
|
|
1098
1186
|
const out = [];
|
|
1099
|
-
for (const child of node) out.push(optimizeNode(child, stats, evaluate2));
|
|
1187
|
+
for (const child of node) out.push(optimizeNode(child, stats, evaluate2, options));
|
|
1100
1188
|
return out;
|
|
1101
1189
|
}
|
|
1102
1190
|
if (!isJsonObject(node)) return node;
|
|
@@ -1109,21 +1197,21 @@ var optimizeNode = (node, stats, evaluate2) => {
|
|
|
1109
1197
|
}
|
|
1110
1198
|
const optimizeValues = (record) => {
|
|
1111
1199
|
const out = {};
|
|
1112
|
-
for (const key of Object.keys(record)) out[key] = optimizeNode(record[key], stats, evaluate2);
|
|
1200
|
+
for (const key of Object.keys(record)) out[key] = optimizeNode(record[key], stats, evaluate2, options);
|
|
1113
1201
|
return out;
|
|
1114
1202
|
};
|
|
1115
1203
|
let optimized;
|
|
1116
1204
|
if (isWithNode(node)) {
|
|
1117
|
-
optimized = { $with: { let: optimizeValues(node.$with.let), value: optimizeNode(node.$with.value, stats, evaluate2) } };
|
|
1205
|
+
optimized = { $with: { let: optimizeValues(node.$with.let), value: optimizeNode(node.$with.value, stats, evaluate2, options) } };
|
|
1118
1206
|
} else if (isRenameKeysNode(node)) {
|
|
1119
|
-
optimized = { $renameKeys: { ...node.$renameKeys, from: optimizeNode(node.$renameKeys.from, stats, evaluate2) } };
|
|
1207
|
+
optimized = { $renameKeys: { ...node.$renameKeys, from: optimizeNode(node.$renameKeys.from, stats, evaluate2, options) } };
|
|
1120
1208
|
} else {
|
|
1121
1209
|
optimized = optimizeValues(node);
|
|
1122
1210
|
}
|
|
1123
1211
|
if (isRefNode(optimized)) {
|
|
1124
1212
|
const segments = parseJsonPath(optimized.$ref);
|
|
1125
1213
|
if (segments.length > 0) {
|
|
1126
|
-
attachNonEnumerable(optimized,
|
|
1214
|
+
attachNonEnumerable(optimized, SEGMENTS_KEY, segments);
|
|
1127
1215
|
stats.refsInlined += 1;
|
|
1128
1216
|
}
|
|
1129
1217
|
}
|
|
@@ -1134,16 +1222,17 @@ var optimizeNode = (node, stats, evaluate2) => {
|
|
|
1134
1222
|
}
|
|
1135
1223
|
if (handler !== void 0 && !isConstNode(optimized) && !isRefNode(optimized) && !isVarNode(optimized)) {
|
|
1136
1224
|
const allFoldable = Object.entries(optimized).every(([key, value]) => {
|
|
1137
|
-
if (key === HANDLER_KEY || key ===
|
|
1225
|
+
if (key === HANDLER_KEY || key === SEGMENTS_KEY) return true;
|
|
1138
1226
|
return isFoldableLiteral(value);
|
|
1139
1227
|
});
|
|
1140
1228
|
if (allFoldable) {
|
|
1141
|
-
const emptyCtx = { source: {}, vars: {}, budget: createBudget() };
|
|
1229
|
+
const emptyCtx = { source: {}, vars: {}, budget: createBudget(), folding: true };
|
|
1142
1230
|
try {
|
|
1143
1231
|
const folded = handler(optimized, emptyCtx, evaluate2);
|
|
1144
1232
|
const replacement = { $const: folded };
|
|
1145
1233
|
freezeConstant(folded);
|
|
1146
1234
|
attachNonEnumerable(replacement, HANDLER_KEY, eraseOp(opConst));
|
|
1235
|
+
if (options.recordFoldCost === true) attachNonEnumerable(replacement, COST_KEY, emptyCtx.budget?.steps ?? 0);
|
|
1147
1236
|
stats.constantsFolded += 1;
|
|
1148
1237
|
return replacement;
|
|
1149
1238
|
} catch {
|
|
@@ -1152,9 +1241,9 @@ var optimizeNode = (node, stats, evaluate2) => {
|
|
|
1152
1241
|
}
|
|
1153
1242
|
return optimized;
|
|
1154
1243
|
};
|
|
1155
|
-
var optimize = (node, evaluate2) => {
|
|
1244
|
+
var optimize = (node, evaluate2, options = {}) => {
|
|
1156
1245
|
const stats = { refsInlined: 0, handlersAttached: 0, constantsFolded: 0 };
|
|
1157
|
-
const result = optimizeNode(node, stats, evaluate2);
|
|
1246
|
+
const result = optimizeNode(node, stats, evaluate2, options);
|
|
1158
1247
|
return { node: result, stats };
|
|
1159
1248
|
};
|
|
1160
1249
|
var annotateNode = (node) => {
|
|
@@ -1175,7 +1264,7 @@ var annotateNode = (node) => {
|
|
|
1175
1264
|
}
|
|
1176
1265
|
if (isRefNode(node)) {
|
|
1177
1266
|
const segments = parseJsonPath(node.$ref);
|
|
1178
|
-
if (segments.length > 0) attachNonEnumerable(node,
|
|
1267
|
+
if (segments.length > 0) attachNonEnumerable(node, SEGMENTS_KEY, segments);
|
|
1179
1268
|
}
|
|
1180
1269
|
const handler = resolveHandler(node);
|
|
1181
1270
|
if (handler !== void 0) attachNonEnumerable(node, HANDLER_KEY, handler);
|
|
@@ -1256,12 +1345,11 @@ var placeError = (error, tree, authored) => {
|
|
|
1256
1345
|
};
|
|
1257
1346
|
|
|
1258
1347
|
// src/engine/evaluate.ts
|
|
1259
|
-
var HANDLER_KEY2 = "__op";
|
|
1260
1348
|
var isAttachedFn = (value) => typeof value === "function";
|
|
1261
1349
|
var evaluateNode = (node, context) => {
|
|
1262
1350
|
const { budget } = context;
|
|
1263
1351
|
if (budget === void 0) return evaluateUnbudgeted(node, context);
|
|
1264
|
-
|
|
1352
|
+
spendSteps(budget, node);
|
|
1265
1353
|
const out = evaluateUnbudgeted(node, context);
|
|
1266
1354
|
measureString(out, budget, node);
|
|
1267
1355
|
return out;
|
|
@@ -1272,12 +1360,12 @@ var evaluateUnbudgeted = (node, context) => {
|
|
|
1272
1360
|
if (Array.isArray(node)) return node.map((n) => evaluateNode(n, context));
|
|
1273
1361
|
if (typeof node !== "object") return null;
|
|
1274
1362
|
const obj = node;
|
|
1275
|
-
const attached = obj[
|
|
1363
|
+
const attached = obj[HANDLER_KEY];
|
|
1276
1364
|
if (isAttachedFn(attached)) return attached(obj, context, evaluateNode);
|
|
1277
1365
|
if (isPlainObject(obj)) {
|
|
1278
1366
|
const result = {};
|
|
1279
1367
|
if (!(OPTIONAL_FIELDS_KEY in obj)) {
|
|
1280
|
-
for (const key of Object.keys(obj)) result
|
|
1368
|
+
for (const key of Object.keys(obj)) writeOwnKey(result, key, evaluateNode(obj[key], context));
|
|
1281
1369
|
return result;
|
|
1282
1370
|
}
|
|
1283
1371
|
const optionalFields = new Set(
|
|
@@ -1289,79 +1377,16 @@ var evaluateUnbudgeted = (node, context) => {
|
|
|
1289
1377
|
try {
|
|
1290
1378
|
const evaluated = evaluateNode(value, context);
|
|
1291
1379
|
if (isOptional && (evaluated === null || evaluated === void 0)) continue;
|
|
1292
|
-
result
|
|
1380
|
+
writeOwnKey(result, key, evaluated);
|
|
1293
1381
|
} catch (error) {
|
|
1294
|
-
if (isOptional && error instanceof PrismError && error.code === ErrorCode.MISSING_PATH) continue;
|
|
1382
|
+
if (isOptional && error instanceof PrismError && (error.code === ErrorCode.MISSING_PATH || error.context?.details?.["absent"] === true)) continue;
|
|
1295
1383
|
throw error;
|
|
1296
1384
|
}
|
|
1297
1385
|
}
|
|
1298
1386
|
return result;
|
|
1299
1387
|
}
|
|
1300
|
-
|
|
1301
|
-
if (
|
|
1302
|
-
if (isVarNode(obj)) return opVar(obj, context);
|
|
1303
|
-
if (isGetNode(obj)) return opGet(obj, context, evaluateNode);
|
|
1304
|
-
if (isWithNode(obj)) return opWith(obj, context, evaluateNode);
|
|
1305
|
-
if (isMapNode(obj)) return opMap(obj, context, evaluateNode);
|
|
1306
|
-
if (isFilterNode(obj)) return opFilter(obj, context, evaluateNode);
|
|
1307
|
-
if (isReduceNode(obj)) return opReduce(obj, context, evaluateNode);
|
|
1308
|
-
if (isSliceNode(obj)) return opSlice(obj, context, evaluateNode);
|
|
1309
|
-
if (isFlattenNode(obj)) return opFlatten(obj, context, evaluateNode);
|
|
1310
|
-
if (isUniqueNode(obj)) return opUnique(obj, context, evaluateNode);
|
|
1311
|
-
if (isSortByNode(obj)) return opSortBy(obj, context, evaluateNode);
|
|
1312
|
-
if (isAddNode(obj)) return opAdd(obj, context, evaluateNode);
|
|
1313
|
-
if (isSubNode(obj)) return opSub(obj, context, evaluateNode);
|
|
1314
|
-
if (isMulNode(obj)) return opMul(obj, context, evaluateNode);
|
|
1315
|
-
if (isDivNode(obj)) return opDiv(obj, context, evaluateNode);
|
|
1316
|
-
if (isRoundNode(obj)) return opRound(obj, context, evaluateNode);
|
|
1317
|
-
if (isModNode(obj)) return opMod(obj, context, evaluateNode);
|
|
1318
|
-
if (isToNumberNode(obj)) return opToNumber(obj, context, evaluateNode);
|
|
1319
|
-
if (isJoinNode(obj)) return opJoin(obj, context, evaluateNode);
|
|
1320
|
-
if (isToStringNode(obj)) return opToString(obj, context, evaluateNode);
|
|
1321
|
-
if (isInterpolateNode(obj)) return opInterpolate(obj, context, evaluateNode);
|
|
1322
|
-
if (isFillNode(obj)) return opFill(obj, context, evaluateNode);
|
|
1323
|
-
if (isTrimNode(obj)) return opTrim(obj, context, evaluateNode);
|
|
1324
|
-
if (isLowerNode(obj)) return opLower(obj, context, evaluateNode);
|
|
1325
|
-
if (isUpperNode(obj)) return opUpper(obj, context, evaluateNode);
|
|
1326
|
-
if (isSplitNode(obj)) return opSplit(obj, context, evaluateNode);
|
|
1327
|
-
if (isReplaceNode(obj)) return opReplace(obj, context, evaluateNode);
|
|
1328
|
-
if (isEqNode(obj)) return opEq(obj, context, evaluateNode);
|
|
1329
|
-
if (isNeqNode(obj)) return opNeq(obj, context, evaluateNode);
|
|
1330
|
-
if (isGtNode(obj)) return opGt(obj, context, evaluateNode);
|
|
1331
|
-
if (isGteNode(obj)) return opGte(obj, context, evaluateNode);
|
|
1332
|
-
if (isLtNode(obj)) return opLt(obj, context, evaluateNode);
|
|
1333
|
-
if (isLteNode(obj)) return opLte(obj, context, evaluateNode);
|
|
1334
|
-
if (isEmptyNode(obj)) return opEmpty(obj, context, evaluateNode);
|
|
1335
|
-
if (isStartsWithNode(obj)) return opStartsWith(obj, context, evaluateNode);
|
|
1336
|
-
if (isEndsWithNode(obj)) return opEndsWith(obj, context, evaluateNode);
|
|
1337
|
-
if (isContainsNode(obj)) return opContains(obj, context, evaluateNode);
|
|
1338
|
-
if (isNotNode(obj)) return opNot(obj, context, evaluateNode);
|
|
1339
|
-
if (isAndNode(obj)) return opAnd(obj, context, evaluateNode);
|
|
1340
|
-
if (isOrNode(obj)) return opOr(obj, context, evaluateNode);
|
|
1341
|
-
if (isMergeNode(obj)) return opMerge(obj, context, evaluateNode);
|
|
1342
|
-
if (isCoalesceNode(obj)) return opCoalesce(obj, context, evaluateNode);
|
|
1343
|
-
if (isCaseNode(obj)) return opCase(obj, context, evaluateNode);
|
|
1344
|
-
if (isEntriesOfNode(obj)) return opEntriesOf(obj, context, evaluateNode);
|
|
1345
|
-
if (isKeyByNode(obj)) return opKeyBy(obj, context, evaluateNode);
|
|
1346
|
-
if (isGroupByNode(obj)) return opGroupBy(obj, context, evaluateNode);
|
|
1347
|
-
if (isKeysNode(obj)) return opKeys(obj, context, evaluateNode);
|
|
1348
|
-
if (isValuesNode(obj)) return opValues(obj, context, evaluateNode);
|
|
1349
|
-
if (isFromEntriesNode(obj)) return opFromEntries(obj, context, evaluateNode);
|
|
1350
|
-
if (isPickNode(obj)) return opPick(obj, context, evaluateNode);
|
|
1351
|
-
if (isOmitNode(obj)) return opOmit(obj, context, evaluateNode);
|
|
1352
|
-
if (isTypeNode(obj)) return opType(obj, context, evaluateNode);
|
|
1353
|
-
if (isLengthNode(obj)) return opLength(obj, context, evaluateNode);
|
|
1354
|
-
if (isHasNode(obj)) return opHas(obj, context, evaluateNode);
|
|
1355
|
-
if (isRenameKeysNode(obj)) return opRenameKeys(obj, context, evaluateNode);
|
|
1356
|
-
if (isUpdateNode(obj)) return opUpdate(obj, context, evaluateNode);
|
|
1357
|
-
if (isAssertNode(obj)) return opAssert(obj, context, evaluateNode);
|
|
1358
|
-
if (isWalkNode(obj)) return opWalk(obj, context, evaluateNode);
|
|
1359
|
-
if (isDateNode(obj)) return opDate(obj, context, evaluateNode);
|
|
1360
|
-
if (isDateAddNode(obj)) return opDateAdd(obj, context, evaluateNode);
|
|
1361
|
-
if (isDateDiffNode(obj)) return opDateDiff(obj, context, evaluateNode);
|
|
1362
|
-
if (isLocaleDateNode(obj)) return opLocaleDate(obj, context, evaluateNode);
|
|
1363
|
-
if (isLocaleMoneyNode(obj)) return opLocaleMoney(obj, context, evaluateNode);
|
|
1364
|
-
if (isLocaleNumberNode(obj)) return opLocaleNumber(obj, context, evaluateNode);
|
|
1388
|
+
const handler = resolveHandler(obj);
|
|
1389
|
+
if (handler !== void 0) return handler(obj, context, evaluateNode);
|
|
1365
1390
|
const unknownKeys = Object.keys(obj).filter((k) => k.startsWith("$"));
|
|
1366
1391
|
const preview = JSON.stringify(obj).slice(0, 100);
|
|
1367
1392
|
return failAt(
|
|
@@ -1371,24 +1396,32 @@ var evaluateUnbudgeted = (node, context) => {
|
|
|
1371
1396
|
{ node: obj, form: false }
|
|
1372
1397
|
);
|
|
1373
1398
|
};
|
|
1374
|
-
var notJson = (value, at) => {
|
|
1399
|
+
var notJson = (value, at, ancestors = /* @__PURE__ */ new Map()) => {
|
|
1375
1400
|
if (value === null || typeof value === "string" || typeof value === "boolean") return void 0;
|
|
1376
1401
|
if (typeof value === "number") return Number.isFinite(value) ? void 0 : `${at} is ${value}`;
|
|
1402
|
+
if (typeof value !== "object") return `${at} is ${value === void 0 ? "undefined" : `a ${typeof value}`}`;
|
|
1403
|
+
const loop = ancestors.get(value);
|
|
1404
|
+
if (loop !== void 0) return `${at} refers back to ${loop}`;
|
|
1405
|
+
ancestors.set(value, at);
|
|
1406
|
+
const found = notJsonInside(value, at, ancestors);
|
|
1407
|
+
ancestors.delete(value);
|
|
1408
|
+
return found;
|
|
1409
|
+
};
|
|
1410
|
+
var notJsonInside = (value, at, ancestors) => {
|
|
1377
1411
|
if (Array.isArray(value)) {
|
|
1378
1412
|
for (const index of Object.keys(value)) {
|
|
1379
|
-
const found = notJson(value[Number(index)], `${at}[${index}]
|
|
1413
|
+
const found = notJson(value[Number(index)], `${at}[${index}]`, ancestors);
|
|
1380
1414
|
if (found !== void 0) return found;
|
|
1381
1415
|
}
|
|
1382
1416
|
return void 0;
|
|
1383
1417
|
}
|
|
1384
|
-
if (typeof value !== "object") return `${at} is ${value === void 0 ? "undefined" : `a ${typeof value}`}`;
|
|
1385
1418
|
const proto = Object.getPrototypeOf(value);
|
|
1386
1419
|
if (proto !== Object.prototype && proto !== null) {
|
|
1387
1420
|
const tag = Object.prototype.toString.call(value).slice(8, -1);
|
|
1388
1421
|
return `${at} is ${tag === "Object" ? "an instance of a class" : `a ${tag}`}`;
|
|
1389
1422
|
}
|
|
1390
1423
|
for (const [key, inner] of Object.entries(value)) {
|
|
1391
|
-
const found = notJson(inner, `${at}.${key}
|
|
1424
|
+
const found = notJson(inner, `${at}.${key}`, ancestors);
|
|
1392
1425
|
if (found !== void 0) return found;
|
|
1393
1426
|
}
|
|
1394
1427
|
return void 0;
|
|
@@ -1415,7 +1448,7 @@ var run = (tree, source, limits, kept, authored) => {
|
|
|
1415
1448
|
}
|
|
1416
1449
|
};
|
|
1417
1450
|
var keptTrees = /* @__PURE__ */ new WeakMap();
|
|
1418
|
-
var
|
|
1451
|
+
var evaluateOnce = (config, source, options) => {
|
|
1419
1452
|
if (options?.check === "always") {
|
|
1420
1453
|
const wrong = notJson(source, "source");
|
|
1421
1454
|
if (wrong !== void 0) throw new PrismError(`The source must be plain JSON: ${wrong}.`, ErrorCode.TYPE);
|
|
@@ -1428,11 +1461,18 @@ var evaluate = (config, source, options) => {
|
|
|
1428
1461
|
kept = { tree: checked(config), optimized: false };
|
|
1429
1462
|
keptTrees.set(config, kept);
|
|
1430
1463
|
} else if (!kept.optimized) {
|
|
1431
|
-
kept.tree = optimize(kept.tree, evaluateNode).node;
|
|
1464
|
+
kept.tree = optimize(kept.tree, evaluateNode, { recordFoldCost: true }).node;
|
|
1432
1465
|
kept.optimized = true;
|
|
1433
1466
|
}
|
|
1434
1467
|
return run(kept.tree, input, options?.limits, true, config);
|
|
1435
1468
|
};
|
|
1469
|
+
var evaluate = (config, source, options) => {
|
|
1470
|
+
try {
|
|
1471
|
+
return evaluateOnce(config, source, options);
|
|
1472
|
+
} catch (error) {
|
|
1473
|
+
throw stackRefusal(error);
|
|
1474
|
+
}
|
|
1475
|
+
};
|
|
1436
1476
|
var evaluateSafe = (config, source, options) => {
|
|
1437
1477
|
try {
|
|
1438
1478
|
return { ok: true, data: evaluate(config, source, options) };
|
|
@@ -1529,7 +1569,7 @@ var execute = (ir, source, limits) => {
|
|
|
1529
1569
|
return result;
|
|
1530
1570
|
} catch (error) {
|
|
1531
1571
|
placeError(error, core);
|
|
1532
|
-
throw error;
|
|
1572
|
+
throw stackRefusal(error);
|
|
1533
1573
|
}
|
|
1534
1574
|
};
|
|
1535
1575
|
var validate = (config) => {
|