@niscorp/prism 0.3.4 → 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 +99 -0
- package/DESIGN.md +24 -13
- package/DOCS.md +71 -18
- package/dist/agent.cjs +4 -4
- package/dist/agent.js +2 -2
- package/dist/{chunk-C4IHL4MY.js → chunk-E44DNCG3.js} +36 -22
- package/dist/chunk-E44DNCG3.js.map +1 -0
- package/dist/{chunk-DOCK3CEK.cjs → chunk-NBU7EBGN.cjs} +5 -5
- package/dist/{chunk-DOCK3CEK.cjs.map → chunk-NBU7EBGN.cjs.map} +1 -1
- package/dist/{chunk-L23ZLSDQ.cjs → chunk-VXU22DMV.cjs} +37 -21
- package/dist/chunk-VXU22DMV.cjs.map +1 -0
- package/dist/{chunk-BT3DLVGE.js → chunk-ZMMUIMAK.js} +3 -3
- package/dist/{chunk-BT3DLVGE.js.map → chunk-ZMMUIMAK.js.map} +1 -1
- package/dist/examples/index.d.cts +1 -1
- package/dist/examples/index.d.ts +1 -1
- package/dist/index.cjs +386 -373
- 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 +193 -180
- package/dist/index.js.map +1 -1
- package/dist/migrations/index.cjs +2 -2
- package/dist/migrations/index.js +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-C4IHL4MY.js.map +0 -1
- package/dist/chunk-L23ZLSDQ.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,6 +947,24 @@ 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() === "")
|
|
@@ -934,11 +1003,13 @@ var fallbackOf = (raw, context, evaluate2) => {
|
|
|
934
1003
|
return resolved === null || resolved === void 0 ? "" : resolved;
|
|
935
1004
|
};
|
|
936
1005
|
var numberOf = (value, node, op, written) => {
|
|
937
|
-
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;
|
|
938
1008
|
if (!Number.isFinite(parsed)) return refuseType({ node, op, field: "value", needs: "a number or numeric text", got: value, written, contextOp: op });
|
|
939
1009
|
return parsed;
|
|
940
1010
|
};
|
|
941
1011
|
var opLocaleDate = (node, context, evaluate2) => {
|
|
1012
|
+
notAheadOfTime(context);
|
|
942
1013
|
const spec = node.$localeDate;
|
|
943
1014
|
const value = evaluate2(spec.value, context);
|
|
944
1015
|
if (isBlank(value)) return fallbackOf(spec.fallback, context, evaluate2);
|
|
@@ -954,28 +1025,22 @@ var opLocaleDate = (node, context, evaluate2) => {
|
|
|
954
1025
|
...authored,
|
|
955
1026
|
...timeZone === void 0 ? {} : { timeZone }
|
|
956
1027
|
};
|
|
957
|
-
const
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
}
|
|
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
|
+
);
|
|
963
1033
|
return formatter.format(parsed);
|
|
964
1034
|
};
|
|
965
|
-
var
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
formatter = new Intl.NumberFormat(locale, { style: "currency", currency });
|
|
971
|
-
} catch {
|
|
972
|
-
return 100;
|
|
973
|
-
}
|
|
974
|
-
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;
|
|
975
1040
|
}
|
|
976
|
-
|
|
977
|
-
};
|
|
1041
|
+
});
|
|
978
1042
|
var opLocaleMoney = (node, context, evaluate2) => {
|
|
1043
|
+
notAheadOfTime(context);
|
|
979
1044
|
const spec = node.$localeMoney;
|
|
980
1045
|
const value = evaluate2(spec.value, context);
|
|
981
1046
|
if (isBlank(value)) return fallbackOf(spec.fallback, context, evaluate2);
|
|
@@ -985,25 +1050,23 @@ var opLocaleMoney = (node, context, evaluate2) => {
|
|
|
985
1050
|
return refuseType({ node, op: "$localeMoney", field: "currency", needs: 'an ISO-4217 currency code such as "EUR"', got: rawCurrency, written: spec.currency, contextOp: "$localeMoney" });
|
|
986
1051
|
const currency = rawCurrency.toUpperCase();
|
|
987
1052
|
const amount = numberOf(value, node, "$localeMoney", spec.value);
|
|
988
|
-
const
|
|
1053
|
+
const digits = minorDigits(currency);
|
|
1054
|
+
const major = spec.minorUnits === false ? amount : amount / 10 ** digits;
|
|
989
1055
|
const options = {
|
|
990
1056
|
style: "currency",
|
|
991
1057
|
currency,
|
|
992
1058
|
...spec.digits === void 0 ? {} : { minimumFractionDigits: spec.digits, maximumFractionDigits: spec.digits }
|
|
993
1059
|
};
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
} catch {
|
|
1000
|
-
return `${currency} ${major.toFixed(spec.digits ?? 2)}`;
|
|
1001
|
-
}
|
|
1002
|
-
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)}`;
|
|
1003
1065
|
}
|
|
1004
1066
|
return formatter.format(major);
|
|
1005
1067
|
};
|
|
1006
1068
|
var opLocaleNumber = (node, context, evaluate2) => {
|
|
1069
|
+
notAheadOfTime(context);
|
|
1007
1070
|
const spec = node.$localeNumber;
|
|
1008
1071
|
const value = evaluate2(spec.value, context);
|
|
1009
1072
|
if (isBlank(value)) return fallbackOf(spec.fallback, context, evaluate2);
|
|
@@ -1014,18 +1077,16 @@ var opLocaleNumber = (node, context, evaluate2) => {
|
|
|
1014
1077
|
...spec.minDigits === void 0 ? {} : { minimumFractionDigits: spec.minDigits },
|
|
1015
1078
|
...spec.compact === true ? { notation: "compact" } : {}
|
|
1016
1079
|
};
|
|
1017
|
-
const
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
}
|
|
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
|
+
);
|
|
1023
1085
|
return formatter.format(numberOf(value, node, "$localeNumber", spec.value));
|
|
1024
1086
|
};
|
|
1025
1087
|
|
|
1026
1088
|
// src/engine/optimize.ts
|
|
1027
1089
|
var HANDLER_KEY = "__op";
|
|
1028
|
-
var SEGMENTS_KEY2 = "__segments";
|
|
1029
1090
|
var eraseOp = (op) => op;
|
|
1030
1091
|
var attachNonEnumerable = (target, key, value) => {
|
|
1031
1092
|
Object.defineProperty(target, key, {
|
|
@@ -1118,12 +1179,12 @@ var isFoldableLiteral = (node) => {
|
|
|
1118
1179
|
if (Object.keys(node).some((k) => k.startsWith("$"))) return false;
|
|
1119
1180
|
return Object.values(node).every(isFoldableLiteral);
|
|
1120
1181
|
};
|
|
1121
|
-
var optimizeNode = (node, stats, evaluate2) => {
|
|
1182
|
+
var optimizeNode = (node, stats, evaluate2, options) => {
|
|
1122
1183
|
if (node === null || node === void 0) return node;
|
|
1123
1184
|
if (typeof node !== "object") return node;
|
|
1124
1185
|
if (Array.isArray(node)) {
|
|
1125
1186
|
const out = [];
|
|
1126
|
-
for (const child of node) out.push(optimizeNode(child, stats, evaluate2));
|
|
1187
|
+
for (const child of node) out.push(optimizeNode(child, stats, evaluate2, options));
|
|
1127
1188
|
return out;
|
|
1128
1189
|
}
|
|
1129
1190
|
if (!isJsonObject(node)) return node;
|
|
@@ -1136,21 +1197,21 @@ var optimizeNode = (node, stats, evaluate2) => {
|
|
|
1136
1197
|
}
|
|
1137
1198
|
const optimizeValues = (record) => {
|
|
1138
1199
|
const out = {};
|
|
1139
|
-
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);
|
|
1140
1201
|
return out;
|
|
1141
1202
|
};
|
|
1142
1203
|
let optimized;
|
|
1143
1204
|
if (isWithNode(node)) {
|
|
1144
|
-
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) } };
|
|
1145
1206
|
} else if (isRenameKeysNode(node)) {
|
|
1146
|
-
optimized = { $renameKeys: { ...node.$renameKeys, from: optimizeNode(node.$renameKeys.from, stats, evaluate2) } };
|
|
1207
|
+
optimized = { $renameKeys: { ...node.$renameKeys, from: optimizeNode(node.$renameKeys.from, stats, evaluate2, options) } };
|
|
1147
1208
|
} else {
|
|
1148
1209
|
optimized = optimizeValues(node);
|
|
1149
1210
|
}
|
|
1150
1211
|
if (isRefNode(optimized)) {
|
|
1151
1212
|
const segments = parseJsonPath(optimized.$ref);
|
|
1152
1213
|
if (segments.length > 0) {
|
|
1153
|
-
attachNonEnumerable(optimized,
|
|
1214
|
+
attachNonEnumerable(optimized, SEGMENTS_KEY, segments);
|
|
1154
1215
|
stats.refsInlined += 1;
|
|
1155
1216
|
}
|
|
1156
1217
|
}
|
|
@@ -1161,16 +1222,17 @@ var optimizeNode = (node, stats, evaluate2) => {
|
|
|
1161
1222
|
}
|
|
1162
1223
|
if (handler !== void 0 && !isConstNode(optimized) && !isRefNode(optimized) && !isVarNode(optimized)) {
|
|
1163
1224
|
const allFoldable = Object.entries(optimized).every(([key, value]) => {
|
|
1164
|
-
if (key === HANDLER_KEY || key ===
|
|
1225
|
+
if (key === HANDLER_KEY || key === SEGMENTS_KEY) return true;
|
|
1165
1226
|
return isFoldableLiteral(value);
|
|
1166
1227
|
});
|
|
1167
1228
|
if (allFoldable) {
|
|
1168
|
-
const emptyCtx = { source: {}, vars: {}, budget: createBudget() };
|
|
1229
|
+
const emptyCtx = { source: {}, vars: {}, budget: createBudget(), folding: true };
|
|
1169
1230
|
try {
|
|
1170
1231
|
const folded = handler(optimized, emptyCtx, evaluate2);
|
|
1171
1232
|
const replacement = { $const: folded };
|
|
1172
1233
|
freezeConstant(folded);
|
|
1173
1234
|
attachNonEnumerable(replacement, HANDLER_KEY, eraseOp(opConst));
|
|
1235
|
+
if (options.recordFoldCost === true) attachNonEnumerable(replacement, COST_KEY, emptyCtx.budget?.steps ?? 0);
|
|
1174
1236
|
stats.constantsFolded += 1;
|
|
1175
1237
|
return replacement;
|
|
1176
1238
|
} catch {
|
|
@@ -1179,9 +1241,9 @@ var optimizeNode = (node, stats, evaluate2) => {
|
|
|
1179
1241
|
}
|
|
1180
1242
|
return optimized;
|
|
1181
1243
|
};
|
|
1182
|
-
var optimize = (node, evaluate2) => {
|
|
1244
|
+
var optimize = (node, evaluate2, options = {}) => {
|
|
1183
1245
|
const stats = { refsInlined: 0, handlersAttached: 0, constantsFolded: 0 };
|
|
1184
|
-
const result = optimizeNode(node, stats, evaluate2);
|
|
1246
|
+
const result = optimizeNode(node, stats, evaluate2, options);
|
|
1185
1247
|
return { node: result, stats };
|
|
1186
1248
|
};
|
|
1187
1249
|
var annotateNode = (node) => {
|
|
@@ -1202,7 +1264,7 @@ var annotateNode = (node) => {
|
|
|
1202
1264
|
}
|
|
1203
1265
|
if (isRefNode(node)) {
|
|
1204
1266
|
const segments = parseJsonPath(node.$ref);
|
|
1205
|
-
if (segments.length > 0) attachNonEnumerable(node,
|
|
1267
|
+
if (segments.length > 0) attachNonEnumerable(node, SEGMENTS_KEY, segments);
|
|
1206
1268
|
}
|
|
1207
1269
|
const handler = resolveHandler(node);
|
|
1208
1270
|
if (handler !== void 0) attachNonEnumerable(node, HANDLER_KEY, handler);
|
|
@@ -1283,12 +1345,11 @@ var placeError = (error, tree, authored) => {
|
|
|
1283
1345
|
};
|
|
1284
1346
|
|
|
1285
1347
|
// src/engine/evaluate.ts
|
|
1286
|
-
var HANDLER_KEY2 = "__op";
|
|
1287
1348
|
var isAttachedFn = (value) => typeof value === "function";
|
|
1288
1349
|
var evaluateNode = (node, context) => {
|
|
1289
1350
|
const { budget } = context;
|
|
1290
1351
|
if (budget === void 0) return evaluateUnbudgeted(node, context);
|
|
1291
|
-
|
|
1352
|
+
spendSteps(budget, node);
|
|
1292
1353
|
const out = evaluateUnbudgeted(node, context);
|
|
1293
1354
|
measureString(out, budget, node);
|
|
1294
1355
|
return out;
|
|
@@ -1299,12 +1360,12 @@ var evaluateUnbudgeted = (node, context) => {
|
|
|
1299
1360
|
if (Array.isArray(node)) return node.map((n) => evaluateNode(n, context));
|
|
1300
1361
|
if (typeof node !== "object") return null;
|
|
1301
1362
|
const obj = node;
|
|
1302
|
-
const attached = obj[
|
|
1363
|
+
const attached = obj[HANDLER_KEY];
|
|
1303
1364
|
if (isAttachedFn(attached)) return attached(obj, context, evaluateNode);
|
|
1304
1365
|
if (isPlainObject(obj)) {
|
|
1305
1366
|
const result = {};
|
|
1306
1367
|
if (!(OPTIONAL_FIELDS_KEY in obj)) {
|
|
1307
|
-
for (const key of Object.keys(obj)) result
|
|
1368
|
+
for (const key of Object.keys(obj)) writeOwnKey(result, key, evaluateNode(obj[key], context));
|
|
1308
1369
|
return result;
|
|
1309
1370
|
}
|
|
1310
1371
|
const optionalFields = new Set(
|
|
@@ -1316,79 +1377,16 @@ var evaluateUnbudgeted = (node, context) => {
|
|
|
1316
1377
|
try {
|
|
1317
1378
|
const evaluated = evaluateNode(value, context);
|
|
1318
1379
|
if (isOptional && (evaluated === null || evaluated === void 0)) continue;
|
|
1319
|
-
result
|
|
1380
|
+
writeOwnKey(result, key, evaluated);
|
|
1320
1381
|
} catch (error) {
|
|
1321
|
-
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;
|
|
1322
1383
|
throw error;
|
|
1323
1384
|
}
|
|
1324
1385
|
}
|
|
1325
1386
|
return result;
|
|
1326
1387
|
}
|
|
1327
|
-
|
|
1328
|
-
if (
|
|
1329
|
-
if (isVarNode(obj)) return opVar(obj, context);
|
|
1330
|
-
if (isGetNode(obj)) return opGet(obj, context, evaluateNode);
|
|
1331
|
-
if (isWithNode(obj)) return opWith(obj, context, evaluateNode);
|
|
1332
|
-
if (isMapNode(obj)) return opMap(obj, context, evaluateNode);
|
|
1333
|
-
if (isFilterNode(obj)) return opFilter(obj, context, evaluateNode);
|
|
1334
|
-
if (isReduceNode(obj)) return opReduce(obj, context, evaluateNode);
|
|
1335
|
-
if (isSliceNode(obj)) return opSlice(obj, context, evaluateNode);
|
|
1336
|
-
if (isFlattenNode(obj)) return opFlatten(obj, context, evaluateNode);
|
|
1337
|
-
if (isUniqueNode(obj)) return opUnique(obj, context, evaluateNode);
|
|
1338
|
-
if (isSortByNode(obj)) return opSortBy(obj, context, evaluateNode);
|
|
1339
|
-
if (isAddNode(obj)) return opAdd(obj, context, evaluateNode);
|
|
1340
|
-
if (isSubNode(obj)) return opSub(obj, context, evaluateNode);
|
|
1341
|
-
if (isMulNode(obj)) return opMul(obj, context, evaluateNode);
|
|
1342
|
-
if (isDivNode(obj)) return opDiv(obj, context, evaluateNode);
|
|
1343
|
-
if (isRoundNode(obj)) return opRound(obj, context, evaluateNode);
|
|
1344
|
-
if (isModNode(obj)) return opMod(obj, context, evaluateNode);
|
|
1345
|
-
if (isToNumberNode(obj)) return opToNumber(obj, context, evaluateNode);
|
|
1346
|
-
if (isJoinNode(obj)) return opJoin(obj, context, evaluateNode);
|
|
1347
|
-
if (isToStringNode(obj)) return opToString(obj, context, evaluateNode);
|
|
1348
|
-
if (isInterpolateNode(obj)) return opInterpolate(obj, context, evaluateNode);
|
|
1349
|
-
if (isFillNode(obj)) return opFill(obj, context, evaluateNode);
|
|
1350
|
-
if (isTrimNode(obj)) return opTrim(obj, context, evaluateNode);
|
|
1351
|
-
if (isLowerNode(obj)) return opLower(obj, context, evaluateNode);
|
|
1352
|
-
if (isUpperNode(obj)) return opUpper(obj, context, evaluateNode);
|
|
1353
|
-
if (isSplitNode(obj)) return opSplit(obj, context, evaluateNode);
|
|
1354
|
-
if (isReplaceNode(obj)) return opReplace(obj, context, evaluateNode);
|
|
1355
|
-
if (isEqNode(obj)) return opEq(obj, context, evaluateNode);
|
|
1356
|
-
if (isNeqNode(obj)) return opNeq(obj, context, evaluateNode);
|
|
1357
|
-
if (isGtNode(obj)) return opGt(obj, context, evaluateNode);
|
|
1358
|
-
if (isGteNode(obj)) return opGte(obj, context, evaluateNode);
|
|
1359
|
-
if (isLtNode(obj)) return opLt(obj, context, evaluateNode);
|
|
1360
|
-
if (isLteNode(obj)) return opLte(obj, context, evaluateNode);
|
|
1361
|
-
if (isEmptyNode(obj)) return opEmpty(obj, context, evaluateNode);
|
|
1362
|
-
if (isStartsWithNode(obj)) return opStartsWith(obj, context, evaluateNode);
|
|
1363
|
-
if (isEndsWithNode(obj)) return opEndsWith(obj, context, evaluateNode);
|
|
1364
|
-
if (isContainsNode(obj)) return opContains(obj, context, evaluateNode);
|
|
1365
|
-
if (isNotNode(obj)) return opNot(obj, context, evaluateNode);
|
|
1366
|
-
if (isAndNode(obj)) return opAnd(obj, context, evaluateNode);
|
|
1367
|
-
if (isOrNode(obj)) return opOr(obj, context, evaluateNode);
|
|
1368
|
-
if (isMergeNode(obj)) return opMerge(obj, context, evaluateNode);
|
|
1369
|
-
if (isCoalesceNode(obj)) return opCoalesce(obj, context, evaluateNode);
|
|
1370
|
-
if (isCaseNode(obj)) return opCase(obj, context, evaluateNode);
|
|
1371
|
-
if (isEntriesOfNode(obj)) return opEntriesOf(obj, context, evaluateNode);
|
|
1372
|
-
if (isKeyByNode(obj)) return opKeyBy(obj, context, evaluateNode);
|
|
1373
|
-
if (isGroupByNode(obj)) return opGroupBy(obj, context, evaluateNode);
|
|
1374
|
-
if (isKeysNode(obj)) return opKeys(obj, context, evaluateNode);
|
|
1375
|
-
if (isValuesNode(obj)) return opValues(obj, context, evaluateNode);
|
|
1376
|
-
if (isFromEntriesNode(obj)) return opFromEntries(obj, context, evaluateNode);
|
|
1377
|
-
if (isPickNode(obj)) return opPick(obj, context, evaluateNode);
|
|
1378
|
-
if (isOmitNode(obj)) return opOmit(obj, context, evaluateNode);
|
|
1379
|
-
if (isTypeNode(obj)) return opType(obj, context, evaluateNode);
|
|
1380
|
-
if (isLengthNode(obj)) return opLength(obj, context, evaluateNode);
|
|
1381
|
-
if (isHasNode(obj)) return opHas(obj, context, evaluateNode);
|
|
1382
|
-
if (isRenameKeysNode(obj)) return opRenameKeys(obj, context, evaluateNode);
|
|
1383
|
-
if (isUpdateNode(obj)) return opUpdate(obj, context, evaluateNode);
|
|
1384
|
-
if (isAssertNode(obj)) return opAssert(obj, context, evaluateNode);
|
|
1385
|
-
if (isWalkNode(obj)) return opWalk(obj, context, evaluateNode);
|
|
1386
|
-
if (isDateNode(obj)) return opDate(obj, context, evaluateNode);
|
|
1387
|
-
if (isDateAddNode(obj)) return opDateAdd(obj, context, evaluateNode);
|
|
1388
|
-
if (isDateDiffNode(obj)) return opDateDiff(obj, context, evaluateNode);
|
|
1389
|
-
if (isLocaleDateNode(obj)) return opLocaleDate(obj, context, evaluateNode);
|
|
1390
|
-
if (isLocaleMoneyNode(obj)) return opLocaleMoney(obj, context, evaluateNode);
|
|
1391
|
-
if (isLocaleNumberNode(obj)) return opLocaleNumber(obj, context, evaluateNode);
|
|
1388
|
+
const handler = resolveHandler(obj);
|
|
1389
|
+
if (handler !== void 0) return handler(obj, context, evaluateNode);
|
|
1392
1390
|
const unknownKeys = Object.keys(obj).filter((k) => k.startsWith("$"));
|
|
1393
1391
|
const preview = JSON.stringify(obj).slice(0, 100);
|
|
1394
1392
|
return failAt(
|
|
@@ -1398,24 +1396,32 @@ var evaluateUnbudgeted = (node, context) => {
|
|
|
1398
1396
|
{ node: obj, form: false }
|
|
1399
1397
|
);
|
|
1400
1398
|
};
|
|
1401
|
-
var notJson = (value, at) => {
|
|
1399
|
+
var notJson = (value, at, ancestors = /* @__PURE__ */ new Map()) => {
|
|
1402
1400
|
if (value === null || typeof value === "string" || typeof value === "boolean") return void 0;
|
|
1403
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) => {
|
|
1404
1411
|
if (Array.isArray(value)) {
|
|
1405
1412
|
for (const index of Object.keys(value)) {
|
|
1406
|
-
const found = notJson(value[Number(index)], `${at}[${index}]
|
|
1413
|
+
const found = notJson(value[Number(index)], `${at}[${index}]`, ancestors);
|
|
1407
1414
|
if (found !== void 0) return found;
|
|
1408
1415
|
}
|
|
1409
1416
|
return void 0;
|
|
1410
1417
|
}
|
|
1411
|
-
if (typeof value !== "object") return `${at} is ${value === void 0 ? "undefined" : `a ${typeof value}`}`;
|
|
1412
1418
|
const proto = Object.getPrototypeOf(value);
|
|
1413
1419
|
if (proto !== Object.prototype && proto !== null) {
|
|
1414
1420
|
const tag = Object.prototype.toString.call(value).slice(8, -1);
|
|
1415
1421
|
return `${at} is ${tag === "Object" ? "an instance of a class" : `a ${tag}`}`;
|
|
1416
1422
|
}
|
|
1417
1423
|
for (const [key, inner] of Object.entries(value)) {
|
|
1418
|
-
const found = notJson(inner, `${at}.${key}
|
|
1424
|
+
const found = notJson(inner, `${at}.${key}`, ancestors);
|
|
1419
1425
|
if (found !== void 0) return found;
|
|
1420
1426
|
}
|
|
1421
1427
|
return void 0;
|
|
@@ -1442,7 +1448,7 @@ var run = (tree, source, limits, kept, authored) => {
|
|
|
1442
1448
|
}
|
|
1443
1449
|
};
|
|
1444
1450
|
var keptTrees = /* @__PURE__ */ new WeakMap();
|
|
1445
|
-
var
|
|
1451
|
+
var evaluateOnce = (config, source, options) => {
|
|
1446
1452
|
if (options?.check === "always") {
|
|
1447
1453
|
const wrong = notJson(source, "source");
|
|
1448
1454
|
if (wrong !== void 0) throw new PrismError(`The source must be plain JSON: ${wrong}.`, ErrorCode.TYPE);
|
|
@@ -1455,11 +1461,18 @@ var evaluate = (config, source, options) => {
|
|
|
1455
1461
|
kept = { tree: checked(config), optimized: false };
|
|
1456
1462
|
keptTrees.set(config, kept);
|
|
1457
1463
|
} else if (!kept.optimized) {
|
|
1458
|
-
kept.tree = optimize(kept.tree, evaluateNode).node;
|
|
1464
|
+
kept.tree = optimize(kept.tree, evaluateNode, { recordFoldCost: true }).node;
|
|
1459
1465
|
kept.optimized = true;
|
|
1460
1466
|
}
|
|
1461
1467
|
return run(kept.tree, input, options?.limits, true, config);
|
|
1462
1468
|
};
|
|
1469
|
+
var evaluate = (config, source, options) => {
|
|
1470
|
+
try {
|
|
1471
|
+
return evaluateOnce(config, source, options);
|
|
1472
|
+
} catch (error) {
|
|
1473
|
+
throw stackRefusal(error);
|
|
1474
|
+
}
|
|
1475
|
+
};
|
|
1463
1476
|
var evaluateSafe = (config, source, options) => {
|
|
1464
1477
|
try {
|
|
1465
1478
|
return { ok: true, data: evaluate(config, source, options) };
|
|
@@ -1556,7 +1569,7 @@ var execute = (ir, source, limits) => {
|
|
|
1556
1569
|
return result;
|
|
1557
1570
|
} catch (error) {
|
|
1558
1571
|
placeError(error, core);
|
|
1559
|
-
throw error;
|
|
1572
|
+
throw stackRefusal(error);
|
|
1560
1573
|
}
|
|
1561
1574
|
};
|
|
1562
1575
|
var validate = (config) => {
|