@promptctl/go-template-js 0.9.0 → 0.12.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/README.md +131 -10
- package/dist/index.d.ts +284 -44
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1306 -519
- package/dist/index.js.map +1 -1
- package/package.json +5 -4
package/dist/index.js
CHANGED
|
@@ -8,7 +8,7 @@ var TemplateError = class extends Error {
|
|
|
8
8
|
pos;
|
|
9
9
|
source;
|
|
10
10
|
constructor(message, pos, ctx = {}) {
|
|
11
|
-
super(message);
|
|
11
|
+
super(message, { cause: ctx.cause });
|
|
12
12
|
this.pos = pos;
|
|
13
13
|
this.source = ctx.source;
|
|
14
14
|
}
|
|
@@ -55,6 +55,32 @@ var FuncNotFoundError = class extends EvalError {
|
|
|
55
55
|
this.suggestions = suggestions;
|
|
56
56
|
}
|
|
57
57
|
};
|
|
58
|
+
/**
|
|
59
|
+
* Thrown when a func is called with a number of arguments its declared
|
|
60
|
+
* arity does not accept — `{{ upper "a" "b" }}`, `{{ min }}`.
|
|
61
|
+
*
|
|
62
|
+
* The message mirrors Go's `text/template`, which reports a fixed-arity
|
|
63
|
+
* signature as `want N` and a variadic one as `want at least N`. That
|
|
64
|
+
* distinction is read off `accepted`, not passed in, so the two phrasings
|
|
65
|
+
* cannot disagree about which one a given func gets.
|
|
66
|
+
*/
|
|
67
|
+
var ArgCountError = class extends EvalError {
|
|
68
|
+
name = "ArgCountError";
|
|
69
|
+
kind = "ArgCountError";
|
|
70
|
+
funcName;
|
|
71
|
+
accepted;
|
|
72
|
+
got;
|
|
73
|
+
constructor(funcName, accepted, got, pos, ctx = {}) {
|
|
74
|
+
const want = accepted.minimum === accepted.maximum ? `${accepted.minimum}` : `at least ${accepted.minimum}`;
|
|
75
|
+
super(`wrong number of args for ${funcName}: want ${want} got ${got}`, pos, ctx);
|
|
76
|
+
this.funcName = funcName;
|
|
77
|
+
this.accepted = accepted;
|
|
78
|
+
this.got = got;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
function isStackOverflow(e) {
|
|
82
|
+
return (e instanceof RangeError || e instanceof Error && e.name === "InternalError") && (e.message.includes("call stack") || e.message.includes("too much recursion"));
|
|
83
|
+
}
|
|
58
84
|
var TypeMismatchError = class extends EvalError {
|
|
59
85
|
name = "TypeMismatchError";
|
|
60
86
|
kind = "TypeMismatchError";
|
|
@@ -64,13 +90,28 @@ var TypeMismatchError = class extends EvalError {
|
|
|
64
90
|
expected;
|
|
65
91
|
receivedSummary;
|
|
66
92
|
constructor(funcName, argIndex, expected, receivedSummary, pos, ctx = {}) {
|
|
67
|
-
super(`function ${JSON.stringify(funcName)} arg ${argIndex}: expected ${expected}, but a ${receivedSummary} flowed in. Pass it through \`unstyled\` (or your engine's flatten function) to convert.`, pos, ctx);
|
|
93
|
+
super(`function ${JSON.stringify(funcName)} arg ${argIndex}: expected ${expected}, but a value of type ${receivedSummary} flowed in. Pass it through \`unstyled\` (or your engine's flatten function) to convert.`, pos, ctx);
|
|
68
94
|
this.funcName = funcName;
|
|
69
95
|
this.argIndex = argIndex;
|
|
70
96
|
this.expected = expected;
|
|
71
97
|
this.receivedSummary = receivedSummary;
|
|
72
98
|
}
|
|
73
99
|
};
|
|
100
|
+
/**
|
|
101
|
+
* An integer slot refused a number that is not a safe integer. Still a
|
|
102
|
+
* `TypeMismatchError` (the slot's type refused the value), but worded
|
|
103
|
+
* with the value and the reason: a fractional is Go's own refusal,
|
|
104
|
+
* `expected integer; found 2.7`, and an integer past 2^53 is
|
|
105
|
+
* `integer out of range: <n>`, since JS cannot carry it exactly. The
|
|
106
|
+
* generic wording would say a number was expected and a number arrived,
|
|
107
|
+
* and suggest flattening a styled value that was never there.
|
|
108
|
+
*/
|
|
109
|
+
var NotIntegerError = class extends TypeMismatchError {
|
|
110
|
+
constructor(funcName, argIndex, value, pos, ctx = {}) {
|
|
111
|
+
super(funcName, argIndex, "integer", String(value), pos, ctx);
|
|
112
|
+
this.message = Number.isInteger(Number(value)) ? `integer out of range: ${String(value)}` : `expected integer; found ${String(value)}`;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
74
115
|
var MissingFieldError = class extends EvalError {
|
|
75
116
|
name = "MissingFieldError";
|
|
76
117
|
kind = "MissingFieldError";
|
|
@@ -561,7 +602,7 @@ function isIdentPart(c) {
|
|
|
561
602
|
/**
|
|
562
603
|
* The named sub-templates a template can invoke: the ones its own source
|
|
563
604
|
* declared plus, transitively, the ones it inherited at parse. Opaque: only
|
|
564
|
-
* `has` and the `EMPTY` identity are public, so a consumer can neither build
|
|
605
|
+
* `has`, `names` and the `EMPTY` identity are public, so a consumer can neither build
|
|
565
606
|
* one by hand (the parser is the single enforcer of the redefinition invariant
|
|
566
607
|
* below) nor reach the AST inside — the entry lookup is `lookupDefine`, a
|
|
567
608
|
* module export index.ts never re-exports.
|
|
@@ -597,10 +638,26 @@ var Defines = class Defines {
|
|
|
597
638
|
has(name) {
|
|
598
639
|
return readDefine(this, name) !== void 0;
|
|
599
640
|
}
|
|
641
|
+
/**
|
|
642
|
+
* Every name this set can render, own and inherited, sorted — what a
|
|
643
|
+
* consumer reads after `evaluateDefine` reports a name as not defined.
|
|
644
|
+
* The parser forbids redeclaring an inherited name, so the chain repeats
|
|
645
|
+
* none; the Set keeps the listing duplicate-free without leaning on that.
|
|
646
|
+
*/
|
|
647
|
+
names() {
|
|
648
|
+
const names = /* @__PURE__ */ new Set();
|
|
649
|
+
for (let d = this; d; d = d.#parent) for (const name of d.#own.keys()) names.add(name);
|
|
650
|
+
return [...names].sort();
|
|
651
|
+
}
|
|
600
652
|
};
|
|
601
653
|
function parse(source, delims, inherit = Defines.EMPTY) {
|
|
602
654
|
return new Parser(source, delims, inherit).parseTemplate();
|
|
603
655
|
}
|
|
656
|
+
const PIPELINE_CONTEXT = {
|
|
657
|
+
If: "if",
|
|
658
|
+
Range: "range",
|
|
659
|
+
With: "with"
|
|
660
|
+
};
|
|
604
661
|
var Parser = class {
|
|
605
662
|
lex;
|
|
606
663
|
buf = [];
|
|
@@ -722,7 +779,7 @@ var Parser = class {
|
|
|
722
779
|
return null;
|
|
723
780
|
case "RightDelim": throw this.errAt(head, "empty action", { expected: "pipeline" });
|
|
724
781
|
default: {
|
|
725
|
-
const pipe = this.parsePipeline();
|
|
782
|
+
const pipe = this.parsePipeline("command");
|
|
726
783
|
const right = this.expect("RightDelim", "`}}`");
|
|
727
784
|
return {
|
|
728
785
|
type: "Action",
|
|
@@ -737,7 +794,7 @@ var Parser = class {
|
|
|
737
794
|
}
|
|
738
795
|
}
|
|
739
796
|
parseBranch(kind, startPos, trimLeft) {
|
|
740
|
-
const pipe = this.parsePipeline();
|
|
797
|
+
const pipe = this.parsePipeline(PIPELINE_CONTEXT[kind]);
|
|
741
798
|
this.expect("RightDelim", "`}}`");
|
|
742
799
|
if (kind === "Range") this.rangeDepth += 1;
|
|
743
800
|
const list = this.parseList();
|
|
@@ -835,7 +892,7 @@ var Parser = class {
|
|
|
835
892
|
}
|
|
836
893
|
parseTemplateInvocation(startPos, trimLeft) {
|
|
837
894
|
const name = this.parseQuotedName();
|
|
838
|
-
const pipe = this.peek().type === "RightDelim" ? void 0 : this.parsePipeline();
|
|
895
|
+
const pipe = this.peek().type === "RightDelim" ? void 0 : this.parsePipeline("template clause");
|
|
839
896
|
const trim = {
|
|
840
897
|
trimLeft,
|
|
841
898
|
trimRight: this.expect("RightDelim", "`}}`").trimRight === true
|
|
@@ -855,7 +912,7 @@ var Parser = class {
|
|
|
855
912
|
}
|
|
856
913
|
parseBlock(startPos, trimLeft) {
|
|
857
914
|
const name = this.parseQuotedName();
|
|
858
|
-
const pipe = this.
|
|
915
|
+
const pipe = this.parsePipeline("block clause");
|
|
859
916
|
this.expect("RightDelim", "`}}`");
|
|
860
917
|
const savedDepth = this.rangeDepth;
|
|
861
918
|
this.rangeDepth = 0;
|
|
@@ -869,19 +926,13 @@ var Parser = class {
|
|
|
869
926
|
list,
|
|
870
927
|
source: this.source
|
|
871
928
|
});
|
|
872
|
-
return
|
|
929
|
+
return {
|
|
873
930
|
type: "Block",
|
|
874
931
|
pos: startPos,
|
|
875
932
|
name,
|
|
876
933
|
pipe,
|
|
877
934
|
list,
|
|
878
935
|
trim
|
|
879
|
-
} : {
|
|
880
|
-
type: "Block",
|
|
881
|
-
pos: startPos,
|
|
882
|
-
name,
|
|
883
|
-
list,
|
|
884
|
-
trim
|
|
885
936
|
};
|
|
886
937
|
}
|
|
887
938
|
parseDefine(_startPos, _trimLeft) {
|
|
@@ -906,9 +957,11 @@ var Parser = class {
|
|
|
906
957
|
});
|
|
907
958
|
return decodeStringLiteral(t.value, t.pos, this.source);
|
|
908
959
|
}
|
|
909
|
-
parsePipeline() {
|
|
960
|
+
parsePipeline(context) {
|
|
910
961
|
const startPos = this.peek().pos;
|
|
911
|
-
const decls = this.tryParseDecls();
|
|
962
|
+
const decls = this.tryParseDecls(context);
|
|
963
|
+
const end = this.peek();
|
|
964
|
+
if (end.type === "RightDelim" || end.type === "RightParen") throw this.errAt(end, `missing value for ${context}`, { expected: "command" });
|
|
912
965
|
const cmds = [this.parseCommand()];
|
|
913
966
|
while (this.peek().type === "Pipe") {
|
|
914
967
|
this.next();
|
|
@@ -927,18 +980,19 @@ var Parser = class {
|
|
|
927
980
|
cmds
|
|
928
981
|
};
|
|
929
982
|
}
|
|
930
|
-
tryParseDecls() {
|
|
931
|
-
if (this.peek()
|
|
983
|
+
tryParseDecls(context) {
|
|
984
|
+
if (!isDeclTarget(this.peek())) return void 0;
|
|
932
985
|
let n = 1;
|
|
933
986
|
while (this.peek(n).type === "Comma") {
|
|
934
|
-
if (this.peek(n + 1)
|
|
987
|
+
if (!isDeclTarget(this.peek(n + 1))) return void 0;
|
|
935
988
|
n += 2;
|
|
936
989
|
}
|
|
937
990
|
const opType = this.peek(n).type;
|
|
938
991
|
if (opType !== "Declare" && opType !== "Assign") return void 0;
|
|
939
992
|
const vars = [varFromToken(this.next())];
|
|
940
993
|
while (this.peek().type === "Comma") {
|
|
941
|
-
this.next();
|
|
994
|
+
const comma = this.next();
|
|
995
|
+
if (vars.length === (context === "range" ? 2 : 1)) throw this.errAt(comma, `too many declarations in ${context}`);
|
|
942
996
|
vars.push(varFromToken(this.expect("Variable", "$variable")));
|
|
943
997
|
}
|
|
944
998
|
return {
|
|
@@ -999,7 +1053,7 @@ var Parser = class {
|
|
|
999
1053
|
}
|
|
1000
1054
|
}
|
|
1001
1055
|
parseChainOrPipe(startPos) {
|
|
1002
|
-
const inner = this.parsePipeline();
|
|
1056
|
+
const inner = this.parsePipeline("parenthesized pipeline");
|
|
1003
1057
|
const rparen = this.expect("RightParen", "`)`");
|
|
1004
1058
|
const fields = [];
|
|
1005
1059
|
const expectedNextOffset = rparen.pos.offset + 1;
|
|
@@ -1049,6 +1103,9 @@ function canStartPrimary(t) {
|
|
|
1049
1103
|
default: return false;
|
|
1050
1104
|
}
|
|
1051
1105
|
}
|
|
1106
|
+
function isDeclTarget(t) {
|
|
1107
|
+
return t.type === "Variable" && !t.value.includes(".");
|
|
1108
|
+
}
|
|
1052
1109
|
function varFromToken(t) {
|
|
1053
1110
|
return {
|
|
1054
1111
|
type: "Variable",
|
|
@@ -1265,7 +1322,7 @@ function children(node) {
|
|
|
1265
1322
|
node.elseList
|
|
1266
1323
|
] : [node.pipe, node.list];
|
|
1267
1324
|
case "Template": return node.pipe ? [node.pipe] : [];
|
|
1268
|
-
case "Block": return
|
|
1325
|
+
case "Block": return [node.pipe, node.list];
|
|
1269
1326
|
default: return assertNever(node);
|
|
1270
1327
|
}
|
|
1271
1328
|
}
|
|
@@ -1408,31 +1465,38 @@ function eagerBuiltins(toString, isT) {
|
|
|
1408
1465
|
return {
|
|
1409
1466
|
eq: {
|
|
1410
1467
|
fn: (a, ...rest) => rest.some((r) => goEqPair(a, r)),
|
|
1411
|
-
argTypes: ["comparable"]
|
|
1468
|
+
argTypes: ["comparable", "comparable"],
|
|
1469
|
+
arity: { kind: "variadic" }
|
|
1412
1470
|
},
|
|
1413
1471
|
ne: {
|
|
1414
1472
|
fn: (a, b) => !goEqPair(a, b),
|
|
1415
|
-
argTypes: ["comparable", "comparable"]
|
|
1473
|
+
argTypes: ["comparable", "comparable"],
|
|
1474
|
+
arity: { kind: "exact" }
|
|
1416
1475
|
},
|
|
1417
1476
|
lt: {
|
|
1418
1477
|
fn: (a, b) => compare(a, b) < 0,
|
|
1419
|
-
argTypes: ["ordered", "ordered"]
|
|
1478
|
+
argTypes: ["ordered", "ordered"],
|
|
1479
|
+
arity: { kind: "exact" }
|
|
1420
1480
|
},
|
|
1421
1481
|
le: {
|
|
1422
1482
|
fn: (a, b) => compare(a, b) <= 0,
|
|
1423
|
-
argTypes: ["ordered", "ordered"]
|
|
1483
|
+
argTypes: ["ordered", "ordered"],
|
|
1484
|
+
arity: { kind: "exact" }
|
|
1424
1485
|
},
|
|
1425
1486
|
gt: {
|
|
1426
1487
|
fn: (a, b) => compare(a, b) > 0,
|
|
1427
|
-
argTypes: ["ordered", "ordered"]
|
|
1488
|
+
argTypes: ["ordered", "ordered"],
|
|
1489
|
+
arity: { kind: "exact" }
|
|
1428
1490
|
},
|
|
1429
1491
|
ge: {
|
|
1430
1492
|
fn: (a, b) => compare(a, b) >= 0,
|
|
1431
|
-
argTypes: ["ordered", "ordered"]
|
|
1493
|
+
argTypes: ["ordered", "ordered"],
|
|
1494
|
+
arity: { kind: "exact" }
|
|
1432
1495
|
},
|
|
1433
1496
|
len: {
|
|
1434
1497
|
fn: (v) => goLen(v),
|
|
1435
|
-
argTypes: ["sized"]
|
|
1498
|
+
argTypes: ["sized"],
|
|
1499
|
+
arity: { kind: "exact" }
|
|
1436
1500
|
},
|
|
1437
1501
|
index: {
|
|
1438
1502
|
fn: (collection, ...keys) => {
|
|
@@ -1440,69 +1504,79 @@ function eagerBuiltins(toString, isT) {
|
|
|
1440
1504
|
for (const k of keys) cur = goIndex(cur, k);
|
|
1441
1505
|
return cur;
|
|
1442
1506
|
},
|
|
1443
|
-
argTypes: ["collection", "index-key"]
|
|
1507
|
+
argTypes: ["collection", "index-key"],
|
|
1508
|
+
arity: { kind: "variadic" }
|
|
1444
1509
|
},
|
|
1445
1510
|
slice: {
|
|
1446
|
-
fn: (collection, ...indices) =>
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
return typeof collection === "string" ? collection.slice(i, j) : collection.slice(i, j);
|
|
1450
|
-
},
|
|
1451
|
-
argTypes: ["sliceable", "int"]
|
|
1511
|
+
fn: (collection, ...indices) => goSlice(collection, indices),
|
|
1512
|
+
argTypes: ["sliceable", "int"],
|
|
1513
|
+
arity: { kind: "variadic" }
|
|
1452
1514
|
},
|
|
1453
1515
|
print: {
|
|
1454
|
-
fn: (...args) => goPrint(args, toString),
|
|
1516
|
+
fn: (...args) => goPrint(args, toString, isT),
|
|
1455
1517
|
argTypes: ["stringifiable"],
|
|
1518
|
+
arity: { kind: "variadic" },
|
|
1456
1519
|
returnType: "string"
|
|
1457
1520
|
},
|
|
1458
1521
|
println: {
|
|
1459
|
-
fn: (...args) => `${
|
|
1522
|
+
fn: (...args) => `${goPrintln(args, toString, isT)}\n`,
|
|
1460
1523
|
argTypes: ["stringifiable"],
|
|
1524
|
+
arity: { kind: "variadic" },
|
|
1461
1525
|
returnType: "string"
|
|
1462
1526
|
},
|
|
1463
1527
|
printf: {
|
|
1464
1528
|
fn: (format, ...args) => sprintf(format, args, toString, isT),
|
|
1465
1529
|
argTypes: ["string", "stringifiable"],
|
|
1530
|
+
arity: { kind: "variadic" },
|
|
1466
1531
|
returnType: "string"
|
|
1467
1532
|
},
|
|
1468
1533
|
toString: {
|
|
1469
1534
|
fn: (v) => formatV(v, toString, isT),
|
|
1470
1535
|
argTypes: ["value"],
|
|
1536
|
+
arity: { kind: "exact" },
|
|
1471
1537
|
returnType: "string"
|
|
1472
1538
|
},
|
|
1473
1539
|
toStrings: {
|
|
1474
1540
|
fn: (list) => list.map((v) => formatV(v, toString, isT)),
|
|
1475
|
-
argTypes: ["list"]
|
|
1541
|
+
argTypes: ["list"],
|
|
1542
|
+
arity: { kind: "exact" }
|
|
1476
1543
|
},
|
|
1477
1544
|
call: {
|
|
1478
1545
|
fn: (fn, ...args) => fn(...args),
|
|
1479
|
-
argTypes: ["callable", "value"]
|
|
1546
|
+
argTypes: ["callable", "value"],
|
|
1547
|
+
arity: { kind: "variadic" },
|
|
1548
|
+
writes: true
|
|
1480
1549
|
},
|
|
1481
1550
|
html: {
|
|
1482
|
-
fn: (...args) => htmlEscape(goPrint(args, toString)),
|
|
1551
|
+
fn: (...args) => htmlEscape(goPrint(args, toString, isT)),
|
|
1483
1552
|
argTypes: ["stringifiable"],
|
|
1553
|
+
arity: { kind: "variadic" },
|
|
1484
1554
|
returnType: "string"
|
|
1485
1555
|
},
|
|
1486
1556
|
js: {
|
|
1487
|
-
fn: (...args) => jsEscape(goPrint(args, toString)),
|
|
1557
|
+
fn: (...args) => jsEscape(goPrint(args, toString, isT)),
|
|
1488
1558
|
argTypes: ["stringifiable"],
|
|
1559
|
+
arity: { kind: "variadic" },
|
|
1489
1560
|
returnType: "string"
|
|
1490
1561
|
},
|
|
1491
1562
|
urlquery: {
|
|
1492
|
-
fn: (...args) => urlQueryEscape(goPrint(args, toString)),
|
|
1563
|
+
fn: (...args) => urlQueryEscape(goPrint(args, toString, isT)),
|
|
1493
1564
|
argTypes: ["stringifiable"],
|
|
1565
|
+
arity: { kind: "variadic" },
|
|
1494
1566
|
returnType: "string"
|
|
1495
1567
|
},
|
|
1496
1568
|
not: {
|
|
1497
1569
|
fn: (v) => !isTruthy(v),
|
|
1498
|
-
argTypes: ["truthy"]
|
|
1570
|
+
argTypes: ["truthy"],
|
|
1571
|
+
arity: { kind: "exact" }
|
|
1499
1572
|
}
|
|
1500
1573
|
};
|
|
1501
1574
|
}
|
|
1502
1575
|
function lazyBuiltins() {
|
|
1503
1576
|
return {
|
|
1504
1577
|
and: markLazy({
|
|
1505
|
-
argTypes: ["truthy"],
|
|
1578
|
+
argTypes: ["truthy", "truthy"],
|
|
1579
|
+
arity: { kind: "variadic" },
|
|
1506
1580
|
fn: (...thunks) => {
|
|
1507
1581
|
let last = true;
|
|
1508
1582
|
for (const t of thunks) {
|
|
@@ -1513,7 +1587,8 @@ function lazyBuiltins() {
|
|
|
1513
1587
|
}
|
|
1514
1588
|
}),
|
|
1515
1589
|
or: markLazy({
|
|
1516
|
-
argTypes: ["truthy"],
|
|
1590
|
+
argTypes: ["truthy", "truthy"],
|
|
1591
|
+
arity: { kind: "variadic" },
|
|
1517
1592
|
fn: (...thunks) => {
|
|
1518
1593
|
let last = false;
|
|
1519
1594
|
for (const t of thunks) {
|
|
@@ -1555,20 +1630,41 @@ function goLen(value) {
|
|
|
1555
1630
|
return Object.keys(value).length;
|
|
1556
1631
|
}
|
|
1557
1632
|
function goIndex(collection, key) {
|
|
1558
|
-
if (Array.isArray(collection)) {
|
|
1559
|
-
const i =
|
|
1560
|
-
if (
|
|
1561
|
-
if (i < 0 || i >= collection.length) throw new Error(`index: out of range [${i}] with length ${collection.length}`);
|
|
1633
|
+
if (Array.isArray(collection) || typeof collection === "string") {
|
|
1634
|
+
const i = indexArg(key, collection.length);
|
|
1635
|
+
if (i === collection.length) throw new Error(`index out of range: ${i}`);
|
|
1562
1636
|
return collection[i];
|
|
1563
1637
|
}
|
|
1564
1638
|
if (collection instanceof Map) return collection.get(key);
|
|
1565
|
-
if (typeof collection === "string") return collection[Number(key)];
|
|
1566
1639
|
if (collection !== null && typeof collection === "object") {
|
|
1567
1640
|
if (typeof key !== "string") throw new Error(`index: object index must be string, got ${describeType(key)}`);
|
|
1568
1641
|
return collection[key];
|
|
1569
1642
|
}
|
|
1570
1643
|
throw new Error(`index: cannot index ${describeType(collection)}`);
|
|
1571
1644
|
}
|
|
1645
|
+
function indexArg(key, cap) {
|
|
1646
|
+
const x = typeof key === "bigint" ? Number(key) : key;
|
|
1647
|
+
if (typeof x !== "number" || !Number.isInteger(x)) throw new Error(`cannot index slice/array with type ${goPrintfType(key)}`);
|
|
1648
|
+
if (x < 0 || x > cap) throw new Error(`index out of range: ${String(key)}`);
|
|
1649
|
+
return x;
|
|
1650
|
+
}
|
|
1651
|
+
function goSlice(collection, indices) {
|
|
1652
|
+
if (indices.length > 3) throw new Error(`too many slice indexes: ${indices.length}`);
|
|
1653
|
+
if (typeof collection === "string" && indices.length > 2) throw new Error("cannot 3-index slice a string");
|
|
1654
|
+
const cap = collection.length;
|
|
1655
|
+
const bounds = [
|
|
1656
|
+
0,
|
|
1657
|
+
cap,
|
|
1658
|
+
cap
|
|
1659
|
+
];
|
|
1660
|
+
indices.forEach((ix, n) => {
|
|
1661
|
+
bounds[n] = indexArg(ix, cap);
|
|
1662
|
+
});
|
|
1663
|
+
const [i, j, k] = bounds;
|
|
1664
|
+
if (i > j) throw new Error(`invalid slice index: ${i} > ${j}`);
|
|
1665
|
+
if (j > k) throw new Error(`invalid slice index: ${j} > ${k}`);
|
|
1666
|
+
return collection.slice(i, j);
|
|
1667
|
+
}
|
|
1572
1668
|
function describeType(v) {
|
|
1573
1669
|
if (v === null) return "null";
|
|
1574
1670
|
if (v === void 0) return "undefined";
|
|
@@ -1586,22 +1682,20 @@ function goPrintfType(v) {
|
|
|
1586
1682
|
if (v instanceof Map || typeof v === "object") return "map[string]interface {}";
|
|
1587
1683
|
return typeof v;
|
|
1588
1684
|
}
|
|
1589
|
-
function goPrint(args, toString) {
|
|
1685
|
+
function goPrint(args, toString, isT) {
|
|
1590
1686
|
let out = "";
|
|
1591
1687
|
let prevIsString = false;
|
|
1592
1688
|
for (let i = 0; i < args.length; i++) {
|
|
1593
1689
|
const arg = args[i];
|
|
1594
1690
|
const isString = typeof arg === "string";
|
|
1595
1691
|
if (i > 0 && !prevIsString && !isString) out += " ";
|
|
1596
|
-
out +=
|
|
1692
|
+
out += formatV(arg, toString, isT);
|
|
1597
1693
|
prevIsString = isString;
|
|
1598
1694
|
}
|
|
1599
1695
|
return out;
|
|
1600
1696
|
}
|
|
1601
|
-
function
|
|
1602
|
-
|
|
1603
|
-
if (typeof value === "string") return value;
|
|
1604
|
-
return toString(value);
|
|
1697
|
+
function goPrintln(args, toString, isT) {
|
|
1698
|
+
return args.map((arg) => formatV(arg, toString, isT)).join(" ");
|
|
1605
1699
|
}
|
|
1606
1700
|
const HTML_ESCAPE_TABLE = {
|
|
1607
1701
|
"\0": "�",
|
|
@@ -1675,26 +1769,34 @@ function sprintf(format, args, toString, isT) {
|
|
|
1675
1769
|
}
|
|
1676
1770
|
const verb = format[j];
|
|
1677
1771
|
const spec = format.slice(i + 1, j);
|
|
1772
|
+
const missing = argIdx >= args.length;
|
|
1678
1773
|
const arg = args[argIdx++];
|
|
1679
|
-
out += formatVerb(verb ?? "", spec, arg, toString, isT);
|
|
1774
|
+
out += missing ? `%!${verb ?? ""}(MISSING)` : formatVerb(verb ?? "", spec, arg, toString, isT);
|
|
1680
1775
|
i = j + 1;
|
|
1681
1776
|
}
|
|
1682
1777
|
return out;
|
|
1683
1778
|
}
|
|
1684
1779
|
function formatVerb(verb, spec, arg, toString, isT) {
|
|
1780
|
+
if (arg instanceof Uint8Array && (verb === "s" || verb === "q")) return formatLeafVerb(verb, spec, new TextDecoder().decode(arg), toString, isT);
|
|
1781
|
+
if (arg instanceof Uint8Array && verb === "x") return applyWidth(spec, Array.from(arg, (b) => b.toString(16).padStart(2, "0")).join(""));
|
|
1782
|
+
return formatWalk(arg, (leaf) => formatLeafVerb(verb, spec, leaf, toString, isT), toString, isT);
|
|
1783
|
+
}
|
|
1784
|
+
function formatLeafVerb(verb, spec, arg, toString, isT) {
|
|
1685
1785
|
switch (verb) {
|
|
1686
|
-
case "s": return
|
|
1786
|
+
case "s": return applyWidth(spec, formatLeafV(arg, toString, isT));
|
|
1687
1787
|
case "d":
|
|
1688
|
-
if (typeof arg !== "number" && typeof arg !== "bigint") return
|
|
1788
|
+
if (typeof arg !== "number" && typeof arg !== "bigint") return badVerb(verb, arg, toString, isT);
|
|
1689
1789
|
return applyWidth(spec, String(typeof arg === "bigint" ? arg : Math.trunc(arg)));
|
|
1690
|
-
case "v": return applyWidth(spec,
|
|
1691
|
-
case "q": return applyWidth(spec, JSON.stringify(
|
|
1790
|
+
case "v": return applyWidth(spec, formatLeafV(arg, toString, isT));
|
|
1791
|
+
case "q": return applyWidth(spec, JSON.stringify(formatLeafV(arg, toString, isT)));
|
|
1692
1792
|
case "f": {
|
|
1693
|
-
if (typeof arg !== "number" && typeof arg !== "bigint") return
|
|
1793
|
+
if (typeof arg !== "number" && typeof arg !== "bigint") return badVerb(verb, arg, toString, isT);
|
|
1694
1794
|
const precision = spec.includes(".") ? Number(spec.split(".")[1]) : 6;
|
|
1695
1795
|
return applyWidth(spec, Number(arg).toFixed(precision));
|
|
1696
1796
|
}
|
|
1697
|
-
case "t":
|
|
1797
|
+
case "t":
|
|
1798
|
+
if (typeof arg !== "boolean") return badVerb(verb, arg, toString, isT);
|
|
1799
|
+
return applyWidth(spec, String(arg));
|
|
1698
1800
|
case "x":
|
|
1699
1801
|
if (typeof arg === "string") {
|
|
1700
1802
|
const bytes = new TextEncoder().encode(arg);
|
|
@@ -1702,12 +1804,16 @@ function formatVerb(verb, spec, arg, toString, isT) {
|
|
|
1702
1804
|
for (const b of bytes) hex += b.toString(16).padStart(2, "0");
|
|
1703
1805
|
return applyWidth(spec, hex);
|
|
1704
1806
|
}
|
|
1705
|
-
if (typeof arg !== "number" && typeof arg !== "bigint") return
|
|
1807
|
+
if (typeof arg !== "number" && typeof arg !== "bigint") return badVerb(verb, arg, toString, isT);
|
|
1706
1808
|
if (typeof arg === "bigint") return applyWidth(spec, arg.toString(16));
|
|
1707
1809
|
return applyWidth(spec, Math.trunc(arg).toString(16));
|
|
1708
|
-
default: return
|
|
1810
|
+
default: return badVerb(verb, arg, toString, isT);
|
|
1709
1811
|
}
|
|
1710
1812
|
}
|
|
1813
|
+
function badVerb(verb, arg, toString, isT) {
|
|
1814
|
+
if (arg === null || arg === void 0) return `%!${verb}(<nil>)`;
|
|
1815
|
+
return `%!${verb}(${goPrintfType(arg)}=${formatLeafV(arg, toString, isT)})`;
|
|
1816
|
+
}
|
|
1711
1817
|
function applyWidth(spec, body) {
|
|
1712
1818
|
const flag = spec.match(/^([+\-# 0]*)/)?.[1] ?? "";
|
|
1713
1819
|
const widthMatch = spec.slice(flag.length).match(/^(\d+)/);
|
|
@@ -1718,70 +1824,248 @@ function applyWidth(spec, body) {
|
|
|
1718
1824
|
return flag.includes("-") ? body + pad : pad + body;
|
|
1719
1825
|
}
|
|
1720
1826
|
function formatV(value, toString, isT) {
|
|
1827
|
+
try {
|
|
1828
|
+
return formatWalk(value, (leaf) => formatLeafV(leaf, toString, isT), toString, isT);
|
|
1829
|
+
} catch (err) {
|
|
1830
|
+
if (err instanceof UnprintableError) throw new UnprintableError(err.leaf, err.cause, value);
|
|
1831
|
+
throw err;
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
function formatWalk(value, leaf, toString, isT) {
|
|
1835
|
+
if (isT(value)) return leaf(value);
|
|
1836
|
+
const walk = (v) => formatWalk(v, leaf, toString, isT);
|
|
1837
|
+
if (Array.isArray(value)) return `[${value.map(walk).join(" ")}]`;
|
|
1838
|
+
if (ArrayBuffer.isView(value) && !(value instanceof DataView)) return `[${Array.from(value, walk).join(" ")}]`;
|
|
1839
|
+
if (value instanceof Map) return formatEntries([...value.entries()], walk, toString, isT);
|
|
1840
|
+
if (isPlainObject(value)) return formatEntries(Object.entries(value), walk, toString, isT);
|
|
1841
|
+
if (isStruct(value)) return `{${Object.values(value).map(walk).join(" ")}}`;
|
|
1842
|
+
return leaf(value);
|
|
1843
|
+
}
|
|
1844
|
+
function isStruct(value) {
|
|
1845
|
+
return typeof value === "object" && value !== null && !(value instanceof Set) && !hasOwnString(value);
|
|
1846
|
+
}
|
|
1847
|
+
function formatLeafV(value, toString, isT) {
|
|
1721
1848
|
if (value === null || value === void 0) return "<nil>";
|
|
1722
1849
|
if (typeof value === "string") return value;
|
|
1723
|
-
if (isT(value)) return
|
|
1724
|
-
if (
|
|
1725
|
-
if (value
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1850
|
+
if (isT(value)) return flattenT(value, toString);
|
|
1851
|
+
if (typeof value === "number" || typeof value === "bigint" || typeof value === "boolean") return String(value);
|
|
1852
|
+
if (typeof value === "object" && hasOwnString(value)) return stringerText(value);
|
|
1853
|
+
throw new UnprintableError(value);
|
|
1854
|
+
}
|
|
1855
|
+
function flattenT(value, toString) {
|
|
1856
|
+
try {
|
|
1857
|
+
return toString(value);
|
|
1858
|
+
} catch (err) {
|
|
1859
|
+
if (isStackOverflow(err)) throw err;
|
|
1860
|
+
throw new UnprintableError(value, err);
|
|
1861
|
+
}
|
|
1862
|
+
}
|
|
1863
|
+
function stringerText(value) {
|
|
1864
|
+
try {
|
|
1865
|
+
return String(value);
|
|
1866
|
+
} catch (err) {
|
|
1867
|
+
if (isStackOverflow(err)) throw err;
|
|
1868
|
+
throw new UnprintableError(value, err);
|
|
1869
|
+
}
|
|
1870
|
+
}
|
|
1871
|
+
function hasOwnString(value) {
|
|
1872
|
+
let root = value;
|
|
1873
|
+
for (let p = Object.getPrototypeOf(root); p !== null; p = Object.getPrototypeOf(p)) root = p;
|
|
1874
|
+
const own = value.toString;
|
|
1875
|
+
return typeof own === "function" && own !== root.toString;
|
|
1876
|
+
}
|
|
1877
|
+
/** A value Go's `%v` has no text form for: a Set, a function, a symbol, or
|
|
1878
|
+
* a T the engine's `toString` cannot flatten. The action and the call that
|
|
1879
|
+
* met it report it at their position. */
|
|
1880
|
+
var UnprintableError = class extends Error {
|
|
1881
|
+
name = "UnprintableError";
|
|
1882
|
+
/** What cannot print, named from the printed value: the leaf alone, or
|
|
1883
|
+
* the value holding it. */
|
|
1884
|
+
summary;
|
|
1885
|
+
constructor(leaf, cause, root = leaf) {
|
|
1886
|
+
const summary = root === leaf ? typeNameOf(leaf) : `${typeNameOf(root)} holding ${typeNameOf(leaf)}`;
|
|
1887
|
+
const why = cause instanceof TypeMismatchError ? `expected ${cause.expected}` : "it has no text form";
|
|
1888
|
+
super(`cannot print a value of type ${summary}: ${why}`, { cause });
|
|
1889
|
+
this.leaf = leaf;
|
|
1890
|
+
this.summary = summary;
|
|
1891
|
+
}
|
|
1892
|
+
};
|
|
1893
|
+
function typeNameOf(value) {
|
|
1894
|
+
if (value === null) return "null";
|
|
1895
|
+
if (Array.isArray(value)) return "array";
|
|
1896
|
+
if (typeof value !== "object") return typeof value;
|
|
1897
|
+
if (isPlainObject(value)) return "object";
|
|
1898
|
+
for (let p = Object.getPrototypeOf(value); p !== null; p = Object.getPrototypeOf(p)) {
|
|
1899
|
+
const name = p.constructor?.name;
|
|
1900
|
+
if (typeof name === "string" && name !== "") return name === "Object" ? "object" : name;
|
|
1901
|
+
}
|
|
1902
|
+
return "object";
|
|
1903
|
+
}
|
|
1904
|
+
function formatEntries(entries, walk, toString, isT) {
|
|
1731
1905
|
const isNumeric = (k) => typeof k === "number" || typeof k === "bigint";
|
|
1732
|
-
const byKey = (
|
|
1733
|
-
if (isNumeric(a) && isNumeric(b)) return a < b ? -1 : a > b ? 1 : 0;
|
|
1734
|
-
if (isNumeric(a) !== isNumeric(b)) return isNumeric(a) ? -1 : 1;
|
|
1735
|
-
|
|
1736
|
-
return as < bs ? -1 : as > bs ? 1 : 0;
|
|
1906
|
+
const byKey = (a, b) => {
|
|
1907
|
+
if (isNumeric(a.k) && isNumeric(b.k)) return a.k < b.k ? -1 : a.k > b.k ? 1 : 0;
|
|
1908
|
+
if (isNumeric(a.k) !== isNumeric(b.k)) return isNumeric(a.k) ? -1 : 1;
|
|
1909
|
+
return a.text < b.text ? -1 : a.text > b.text ? 1 : 0;
|
|
1737
1910
|
};
|
|
1738
|
-
return `map[${
|
|
1911
|
+
return `map[${entries.map(([k, v]) => ({
|
|
1912
|
+
k,
|
|
1913
|
+
v,
|
|
1914
|
+
text: formatV(k, toString, isT)
|
|
1915
|
+
})).sort(byKey).map(({ k, v }) => `${walk(k)}:${walk(v)}`).join(" ")}]`;
|
|
1739
1916
|
}
|
|
1740
1917
|
function isPlainObject(value) {
|
|
1741
1918
|
if (value === null || typeof value !== "object") return false;
|
|
1742
1919
|
if (Array.isArray(value)) return false;
|
|
1743
1920
|
if (value instanceof Map || value instanceof Set) return false;
|
|
1744
1921
|
const proto = Object.getPrototypeOf(value);
|
|
1745
|
-
return proto === null || proto ===
|
|
1922
|
+
return proto === null || Object.getPrototypeOf(proto) === null;
|
|
1923
|
+
}
|
|
1924
|
+
//#endregion
|
|
1925
|
+
//#region src/evaluator/include.ts
|
|
1926
|
+
const render = () => {
|
|
1927
|
+
throw new Error("include was called outside the evaluator's dispatch");
|
|
1928
|
+
};
|
|
1929
|
+
/** Helm's `include(name string, data interface{}) (string, error)`. */
|
|
1930
|
+
const include = {
|
|
1931
|
+
fn: render,
|
|
1932
|
+
argTypes: ["string", "value"],
|
|
1933
|
+
arity: { kind: "exact" },
|
|
1934
|
+
returnType: "string"
|
|
1935
|
+
};
|
|
1936
|
+
/** True iff `fn` is `include`, under whatever name or copy it was registered. */
|
|
1937
|
+
function isInclude(fn) {
|
|
1938
|
+
return fn.fn === render;
|
|
1939
|
+
}
|
|
1940
|
+
//#endregion
|
|
1941
|
+
//#region src/evaluator/marks.ts
|
|
1942
|
+
const NO_MARKS = /* @__PURE__ */ new Set();
|
|
1943
|
+
let createMarked;
|
|
1944
|
+
let openMarked;
|
|
1945
|
+
let readCarrier;
|
|
1946
|
+
/**
|
|
1947
|
+
* A func's return value together with the marks of the call that
|
|
1948
|
+
* produced it. Opaque: build one with {@link marked}; only the engine
|
|
1949
|
+
* reads it.
|
|
1950
|
+
*/
|
|
1951
|
+
var Marked = class Marked {
|
|
1952
|
+
#value;
|
|
1953
|
+
#marks;
|
|
1954
|
+
constructor(value, marks) {
|
|
1955
|
+
this.#value = value;
|
|
1956
|
+
this.#marks = marks;
|
|
1957
|
+
}
|
|
1958
|
+
static {
|
|
1959
|
+
createMarked = (value, marks) => value instanceof Marked ? new Marked(value.#value, [...value.#marks, ...marks]) : new Marked(value, marks);
|
|
1960
|
+
openMarked = (result) => result instanceof Marked ? {
|
|
1961
|
+
value: result.#value,
|
|
1962
|
+
marks: result.#marks
|
|
1963
|
+
} : {
|
|
1964
|
+
value: result,
|
|
1965
|
+
marks: []
|
|
1966
|
+
};
|
|
1967
|
+
readCarrier = (result, into) => {
|
|
1968
|
+
if (!(result instanceof Marked)) return result;
|
|
1969
|
+
for (const mark of result.#marks) into.add(mark);
|
|
1970
|
+
return result.#value;
|
|
1971
|
+
};
|
|
1972
|
+
}
|
|
1973
|
+
};
|
|
1974
|
+
/**
|
|
1975
|
+
* Return this from a registered func to mark the call: every emission
|
|
1976
|
+
* and control decision that depends on the call's result reports
|
|
1977
|
+
* `marks`. The template sees `value`, exactly as if it had been returned
|
|
1978
|
+
* directly.
|
|
1979
|
+
*/
|
|
1980
|
+
function marked(value, ...marks) {
|
|
1981
|
+
return createMarked(value, marks);
|
|
1982
|
+
}
|
|
1983
|
+
/**
|
|
1984
|
+
* A func's return read apart: its value and the marks it carries (none
|
|
1985
|
+
* for a plain value). For JS code that builds on a marking func outside a
|
|
1986
|
+
* template — `const { value, marks } = unmarked(tween.fn(...))`, then
|
|
1987
|
+
* `return marked(Math.min(1, value as number), ...marks)`.
|
|
1988
|
+
*/
|
|
1989
|
+
function unmarked(result) {
|
|
1990
|
+
return openMarked(result);
|
|
1991
|
+
}
|
|
1992
|
+
/** `a ∪ b`, sharing an operand when the other is empty. */
|
|
1993
|
+
function unionMarks(a, b) {
|
|
1994
|
+
if (b.size === 0) return a;
|
|
1995
|
+
if (a.size === 0) return b;
|
|
1996
|
+
return new Set([...a, ...b]);
|
|
1746
1997
|
}
|
|
1747
1998
|
//#endregion
|
|
1748
1999
|
//#region src/evaluator/scope.ts
|
|
1749
|
-
|
|
2000
|
+
/**
|
|
2001
|
+
* Lexical scope chain for the evaluator.
|
|
2002
|
+
*
|
|
2003
|
+
* [LAW:dataflow-not-control-flow] A `Scope` is a `dot`, a parent
|
|
2004
|
+
* pointer and the variables declared in it. Entering a construct or an
|
|
2005
|
+
* iteration *constructs* a frame and leaving it drops the frame, so
|
|
2006
|
+
* nothing is ever undone: iterations of `{{range}}` cannot see each
|
|
2007
|
+
* other's `:=` declarations — the data shape itself prevents the bug.
|
|
2008
|
+
*
|
|
2009
|
+
* Only a frame's variables change. A lookup walks the parent chain.
|
|
2010
|
+
* Assignment via `=` searches the chain and overwrites the binding in
|
|
2011
|
+
* the frame that declared it; declaration via `:=` always creates the
|
|
2012
|
+
* binding in the current leaf frame.
|
|
2013
|
+
*
|
|
2014
|
+
* `$` is an ordinary variable declared in a chain's root frame, as in
|
|
2015
|
+
* Go, where it is `vars[0]`: so `$ = v` assigns it, and a `template` /
|
|
2016
|
+
* `block` invocation starts a fresh chain whose only variable is `$`.
|
|
2017
|
+
*/
|
|
2018
|
+
/** The frame a template body starts in: `.` and `$` are both `dot`. */
|
|
2019
|
+
function rootScope(dot) {
|
|
1750
2020
|
return {
|
|
1751
|
-
dot
|
|
1752
|
-
root: value,
|
|
2021
|
+
dot,
|
|
1753
2022
|
parent: void 0,
|
|
1754
|
-
vars:
|
|
2023
|
+
vars: new Map([["$", dot]])
|
|
1755
2024
|
};
|
|
1756
2025
|
}
|
|
1757
2026
|
function pushScope(parent, dot) {
|
|
1758
2027
|
return {
|
|
1759
2028
|
dot,
|
|
1760
|
-
root: parent.root,
|
|
1761
2029
|
parent,
|
|
1762
2030
|
vars: /* @__PURE__ */ new Map()
|
|
1763
2031
|
};
|
|
1764
2032
|
}
|
|
2033
|
+
function findFrame(scope, name) {
|
|
2034
|
+
let cur = scope;
|
|
2035
|
+
while (cur !== void 0 && !cur.vars.has(name)) cur = cur.parent;
|
|
2036
|
+
return cur;
|
|
2037
|
+
}
|
|
1765
2038
|
/**
|
|
1766
2039
|
* Look up a `$var` binding, walking the parent chain.
|
|
1767
|
-
* Returns
|
|
1768
|
-
*
|
|
1769
|
-
* "not declared" from "declared but undefined".
|
|
2040
|
+
* Returns the binding on hit and `undefined` on miss — a `Binding` is a
|
|
2041
|
+
* record, so a variable declared as `undefined` is still a hit.
|
|
1770
2042
|
*/
|
|
1771
2043
|
function lookupVar(scope, name) {
|
|
1772
|
-
|
|
1773
|
-
while (cur !== void 0) {
|
|
1774
|
-
if (cur.vars.has(name)) return {
|
|
1775
|
-
found: true,
|
|
1776
|
-
value: cur.vars.get(name)
|
|
1777
|
-
};
|
|
1778
|
-
cur = cur.parent;
|
|
1779
|
-
}
|
|
1780
|
-
return { found: false };
|
|
2044
|
+
return findFrame(scope, name)?.vars.get(name);
|
|
1781
2045
|
}
|
|
1782
2046
|
/** Declare a new variable in the leaf scope (for `:=`). */
|
|
1783
|
-
function declareVar(scope, name,
|
|
1784
|
-
scope.vars.set(name,
|
|
2047
|
+
function declareVar(scope, name, binding) {
|
|
2048
|
+
scope.vars.set(name, binding);
|
|
2049
|
+
}
|
|
2050
|
+
/**
|
|
2051
|
+
* Overwrite the nearest frame's binding of `name` (for `=`), as Go's
|
|
2052
|
+
* text/template exec.go walks its variable stack. Returns whether any
|
|
2053
|
+
* frame declared it; the caller owns the "undefined variable" error,
|
|
2054
|
+
* which needs a position this module does not have.
|
|
2055
|
+
*/
|
|
2056
|
+
function assignVar(scope, name, binding) {
|
|
2057
|
+
const frame = findFrame(scope, name);
|
|
2058
|
+
frame?.vars.set(name, binding);
|
|
2059
|
+
return frame !== void 0;
|
|
2060
|
+
}
|
|
2061
|
+
/** Add `marks` to what `name`'s binding depends on, if any frame declared it. */
|
|
2062
|
+
function markVar(scope, name, marks) {
|
|
2063
|
+
const binding = lookupVar(scope, name);
|
|
2064
|
+
if (binding === void 0) return;
|
|
2065
|
+
assignVar(scope, name, {
|
|
2066
|
+
value: binding.value,
|
|
2067
|
+
marks: unionMarks(binding.marks, marks)
|
|
2068
|
+
});
|
|
1785
2069
|
}
|
|
1786
2070
|
//#endregion
|
|
1787
2071
|
//#region src/evaluator/evaluator.ts
|
|
@@ -1802,6 +2086,43 @@ function declareVar(scope, name, value) {
|
|
|
1802
2086
|
* no instance mutation, so the same Engine handles concurrent evaluate
|
|
1803
2087
|
* calls (tested through the parse-once-eval-many invariant).
|
|
1804
2088
|
*/
|
|
2089
|
+
/**
|
|
2090
|
+
* Every kind a slot may declare.
|
|
2091
|
+
*
|
|
2092
|
+
* [LAW:one-source-of-truth] The kinds are a *list* first and a type
|
|
2093
|
+
* second. `validateArgSpecs` must reject a declaration naming a kind
|
|
2094
|
+
* that does not exist, and a runtime check needs a runtime list; writing
|
|
2095
|
+
* that list out beside a hand-maintained union would be the second
|
|
2096
|
+
* clock. So the array is authoritative and `ArgType` is derived from it
|
|
2097
|
+
* — one edit adds a kind to the type, to the construct-time check, and
|
|
2098
|
+
* to the two exhaustive switches that must then grow an arm.
|
|
2099
|
+
*
|
|
2100
|
+
* The per-kind semantics live on `ArgType` below, where a consumer
|
|
2101
|
+
* hovering the exported name finds them.
|
|
2102
|
+
*/
|
|
2103
|
+
const ARG_TYPES = [
|
|
2104
|
+
"string",
|
|
2105
|
+
"int",
|
|
2106
|
+
"truncating-int",
|
|
2107
|
+
"float",
|
|
2108
|
+
"bool",
|
|
2109
|
+
"T",
|
|
2110
|
+
"ordered",
|
|
2111
|
+
"list",
|
|
2112
|
+
"dict",
|
|
2113
|
+
"sized",
|
|
2114
|
+
"comparable",
|
|
2115
|
+
"stringifiable",
|
|
2116
|
+
"liftable",
|
|
2117
|
+
"callable",
|
|
2118
|
+
"collection",
|
|
2119
|
+
"index-key",
|
|
2120
|
+
"sliceable",
|
|
2121
|
+
"truthy",
|
|
2122
|
+
"reflective",
|
|
2123
|
+
"value",
|
|
2124
|
+
"serializable"
|
|
2125
|
+
];
|
|
1805
2126
|
const VALID_MISSING_KEYS = new Set([
|
|
1806
2127
|
"default",
|
|
1807
2128
|
"zero",
|
|
@@ -1832,9 +2153,28 @@ function validateDelims(value) {
|
|
|
1832
2153
|
right: value.right
|
|
1833
2154
|
};
|
|
1834
2155
|
}
|
|
2156
|
+
function readsSlotsFromArgTypes(arity) {
|
|
2157
|
+
switch (arity.kind) {
|
|
2158
|
+
case "exact": return false;
|
|
2159
|
+
case "variadic": return true;
|
|
2160
|
+
case "alternating": return true;
|
|
2161
|
+
}
|
|
2162
|
+
}
|
|
2163
|
+
function validateArgSpecs(funcs) {
|
|
2164
|
+
const legal = new Set(ARG_TYPES);
|
|
2165
|
+
for (const [name, fn] of Object.entries(funcs)) {
|
|
2166
|
+
fn.argTypes.forEach((declared, slot) => {
|
|
2167
|
+
if (!legal.has(declared)) throw new Error(`EngineConfig.funcs.${name}: argTypes[${slot}] is ${JSON.stringify(declared)}, which is not an ArgType; declare one of ${ARG_TYPES.join(", ")}`);
|
|
2168
|
+
});
|
|
2169
|
+
if (readsSlotsFromArgTypes(fn.arity) && fn.argTypes.length === 0) throw new Error(`EngineConfig.funcs.${name}: arity ${JSON.stringify(fn.arity.kind)} needs at least one declared argType (it reads its repeating slot from the end of argTypes); declare the slots the Go signature has, or use { kind: "exact" }`);
|
|
2170
|
+
if (fn.arity.kind === "alternating" && (!Number.isSafeInteger(fn.arity.minimum) || fn.arity.minimum < 0)) throw new Error(`EngineConfig.funcs.${name}: arity "alternating" needs a non-negative integer minimum, got ${String(fn.arity.minimum)}; the gate rejects every count below it, so a negative or non-finite minimum would disable the lower bound`);
|
|
2171
|
+
}
|
|
2172
|
+
return funcs;
|
|
2173
|
+
}
|
|
1835
2174
|
const BREAK_SIGNAL = Object.freeze({ kind: "break" });
|
|
1836
2175
|
const CONTINUE_SIGNAL = Object.freeze({ kind: "continue" });
|
|
1837
2176
|
const NO_PIPE = { kind: "none" };
|
|
2177
|
+
const notDefined = (name) => `template ${JSON.stringify(name)} is not defined`;
|
|
1838
2178
|
/**
|
|
1839
2179
|
* A parsed template bound to its parent engine.
|
|
1840
2180
|
*
|
|
@@ -1851,18 +2191,20 @@ let internalCreateTemplate;
|
|
|
1851
2191
|
var Template = class Template {
|
|
1852
2192
|
source;
|
|
1853
2193
|
_evaluate;
|
|
2194
|
+
_evaluateDefine;
|
|
1854
2195
|
_defines;
|
|
1855
2196
|
_referencedFunctions;
|
|
1856
2197
|
_referencedCalls;
|
|
1857
|
-
constructor(source, evaluate, defines, referencedFunctions, referencedCalls) {
|
|
2198
|
+
constructor(source, evaluate, evaluateDefine, defines, referencedFunctions, referencedCalls) {
|
|
1858
2199
|
this.source = source;
|
|
1859
2200
|
this._evaluate = evaluate;
|
|
2201
|
+
this._evaluateDefine = evaluateDefine;
|
|
1860
2202
|
this._defines = defines;
|
|
1861
2203
|
this._referencedFunctions = referencedFunctions;
|
|
1862
2204
|
this._referencedCalls = referencedCalls;
|
|
1863
2205
|
}
|
|
1864
2206
|
static {
|
|
1865
|
-
internalCreateTemplate = (source, evaluate, defines, referencedFunctions, referencedCalls) => new Template(source, evaluate, defines, referencedFunctions, referencedCalls);
|
|
2207
|
+
internalCreateTemplate = (source, evaluate, evaluateDefine, defines, referencedFunctions, referencedCalls) => new Template(source, evaluate, evaluateDefine, defines, referencedFunctions, referencedCalls);
|
|
1866
2208
|
}
|
|
1867
2209
|
/**
|
|
1868
2210
|
* The named sub-templates this template can invoke — its own `{{define}}`s
|
|
@@ -1880,6 +2222,15 @@ var Template = class Template {
|
|
|
1880
2222
|
return this._evaluate(scope);
|
|
1881
2223
|
}
|
|
1882
2224
|
/**
|
|
2225
|
+
* Render one named sub-template — any `{{ define }}` in {@link defines}, its
|
|
2226
|
+
* own or inherited — with `scope` as its dot and `$`, as Go's
|
|
2227
|
+
* `ExecuteTemplate` does. The output is the same `T[]` a whole-template
|
|
2228
|
+
* {@link evaluate} produces, through the same `emitted` / `decided` hooks.
|
|
2229
|
+
*/
|
|
2230
|
+
evaluateDefine(name, scope) {
|
|
2231
|
+
return this._evaluateDefine(name, scope);
|
|
2232
|
+
}
|
|
2233
|
+
/**
|
|
1883
2234
|
* The FuncMap function names this template references — every identifier that
|
|
1884
2235
|
* names a function in this parse: the template body and its own `{{ define }}`
|
|
1885
2236
|
* blocks. A set inherited at parse (see {@link Template.defines}) is described
|
|
@@ -1995,17 +2346,21 @@ var Engine = class {
|
|
|
1995
2346
|
funcs;
|
|
1996
2347
|
missingKey;
|
|
1997
2348
|
delims;
|
|
2349
|
+
emitted;
|
|
2350
|
+
decided;
|
|
1998
2351
|
constructor(config) {
|
|
1999
2352
|
this.fromString = config.fromString;
|
|
2000
2353
|
const userToString = Object.hasOwn(config, "toString") ? config.toString : void 0;
|
|
2001
2354
|
this.toString = userToString ?? defaultToString;
|
|
2002
|
-
this.isT = config.isT ?? DEFAULT_IS_T;
|
|
2003
2355
|
this.missingKey = validateMissingKey(config.missingKey);
|
|
2004
2356
|
this.delims = validateDelims(config.delims);
|
|
2005
|
-
this.
|
|
2357
|
+
this.isT = config.isT ?? DEFAULT_IS_T;
|
|
2358
|
+
this.emitted = config.emitted ?? ((value) => value);
|
|
2359
|
+
this.decided = config.decided ?? (() => {});
|
|
2360
|
+
this.funcs = validateArgSpecs({
|
|
2006
2361
|
...defaultBuiltins(this.toString, this.isT),
|
|
2007
2362
|
...config.funcs ?? {}
|
|
2008
|
-
};
|
|
2363
|
+
});
|
|
2009
2364
|
}
|
|
2010
2365
|
/**
|
|
2011
2366
|
* Parse a template source into a reusable Template handle.
|
|
@@ -2016,7 +2371,14 @@ var Engine = class {
|
|
|
2016
2371
|
*/
|
|
2017
2372
|
parse(source, inherit) {
|
|
2018
2373
|
const parsed = parse(source, this.delims, inherit);
|
|
2019
|
-
return internalCreateTemplate(parsed.source, (scope) => this.evalParsed(parsed, scope
|
|
2374
|
+
return internalCreateTemplate(parsed.source, (scope) => this.evalParsed(parsed, scope, (root, ctx) => this.evalList(parsed.root, root, ctx)), (name, scope) => {
|
|
2375
|
+
const entry = lookupDefine(parsed.defines, name);
|
|
2376
|
+
if (!entry) throw new Error(notDefined(name));
|
|
2377
|
+
return this.evalParsed(parsed, scope, (root, ctx) => this.renderDefine(name, entry, root.dot, entry.list.pos, {
|
|
2378
|
+
...ctx,
|
|
2379
|
+
source: entry.source
|
|
2380
|
+
}));
|
|
2381
|
+
}, parsed.defines, collectReferencedFunctions(parsed), collectReferencedCalls(parsed));
|
|
2020
2382
|
}
|
|
2021
2383
|
/**
|
|
2022
2384
|
* Convenience sugar for `parse(src).evaluate.bind(template)`.
|
|
@@ -2037,15 +2399,19 @@ var Engine = class {
|
|
|
2037
2399
|
evaluate(template, scope) {
|
|
2038
2400
|
return template.evaluate(scope);
|
|
2039
2401
|
}
|
|
2040
|
-
evalParsed(parsed, scope) {
|
|
2402
|
+
evalParsed(parsed, scope, body) {
|
|
2041
2403
|
const out = [];
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2404
|
+
body(rootScope({
|
|
2405
|
+
value: scope,
|
|
2406
|
+
marks: NO_MARKS
|
|
2407
|
+
}), {
|
|
2408
|
+
emit: (value, marks) => out.push(this.emitted(value, marks)),
|
|
2409
|
+
decided: this.decided,
|
|
2045
2410
|
defines: parsed.defines,
|
|
2046
|
-
source: parsed.source
|
|
2047
|
-
|
|
2048
|
-
|
|
2411
|
+
source: parsed.source,
|
|
2412
|
+
marks: NO_MARKS,
|
|
2413
|
+
depth: 0
|
|
2414
|
+
});
|
|
2049
2415
|
return out;
|
|
2050
2416
|
}
|
|
2051
2417
|
evalList(node, scope, ctx) {
|
|
@@ -2054,14 +2420,15 @@ var Engine = class {
|
|
|
2054
2420
|
evalNode(node, scope, ctx) {
|
|
2055
2421
|
switch (node.type) {
|
|
2056
2422
|
case "Text":
|
|
2057
|
-
ctx.
|
|
2423
|
+
ctx.emit(this.fromString(node.text), ctx.marks);
|
|
2058
2424
|
return;
|
|
2059
2425
|
case "Comment": return;
|
|
2060
|
-
case "Action":
|
|
2061
|
-
|
|
2062
|
-
|
|
2426
|
+
case "Action":
|
|
2427
|
+
this.read(ctx, (reads) => {
|
|
2428
|
+
const value = this.evalPipe(node.pipe, scope, ctx, reads);
|
|
2429
|
+
if (node.pipe.decls.length === 0) ctx.emit(this.lift(value, node.pos, ctx.source), unionMarks(reads, ctx.marks));
|
|
2430
|
+
});
|
|
2063
2431
|
return;
|
|
2064
|
-
}
|
|
2065
2432
|
case "List":
|
|
2066
2433
|
this.evalList(node, scope, ctx);
|
|
2067
2434
|
return;
|
|
@@ -2097,36 +2464,47 @@ var Engine = class {
|
|
|
2097
2464
|
}
|
|
2098
2465
|
}
|
|
2099
2466
|
evalIf(node, scope, ctx) {
|
|
2100
|
-
|
|
2101
|
-
|
|
2467
|
+
const frame = constructFrame(scope);
|
|
2468
|
+
const { value: cond, reads } = this.read(ctx, (r) => this.evalPipe(node.pipe, frame, ctx, r));
|
|
2469
|
+
const arm = this.decide(reads, ctx, frame, node);
|
|
2470
|
+
if (isTruthy(cond)) this.evalList(node.list, frame, arm);
|
|
2471
|
+
else if (node.elseList) this.evalList(node.elseList, frame, arm);
|
|
2102
2472
|
}
|
|
2103
2473
|
evalWith(node, scope, ctx) {
|
|
2104
|
-
const
|
|
2105
|
-
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2474
|
+
const frame = constructFrame(scope);
|
|
2475
|
+
const { value, reads } = this.read(ctx, (r) => this.evalPipe(node.pipe, frame, ctx, r));
|
|
2476
|
+
const arm = this.decide(reads, ctx, frame, node);
|
|
2477
|
+
if (isTruthy(value)) this.evalList(node.list, pushScope(frame, {
|
|
2478
|
+
value,
|
|
2479
|
+
marks: arm.marks
|
|
2480
|
+
}), arm);
|
|
2481
|
+
else if (node.elseList) this.evalList(node.elseList, frame, arm);
|
|
2109
2482
|
}
|
|
2110
2483
|
evalRange(node, scope, ctx) {
|
|
2111
|
-
const
|
|
2112
|
-
const
|
|
2484
|
+
const rangeScope = constructFrame(scope);
|
|
2485
|
+
const { value, reads } = this.read(ctx, (r) => this.evalPipe(node.pipe, rangeScope, ctx, r));
|
|
2486
|
+
const arm = this.decide(reads, ctx, rangeScope, node);
|
|
2487
|
+
const { decls, isAssign } = node.pipe;
|
|
2113
2488
|
const entries = enumerateForRange(value);
|
|
2114
2489
|
try {
|
|
2115
2490
|
if (entries.length === 0) {
|
|
2116
|
-
if (node.elseList) this.evalList(node.elseList,
|
|
2491
|
+
if (node.elseList) this.evalList(node.elseList, rangeScope, arm);
|
|
2117
2492
|
return;
|
|
2118
2493
|
}
|
|
2119
2494
|
for (const [key, item] of entries) {
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2495
|
+
decls.forEach((decl, i) => {
|
|
2496
|
+
const binding = {
|
|
2497
|
+
value: i === decls.length - 1 ? item : key,
|
|
2498
|
+
marks: arm.marks
|
|
2499
|
+
};
|
|
2500
|
+
this.bind(rangeScope, decl, isAssign, binding, ctx);
|
|
2501
|
+
});
|
|
2502
|
+
const child = pushScope(rangeScope, {
|
|
2503
|
+
value: item,
|
|
2504
|
+
marks: arm.marks
|
|
2505
|
+
});
|
|
2128
2506
|
try {
|
|
2129
|
-
this.evalList(node.list, child,
|
|
2507
|
+
this.evalList(node.list, child, arm);
|
|
2130
2508
|
} catch (e) {
|
|
2131
2509
|
if (e === CONTINUE_SIGNAL) continue;
|
|
2132
2510
|
throw e;
|
|
@@ -2138,59 +2516,143 @@ var Engine = class {
|
|
|
2138
2516
|
}
|
|
2139
2517
|
}
|
|
2140
2518
|
evalTemplateInvoke(node, scope, ctx) {
|
|
2141
|
-
const entry =
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2519
|
+
const entry = this.definedEntry(node.name, node.pos, ctx);
|
|
2520
|
+
const dot = this.invocationDot(node.pipe, scope, ctx);
|
|
2521
|
+
this.renderDefine(node.name, entry, dot, node.pos, ctx);
|
|
2522
|
+
}
|
|
2523
|
+
definedEntry(name, pos, ctx) {
|
|
2524
|
+
const entry = lookupDefine(ctx.defines, name);
|
|
2525
|
+
if (!entry) throw new EvalError(notDefined(name), pos, { source: ctx.source });
|
|
2526
|
+
return entry;
|
|
2527
|
+
}
|
|
2528
|
+
renderDefine(name, body, dot, pos, ctx) {
|
|
2529
|
+
try {
|
|
2530
|
+
this.evalList(body.list, rootScope(dot), {
|
|
2531
|
+
...ctx,
|
|
2532
|
+
source: body.source,
|
|
2533
|
+
depth: ctx.depth + 1
|
|
2534
|
+
});
|
|
2535
|
+
} catch (e) {
|
|
2536
|
+
if (!isStackOverflow(e)) throw e;
|
|
2537
|
+
throw new EvalError(`template ${JSON.stringify(name)}: nesting depth ${ctx.depth + 1} exceeds the host's stack`, pos, {
|
|
2538
|
+
source: ctx.source,
|
|
2539
|
+
cause: e
|
|
2540
|
+
});
|
|
2541
|
+
}
|
|
2542
|
+
}
|
|
2543
|
+
include(registered, name, data, pos, ctx, reads) {
|
|
2544
|
+
const entry = this.definedEntry(name, pos, ctx);
|
|
2545
|
+
const parts = [];
|
|
2546
|
+
const emit = (value, marks) => {
|
|
2547
|
+
parts.push(this.toString(value));
|
|
2548
|
+
for (const mark of marks) reads.add(mark);
|
|
2549
|
+
};
|
|
2550
|
+
const decided = (marks) => {
|
|
2551
|
+
ctx.decided(marks);
|
|
2552
|
+
for (const mark of marks) reads.add(mark);
|
|
2553
|
+
};
|
|
2554
|
+
const dot = {
|
|
2555
|
+
value: data,
|
|
2556
|
+
marks: new Set([...reads, ...ctx.marks])
|
|
2557
|
+
};
|
|
2558
|
+
try {
|
|
2559
|
+
this.renderDefine(name, entry, dot, pos, {
|
|
2560
|
+
...ctx,
|
|
2561
|
+
emit,
|
|
2562
|
+
decided
|
|
2563
|
+
});
|
|
2564
|
+
} catch (e) {
|
|
2565
|
+
if (!(e instanceof TemplateError)) throw e;
|
|
2566
|
+
const root = innermost(e);
|
|
2567
|
+
throw new (root instanceof FailError ? FailError : EvalError)(`error calling ${registered} ${JSON.stringify(name)}: ${root.message}`, pos, {
|
|
2568
|
+
source: ctx.source,
|
|
2569
|
+
cause: e
|
|
2570
|
+
});
|
|
2571
|
+
}
|
|
2572
|
+
return parts.join("");
|
|
2149
2573
|
}
|
|
2150
2574
|
evalBlock(node, scope, ctx) {
|
|
2151
|
-
const
|
|
2575
|
+
const dot = this.invocationDot(node.pipe, scope, ctx);
|
|
2152
2576
|
const entry = lookupDefine(ctx.defines, node.name) ?? {
|
|
2153
2577
|
list: node.list,
|
|
2154
2578
|
source: ctx.source
|
|
2155
2579
|
};
|
|
2156
|
-
this.
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2580
|
+
this.renderDefine(node.name, entry, dot, node.pos, ctx);
|
|
2581
|
+
}
|
|
2582
|
+
decide(reads, ctx, frame, arms) {
|
|
2583
|
+
const marks = unionMarks(ctx.marks, reads);
|
|
2584
|
+
ctx.decided(marks);
|
|
2585
|
+
for (const name of this.assignedIn(arms)) markVar(frame, name, marks);
|
|
2586
|
+
return {
|
|
2587
|
+
...ctx,
|
|
2588
|
+
marks
|
|
2589
|
+
};
|
|
2590
|
+
}
|
|
2591
|
+
assigned = /* @__PURE__ */ new WeakMap();
|
|
2592
|
+
assignedIn(arms) {
|
|
2593
|
+
const cached = this.assigned.get(arms.list);
|
|
2594
|
+
if (cached !== void 0) return cached;
|
|
2595
|
+
const names = /* @__PURE__ */ new Set();
|
|
2596
|
+
const collect = (n) => {
|
|
2597
|
+
if (n.type === "Pipe" && n.isAssign) for (const d of n.decls) names.add(d.idents[0] ?? "$");
|
|
2598
|
+
};
|
|
2599
|
+
for (const list of [arms.list, arms.elseList]) if (list) walk(list, collect);
|
|
2600
|
+
const found = [...names];
|
|
2601
|
+
this.assigned.set(arms.list, found);
|
|
2602
|
+
return found;
|
|
2603
|
+
}
|
|
2604
|
+
invocationDot(pipe, scope, ctx) {
|
|
2605
|
+
const { value, reads } = this.read(ctx, (r) => pipe ? this.evalPipe(pipe, scope, ctx, r) : null);
|
|
2606
|
+
return {
|
|
2607
|
+
value,
|
|
2608
|
+
marks: unionMarks(reads, ctx.marks)
|
|
2609
|
+
};
|
|
2161
2610
|
}
|
|
2162
|
-
|
|
2163
|
-
|
|
2611
|
+
read(ctx, evaluate) {
|
|
2612
|
+
const reads = /* @__PURE__ */ new Set();
|
|
2613
|
+
try {
|
|
2614
|
+
return {
|
|
2615
|
+
value: evaluate(reads),
|
|
2616
|
+
reads
|
|
2617
|
+
};
|
|
2618
|
+
} catch (e) {
|
|
2619
|
+
ctx.decided(unionMarks(reads, ctx.marks));
|
|
2620
|
+
throw e;
|
|
2621
|
+
}
|
|
2164
2622
|
}
|
|
2165
|
-
evalPipe(pipe, scope, ctx) {
|
|
2623
|
+
evalPipe(pipe, scope, ctx, reads) {
|
|
2166
2624
|
if (pipe.cmds.length === 0) throw new EvalError("empty pipeline", pipe.pos, { source: ctx.source });
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
value = this.evalCommand(
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2625
|
+
const inputs = /* @__PURE__ */ new Set();
|
|
2626
|
+
let value;
|
|
2627
|
+
try {
|
|
2628
|
+
value = this.evalCommand(pipe.cmds[0], scope, ctx, NO_PIPE, inputs);
|
|
2629
|
+
for (let i = 1; i < pipe.cmds.length; i++) {
|
|
2630
|
+
const next = pipe.cmds[i];
|
|
2631
|
+
value = this.evalCommand(next, scope, ctx, {
|
|
2632
|
+
kind: "value",
|
|
2633
|
+
value
|
|
2634
|
+
}, inputs);
|
|
2635
|
+
}
|
|
2636
|
+
} finally {
|
|
2637
|
+
for (const mark of inputs) reads.add(mark);
|
|
2174
2638
|
}
|
|
2175
|
-
|
|
2639
|
+
for (const decl of pipe.decls) this.bind(scope, decl, pipe.isAssign, {
|
|
2640
|
+
value,
|
|
2641
|
+
marks: unionMarks(inputs, ctx.marks)
|
|
2642
|
+
}, ctx);
|
|
2176
2643
|
return value;
|
|
2177
2644
|
}
|
|
2178
|
-
|
|
2179
|
-
const
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
decls: [],
|
|
2183
|
-
isAssign: false,
|
|
2184
|
-
cmds: pipe.cmds
|
|
2185
|
-
};
|
|
2186
|
-
return this.evalPipe(stripped, scope, ctx);
|
|
2645
|
+
bind(scope, decl, isAssign, binding, ctx) {
|
|
2646
|
+
const name = decl.idents[0] ?? "$";
|
|
2647
|
+
if (!isAssign) declareVar(scope, name, binding);
|
|
2648
|
+
else if (!assignVar(scope, name, binding)) throw undefinedVariable(name, decl.pos, ctx);
|
|
2187
2649
|
}
|
|
2188
|
-
evalCommand(cmd, scope, ctx, piped) {
|
|
2650
|
+
evalCommand(cmd, scope, ctx, piped, reads) {
|
|
2189
2651
|
if (cmd.args.length === 0) throw new EvalError("empty command", cmd.pos, { source: ctx.source });
|
|
2190
2652
|
const head = cmd.args[0];
|
|
2191
2653
|
if (head.type !== "Identifier") {
|
|
2192
2654
|
if (cmd.args.length > 1) throw new EvalError(`cannot apply arguments to a ${head.type} primary; only functions take arguments`, cmd.pos, { source: ctx.source });
|
|
2193
|
-
return this.evalPrimary(head, scope, ctx);
|
|
2655
|
+
return this.evalPrimary(head, scope, ctx, reads);
|
|
2194
2656
|
}
|
|
2195
2657
|
const fn = this.funcs[head.ident];
|
|
2196
2658
|
if (!fn) throw new FuncNotFoundError(head.ident, head.pos, {
|
|
@@ -2199,40 +2661,66 @@ var Engine = class {
|
|
|
2199
2661
|
});
|
|
2200
2662
|
const argNodes = cmd.args.slice(1);
|
|
2201
2663
|
const lazy = isLazy(fn);
|
|
2202
|
-
const args = argNodes.map((n) => lazy ? () => this.evalPrimary(n, scope, ctx) : this.evalPrimary(n, scope, ctx));
|
|
2664
|
+
const args = argNodes.map((n) => lazy ? () => this.evalPrimary(n, scope, ctx, reads) : this.evalPrimary(n, scope, ctx, reads));
|
|
2203
2665
|
if (piped.kind === "value") {
|
|
2204
2666
|
const v = piped.value;
|
|
2205
2667
|
args.push(lazy ? () => v : v);
|
|
2206
2668
|
}
|
|
2207
|
-
|
|
2669
|
+
return this.invoke(head.ident, fn, args, cmd.pos, ctx, reads);
|
|
2670
|
+
}
|
|
2671
|
+
invoke(name, fn, args, pos, ctx, reads) {
|
|
2208
2672
|
try {
|
|
2209
|
-
|
|
2673
|
+
enforceArgTypes(name, fn, args, pos, ctx.source, this.toString, this.fromString, this.isT);
|
|
2210
2674
|
} catch (e) {
|
|
2211
|
-
if (e instanceof
|
|
2212
|
-
|
|
2213
|
-
|
|
2675
|
+
if (e instanceof TemplateError) throw e;
|
|
2676
|
+
throw new EvalError(`error calling ${name}: ${errorText(e)}`, pos, {
|
|
2677
|
+
source: ctx.source,
|
|
2678
|
+
cause: e
|
|
2679
|
+
});
|
|
2680
|
+
}
|
|
2681
|
+
let result;
|
|
2682
|
+
try {
|
|
2683
|
+
result = readCarrier(isInclude(fn) ? this.include(name, args[0], args[1], pos, ctx, reads) : fn.fn(...args), reads);
|
|
2684
|
+
} catch (thrown) {
|
|
2685
|
+
const e = readCarrier(thrown, reads);
|
|
2686
|
+
if (e instanceof TypeMismatchError) throw new TypeMismatchError(e.funcName, e.argIndex, e.expected, e.receivedSummary, pos, { source: ctx.source });
|
|
2687
|
+
if (e instanceof FailError) throw new FailError(e.message, pos, {
|
|
2688
|
+
source: ctx.source,
|
|
2689
|
+
cause: e.cause
|
|
2690
|
+
});
|
|
2691
|
+
if (e instanceof TemplateError) throw e;
|
|
2692
|
+
throw new EvalError(`error calling ${name}: ${errorText(e)}`, pos, {
|
|
2693
|
+
source: ctx.source,
|
|
2694
|
+
cause: e
|
|
2695
|
+
});
|
|
2214
2696
|
}
|
|
2697
|
+
if (fn.writes === true) ctx.decided(new Set([...reads, ...ctx.marks]));
|
|
2698
|
+
return result;
|
|
2215
2699
|
}
|
|
2216
|
-
evalPrimary(node, scope, ctx) {
|
|
2700
|
+
evalPrimary(node, scope, ctx, reads) {
|
|
2701
|
+
const value = this.evalOperand(node, scope, ctx, reads);
|
|
2702
|
+
if (value instanceof Marked) throw new EvalError("a marked() value reached the template from inside a func's return; return marked(...) itself, not a structure containing one", node.pos, { source: ctx.source });
|
|
2703
|
+
return value;
|
|
2704
|
+
}
|
|
2705
|
+
evalOperand(node, scope, ctx, reads) {
|
|
2217
2706
|
switch (node.type) {
|
|
2218
|
-
case "Dot": return scope
|
|
2707
|
+
case "Dot": return this.readDot(scope, reads);
|
|
2219
2708
|
case "Nil": return null;
|
|
2220
2709
|
case "Bool": return node.value;
|
|
2221
2710
|
case "Number": return numberValue(node);
|
|
2222
2711
|
case "String": return node.value;
|
|
2223
|
-
case "Field": return this.resolveFieldChain(scope
|
|
2224
|
-
case "Variable": return this.resolveVariable(node.idents, scope, node.pos, ctx);
|
|
2712
|
+
case "Field": return this.resolveFieldChain(this.readDot(scope, reads), node.idents, node.pos, ctx);
|
|
2713
|
+
case "Variable": return this.resolveVariable(node.idents, scope, node.pos, ctx, reads);
|
|
2225
2714
|
case "Identifier": {
|
|
2226
2715
|
const fn = this.funcs[node.ident];
|
|
2227
2716
|
if (!fn) throw new FuncNotFoundError(node.ident, node.pos, {
|
|
2228
2717
|
source: ctx.source,
|
|
2229
2718
|
available: Object.keys(this.funcs)
|
|
2230
2719
|
});
|
|
2231
|
-
|
|
2232
|
-
return fn.fn();
|
|
2720
|
+
return this.invoke(node.ident, fn, [], node.pos, ctx, reads);
|
|
2233
2721
|
}
|
|
2234
|
-
case "Chain": return this.resolveFieldChain(this.evalPrimary(node.node, scope, ctx), node.fields, node.pos, ctx);
|
|
2235
|
-
case "Pipe": return this.evalPipe(node, scope, ctx);
|
|
2722
|
+
case "Chain": return this.resolveFieldChain(this.evalPrimary(node.node, scope, ctx, reads), node.fields, node.pos, ctx);
|
|
2723
|
+
case "Pipe": return this.evalPipe(node, scope, ctx, reads);
|
|
2236
2724
|
default: throw new EvalError(`cannot evaluate ${node.type} as a value`, node.pos, { source: ctx.source });
|
|
2237
2725
|
}
|
|
2238
2726
|
}
|
|
@@ -2241,78 +2729,97 @@ var Engine = class {
|
|
|
2241
2729
|
if (result !== MISSING) return result;
|
|
2242
2730
|
if (this.missingKey === "error") throw new MissingFieldError(idents, pos, { source: ctx.source });
|
|
2243
2731
|
}
|
|
2244
|
-
resolveVariable(idents, scope, pos, ctx) {
|
|
2732
|
+
resolveVariable(idents, scope, pos, ctx, reads) {
|
|
2245
2733
|
const head = idents[0] ?? "$";
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
}
|
|
2250
|
-
const lookup = lookupVar(scope, head);
|
|
2251
|
-
if (!lookup.found) throw new EvalError(`undefined variable ${head}`, pos, { source: ctx.source });
|
|
2734
|
+
const binding = lookupVar(scope, head);
|
|
2735
|
+
if (binding === void 0) throw undefinedVariable(head, pos, ctx);
|
|
2736
|
+
for (const mark of binding.marks) reads.add(mark);
|
|
2252
2737
|
const tail = idents.slice(1);
|
|
2253
|
-
return tail.length === 0 ?
|
|
2738
|
+
return tail.length === 0 ? binding.value : this.resolveFieldChain(binding.value, tail, pos, ctx);
|
|
2254
2739
|
}
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
if (
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2740
|
+
readDot(scope, reads) {
|
|
2741
|
+
for (const mark of scope.dot.marks) reads.add(mark);
|
|
2742
|
+
return scope.dot.value;
|
|
2743
|
+
}
|
|
2744
|
+
lift(value, pos, source) {
|
|
2745
|
+
if (value === null || value === void 0) return this.fromString("<no value>");
|
|
2746
|
+
if (typeof value === "string") return this.fromString(value);
|
|
2747
|
+
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") return this.fromString(String(value));
|
|
2748
|
+
if (this.isT(value)) return value;
|
|
2749
|
+
try {
|
|
2750
|
+
return this.fromString(formatV(value, this.toString, this.isT));
|
|
2751
|
+
} catch (err) {
|
|
2752
|
+
throw new EvalError(err instanceof UnprintableError ? err.message : `cannot print: ${errorText(err)}`, pos, {
|
|
2753
|
+
source,
|
|
2754
|
+
cause: err
|
|
2755
|
+
});
|
|
2271
2756
|
}
|
|
2272
|
-
ctx.out.push(this.fromString(formatV(value, this.toString, this.isT)));
|
|
2273
2757
|
}
|
|
2274
2758
|
};
|
|
2275
|
-
const DEFAULT_IS_T = (v) => typeof v === "
|
|
2759
|
+
const DEFAULT_IS_T = (v) => typeof v === "string";
|
|
2276
2760
|
function createEngine(config) {
|
|
2277
2761
|
return new Engine(config);
|
|
2278
2762
|
}
|
|
2279
|
-
function enforceArgTypes(funcName,
|
|
2280
|
-
const
|
|
2763
|
+
function enforceArgTypes(funcName, spec, values, pos, src, toString = defaultToString, fromString = defaultFromString, isT = DEFAULT_IS_T) {
|
|
2764
|
+
const { argTypes, arity } = spec;
|
|
2765
|
+
const accepted = acceptedArgCount(argTypes, arity);
|
|
2766
|
+
if (values.length < accepted.minimum || values.length > accepted.maximum) throw new ArgCountError(funcName, accepted, values.length, pos, { source: src });
|
|
2767
|
+
const lookup = makeSlotLookup(argTypes, arity);
|
|
2281
2768
|
let firstOrdered = -1;
|
|
2282
2769
|
let firstComparable = -1;
|
|
2283
2770
|
for (let i = 0; i < values.length; i++) {
|
|
2284
2771
|
const declared = lookup(i);
|
|
2285
2772
|
const value = values[i];
|
|
2286
|
-
|
|
2773
|
+
const refusal = refusalOf(declared, value, toString, isT);
|
|
2774
|
+
if (refusal !== void 0) {
|
|
2775
|
+
if ((declared === "int" || declared === "truncating-int") && (typeof value === "number" || typeof value === "bigint")) throw new NotIntegerError(funcName, i + 1, value, pos, { source: src });
|
|
2776
|
+
throw new TypeMismatchError(funcName, i + 1, humanArgType(declared), refusal, pos, { source: src });
|
|
2777
|
+
}
|
|
2287
2778
|
if (declared === "liftable" && typeof value === "string") values[i] = fromString(value);
|
|
2288
|
-
if (declared === "int") values[i] = Math.trunc(Number(value));
|
|
2289
|
-
else if (declared === "float") values[i] = Number(value);
|
|
2779
|
+
if (declared === "truncating-int") values[i] = Math.trunc(Number(value));
|
|
2780
|
+
else if (declared === "int" || declared === "float") values[i] = Number(value);
|
|
2290
2781
|
if (declared === "ordered") {
|
|
2291
2782
|
if (firstOrdered === -1) firstOrdered = i;
|
|
2292
|
-
else if (!sameOrderedKind(values[firstOrdered], value)) throw new TypeMismatchError(funcName, i + 1, `${humanArgType("ordered")} of the same kind as ${
|
|
2783
|
+
else if (!sameOrderedKind(values[firstOrdered], value)) throw new TypeMismatchError(funcName, i + 1, `${humanArgType("ordered")} of the same kind as ${typeNameOf(values[firstOrdered])}`, typeNameOf(value), pos, { source: src });
|
|
2293
2784
|
}
|
|
2294
2785
|
if (declared === "comparable") {
|
|
2295
2786
|
if (firstComparable === -1) firstComparable = i;
|
|
2296
|
-
else if (!sameComparableKind(values[firstComparable], value)) throw new TypeMismatchError(funcName, i + 1, `${humanArgType("comparable")} of the same kind as ${
|
|
2787
|
+
else if (!sameComparableKind(values[firstComparable], value)) throw new TypeMismatchError(funcName, i + 1, `${humanArgType("comparable")} of the same kind as ${typeNameOf(values[firstComparable])}`, typeNameOf(value), pos, { source: src });
|
|
2297
2788
|
}
|
|
2298
2789
|
}
|
|
2299
2790
|
}
|
|
2300
|
-
function
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2791
|
+
function acceptedArgCount(argTypes, arity) {
|
|
2792
|
+
switch (arity.kind) {
|
|
2793
|
+
case "exact": return {
|
|
2794
|
+
minimum: argTypes.length,
|
|
2795
|
+
maximum: argTypes.length
|
|
2796
|
+
};
|
|
2797
|
+
case "variadic": return {
|
|
2798
|
+
minimum: argTypes.length - 1,
|
|
2799
|
+
maximum: Infinity
|
|
2800
|
+
};
|
|
2801
|
+
case "alternating": return {
|
|
2802
|
+
minimum: arity.minimum,
|
|
2803
|
+
maximum: Infinity
|
|
2804
|
+
};
|
|
2305
2805
|
}
|
|
2306
|
-
const trailing = argTypes[argTypes.length - 1];
|
|
2307
|
-
return (i) => i < argTypes.length ? argTypes[i] : trailing;
|
|
2308
2806
|
}
|
|
2309
|
-
function
|
|
2807
|
+
function makeSlotLookup(argTypes, arity) {
|
|
2808
|
+
const len = argTypes.length;
|
|
2809
|
+
switch (arity.kind) {
|
|
2810
|
+
case "exact": return (i) => argTypes[i];
|
|
2811
|
+
case "variadic": return (i) => argTypes[Math.min(i, len - 1)];
|
|
2812
|
+
case "alternating": return (i) => argTypes[i % len];
|
|
2813
|
+
}
|
|
2814
|
+
}
|
|
2815
|
+
function matchesArgType(declared, value, isT) {
|
|
2310
2816
|
switch (declared) {
|
|
2311
2817
|
case "truthy":
|
|
2312
2818
|
case "reflective":
|
|
2313
2819
|
case "value": return true;
|
|
2314
2820
|
case "string": return typeof value === "string";
|
|
2315
|
-
case "int": return typeof value === "number" && Number.
|
|
2821
|
+
case "int": return typeof value === "number" && Number.isSafeInteger(value) || typeof value === "bigint" && Number.isSafeInteger(Number(value));
|
|
2822
|
+
case "truncating-int": return typeof value === "number" && Number.isFinite(value) || typeof value === "bigint" && Number.isSafeInteger(Number(value));
|
|
2316
2823
|
case "float": return typeof value === "number" || typeof value === "bigint" && Number.isFinite(Number(value));
|
|
2317
2824
|
case "bool": return typeof value === "boolean";
|
|
2318
2825
|
case "ordered": return typeof value === "string" || typeof value === "number" || typeof value === "bigint" || typeof value === "boolean";
|
|
@@ -2325,14 +2832,6 @@ function matchesArgType(declared, value, toString, isT) {
|
|
|
2325
2832
|
case "collection": return typeof value === "string" || Array.isArray(value) || value instanceof Map || isPlainObject(value);
|
|
2326
2833
|
case "index-key": return typeof value === "string" || typeof value === "number" || typeof value === "bigint";
|
|
2327
2834
|
case "sliceable": return typeof value === "string" || Array.isArray(value);
|
|
2328
|
-
case "stringifiable":
|
|
2329
|
-
if (typeof value === "string") return true;
|
|
2330
|
-
try {
|
|
2331
|
-
toString(value);
|
|
2332
|
-
return true;
|
|
2333
|
-
} catch {
|
|
2334
|
-
return false;
|
|
2335
|
-
}
|
|
2336
2835
|
case "liftable": return typeof value === "string" || isT(value);
|
|
2337
2836
|
case "serializable": return isJsonSerializable(value);
|
|
2338
2837
|
default: throw new Error(`invalid ArgType: ${String(declared)}`);
|
|
@@ -2345,7 +2844,7 @@ function defaultToString(value) {
|
|
|
2345
2844
|
if (typeof value === "string") return value;
|
|
2346
2845
|
if (value === null || value === void 0) return "<nil>";
|
|
2347
2846
|
if (typeof value === "number" || typeof value === "bigint" || typeof value === "boolean") return String(value);
|
|
2348
|
-
throw new TypeMismatchError("<engine.toString>", 1, "string (or a consumer-supplied toString that flattens T)",
|
|
2847
|
+
throw new TypeMismatchError("<engine.toString>", 1, "string (or a consumer-supplied toString that flattens T)", typeNameOf(value), {
|
|
2349
2848
|
line: 0,
|
|
2350
2849
|
column: 0,
|
|
2351
2850
|
offset: 0
|
|
@@ -2382,15 +2881,16 @@ function comparableKind(v) {
|
|
|
2382
2881
|
}
|
|
2383
2882
|
function humanArgType(t) {
|
|
2384
2883
|
switch (t) {
|
|
2385
|
-
case "int": return "integer (
|
|
2884
|
+
case "int": return "integer (safe-integer number or bigint)";
|
|
2885
|
+
case "truncating-int": return "integer (finite number, truncated toward zero, or safe-integer bigint)";
|
|
2386
2886
|
case "float": return "float (number, including NaN/Infinity, or finite-convertible bigint)";
|
|
2387
|
-
case "T": return "T (
|
|
2887
|
+
case "T": return "T (a value the engine's isT accepts)";
|
|
2388
2888
|
case "ordered": return "orderable primitive";
|
|
2389
2889
|
case "list": return "list";
|
|
2390
2890
|
case "dict": return "dict (plain object)";
|
|
2391
2891
|
case "sized": return "sized value (string, list, map, set, or dict)";
|
|
2392
2892
|
case "comparable": return "comparable value (orderable primitive, nil, list, dict, Map, or Set)";
|
|
2393
|
-
case "stringifiable": return "stringifiable value (string
|
|
2893
|
+
case "stringifiable": return "stringifiable value (string, nil, scalar, a T the engine's toString flattens, an object with its own toString, a class instance, or a list/map of those; not a Set, function or symbol)";
|
|
2394
2894
|
case "liftable": return "liftable value (T or string, the latter lifted via the engine's fromString)";
|
|
2395
2895
|
case "callable": return "callable (function value)";
|
|
2396
2896
|
case "collection": return "collection (string, array, Map, or dict)";
|
|
@@ -2405,12 +2905,30 @@ function humanArgType(t) {
|
|
|
2405
2905
|
default: throw new Error(`invalid ArgType: ${String(t)}`);
|
|
2406
2906
|
}
|
|
2407
2907
|
}
|
|
2408
|
-
function
|
|
2409
|
-
|
|
2410
|
-
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2908
|
+
function constructFrame(scope) {
|
|
2909
|
+
return pushScope(scope, scope.dot);
|
|
2910
|
+
}
|
|
2911
|
+
function undefinedVariable(name, pos, ctx) {
|
|
2912
|
+
return new EvalError(`undefined variable: ${name}`, pos, { source: ctx.source });
|
|
2913
|
+
}
|
|
2914
|
+
function innermost(e) {
|
|
2915
|
+
return e.cause instanceof TemplateError ? innermost(e.cause) : e;
|
|
2916
|
+
}
|
|
2917
|
+
function errorText(e) {
|
|
2918
|
+
return e instanceof Error ? e.message : String(e);
|
|
2919
|
+
}
|
|
2920
|
+
function refusalOf(declared, value, toString, isT) {
|
|
2921
|
+
if (declared === "stringifiable") return printRefusal(value, toString, isT)?.summary;
|
|
2922
|
+
return matchesArgType(declared, value, isT) ? void 0 : typeNameOf(value);
|
|
2923
|
+
}
|
|
2924
|
+
function printRefusal(value, toString, isT) {
|
|
2925
|
+
try {
|
|
2926
|
+
formatV(value, toString, isT);
|
|
2927
|
+
return;
|
|
2928
|
+
} catch (err) {
|
|
2929
|
+
if (err instanceof UnprintableError) return err;
|
|
2930
|
+
throw err;
|
|
2931
|
+
}
|
|
2414
2932
|
}
|
|
2415
2933
|
/**
|
|
2416
2934
|
* Produce [key, value] entries for a `range` operand.
|
|
@@ -2642,30 +3160,36 @@ function sprigConversions() {
|
|
|
2642
3160
|
atoi: {
|
|
2643
3161
|
fn: (s) => atoi(s),
|
|
2644
3162
|
argTypes: ["string"],
|
|
3163
|
+
arity: { kind: "exact" },
|
|
2645
3164
|
returnType: "int"
|
|
2646
3165
|
},
|
|
2647
3166
|
int: {
|
|
2648
3167
|
fn: (v) => int(v),
|
|
2649
3168
|
argTypes: ["value"],
|
|
3169
|
+
arity: { kind: "exact" },
|
|
2650
3170
|
returnType: "int"
|
|
2651
3171
|
},
|
|
2652
3172
|
int64: {
|
|
2653
3173
|
fn: (v) => int64(v),
|
|
2654
|
-
argTypes: ["value"]
|
|
3174
|
+
argTypes: ["value"],
|
|
3175
|
+
arity: { kind: "exact" }
|
|
2655
3176
|
},
|
|
2656
3177
|
float64: {
|
|
2657
3178
|
fn: (v) => float64(v),
|
|
2658
3179
|
argTypes: ["value"],
|
|
3180
|
+
arity: { kind: "exact" },
|
|
2659
3181
|
returnType: "float"
|
|
2660
3182
|
},
|
|
2661
3183
|
toDecimal: {
|
|
2662
3184
|
fn: (s) => toDecimal(s),
|
|
2663
3185
|
argTypes: ["string"],
|
|
3186
|
+
arity: { kind: "exact" },
|
|
2664
3187
|
returnType: "int"
|
|
2665
3188
|
},
|
|
2666
3189
|
toRawJson: {
|
|
2667
3190
|
fn: (v) => toRawJson(v),
|
|
2668
3191
|
argTypes: ["serializable"],
|
|
3192
|
+
arity: { kind: "exact" },
|
|
2669
3193
|
returnType: "string"
|
|
2670
3194
|
}
|
|
2671
3195
|
};
|
|
@@ -2866,33 +3390,41 @@ function pad3(n) {
|
|
|
2866
3390
|
function spacePad2(n) {
|
|
2867
3391
|
return n < 10 ? ` ${n}` : String(n);
|
|
2868
3392
|
}
|
|
3393
|
+
function offsetParts(offsetSeconds) {
|
|
3394
|
+
const minutes = Math.trunc(offsetSeconds / 60);
|
|
3395
|
+
const abs = Math.abs(minutes);
|
|
3396
|
+
return {
|
|
3397
|
+
sign: minutes < 0 ? "-" : "+",
|
|
3398
|
+
hh: pad2(Math.floor(abs / 60)),
|
|
3399
|
+
mm: pad2(abs % 60)
|
|
3400
|
+
};
|
|
3401
|
+
}
|
|
2869
3402
|
/** ±hhmm (no colon). E.g. "+0500", "-0500", "+0000". */
|
|
2870
|
-
function fmtOffset(
|
|
2871
|
-
const sign
|
|
2872
|
-
|
|
2873
|
-
return `${sign}${pad2(Math.floor(abs / 60))}${pad2(abs % 60)}`;
|
|
3403
|
+
function fmtOffset(offsetSeconds) {
|
|
3404
|
+
const { sign, hh, mm } = offsetParts(offsetSeconds);
|
|
3405
|
+
return `${sign}${hh}${mm}`;
|
|
2874
3406
|
}
|
|
2875
3407
|
/** ±hh:mm (with colon). E.g. "+05:00", "-05:00", "+00:00". */
|
|
2876
|
-
function fmtOffsetColon(
|
|
2877
|
-
const sign
|
|
2878
|
-
|
|
2879
|
-
return `${sign}${pad2(Math.floor(abs / 60))}:${pad2(abs % 60)}`;
|
|
3408
|
+
function fmtOffsetColon(offsetSeconds) {
|
|
3409
|
+
const { sign, hh, mm } = offsetParts(offsetSeconds);
|
|
3410
|
+
return `${sign}${hh}:${mm}`;
|
|
2880
3411
|
}
|
|
2881
3412
|
/** ±hh only. */
|
|
2882
|
-
function fmtOffsetHour(
|
|
2883
|
-
|
|
3413
|
+
function fmtOffsetHour(offsetSeconds) {
|
|
3414
|
+
const { sign, hh } = offsetParts(offsetSeconds);
|
|
3415
|
+
return `${sign}${hh}`;
|
|
2884
3416
|
}
|
|
2885
3417
|
/** Z or ±hhmm. */
|
|
2886
|
-
function fmtOffsetZ(
|
|
2887
|
-
return
|
|
3418
|
+
function fmtOffsetZ(offsetSeconds) {
|
|
3419
|
+
return offsetSeconds === 0 ? "Z" : fmtOffset(offsetSeconds);
|
|
2888
3420
|
}
|
|
2889
3421
|
/** Z or ±hh:mm. */
|
|
2890
|
-
function fmtOffsetColonZ(
|
|
2891
|
-
return
|
|
3422
|
+
function fmtOffsetColonZ(offsetSeconds) {
|
|
3423
|
+
return offsetSeconds === 0 ? "Z" : fmtOffsetColon(offsetSeconds);
|
|
2892
3424
|
}
|
|
2893
3425
|
/** Z or ±hh. */
|
|
2894
|
-
function fmtOffsetHourZ(
|
|
2895
|
-
return
|
|
3426
|
+
function fmtOffsetHourZ(offsetSeconds) {
|
|
3427
|
+
return offsetSeconds === 0 ? "Z" : fmtOffsetHour(offsetSeconds);
|
|
2896
3428
|
}
|
|
2897
3429
|
function fmtFrac(ms, width, trimTrailing) {
|
|
2898
3430
|
const nanosTotal = ms * 1e6;
|
|
@@ -2918,16 +3450,16 @@ const TOKENS$1 = [
|
|
|
2918
3450
|
}],
|
|
2919
3451
|
["January", (z) => MONTHS_LONG$1[z.month - 1] ?? "January"],
|
|
2920
3452
|
["Monday", (z) => WEEKDAYS_LONG[z.weekday] ?? "Monday"],
|
|
2921
|
-
["Z07:00", (z) => fmtOffsetColonZ(z.
|
|
2922
|
-
["-07:00", (z) => fmtOffsetColon(z.
|
|
2923
|
-
["Z0700", (z) => fmtOffsetZ(z.
|
|
2924
|
-
["-0700", (z) => fmtOffset(z.
|
|
2925
|
-
["2006", (z) => String(z.year)],
|
|
3453
|
+
["Z07:00", (z) => fmtOffsetColonZ(z.offsetSeconds)],
|
|
3454
|
+
["-07:00", (z) => fmtOffsetColon(z.offsetSeconds)],
|
|
3455
|
+
["Z0700", (z) => fmtOffsetZ(z.offsetSeconds)],
|
|
3456
|
+
["-0700", (z) => fmtOffset(z.offsetSeconds)],
|
|
3457
|
+
["2006", (z) => `${z.year < 0 ? "-" : ""}${String(Math.abs(z.year)).padStart(4, "0")}`],
|
|
2926
3458
|
["Jan", (z) => MONTHS_SHORT$1[z.month - 1] ?? "Jan"],
|
|
2927
3459
|
["Mon", (z) => WEEKDAYS_SHORT[z.weekday] ?? "Mon"],
|
|
2928
3460
|
["MST", (z) => z.tzAbbr],
|
|
2929
|
-
["-07", (z) => fmtOffsetHour(z.
|
|
2930
|
-
["Z07", (z) => fmtOffsetHourZ(z.
|
|
3461
|
+
["-07", (z) => fmtOffsetHour(z.offsetSeconds)],
|
|
3462
|
+
["Z07", (z) => fmtOffsetHourZ(z.offsetSeconds)],
|
|
2931
3463
|
["15", (z) => pad2(z.hour)],
|
|
2932
3464
|
["01", (z) => pad2(z.month)],
|
|
2933
3465
|
["02", (z) => pad2(z.day)],
|
|
@@ -2935,7 +3467,7 @@ const TOKENS$1 = [
|
|
|
2935
3467
|
["03", (z) => pad2(z.hour12)],
|
|
2936
3468
|
["04", (z) => pad2(z.minute)],
|
|
2937
3469
|
["05", (z) => pad2(z.second)],
|
|
2938
|
-
["06", (z) => pad2(z.year % 100)],
|
|
3470
|
+
["06", (z) => pad2(Math.abs(z.year) % 100)],
|
|
2939
3471
|
["PM", (z) => z.hour >= 12 ? "PM" : "AM"],
|
|
2940
3472
|
["pm", (z) => z.hour >= 12 ? "pm" : "am"],
|
|
2941
3473
|
["1", (z) => String(z.month)],
|
|
@@ -2966,6 +3498,7 @@ function formatGoLayout(layout, z) {
|
|
|
2966
3498
|
//#endregion
|
|
2967
3499
|
//#region src/sprig/datetime/_zone.ts
|
|
2968
3500
|
const INTL_OPTS = {
|
|
3501
|
+
era: "short",
|
|
2969
3502
|
year: "numeric",
|
|
2970
3503
|
month: "2-digit",
|
|
2971
3504
|
day: "2-digit",
|
|
@@ -2973,13 +3506,9 @@ const INTL_OPTS = {
|
|
|
2973
3506
|
hour: "2-digit",
|
|
2974
3507
|
minute: "2-digit",
|
|
2975
3508
|
second: "2-digit",
|
|
2976
|
-
hour12: false
|
|
3509
|
+
hour12: false,
|
|
3510
|
+
timeZoneName: "short"
|
|
2977
3511
|
};
|
|
2978
|
-
function indexParts(parts) {
|
|
2979
|
-
const out = /* @__PURE__ */ new Map();
|
|
2980
|
-
for (const p of parts) out.set(p.type, p.value);
|
|
2981
|
-
return out;
|
|
2982
|
-
}
|
|
2983
3512
|
const WEEKDAY_NAMES = [
|
|
2984
3513
|
"Sunday",
|
|
2985
3514
|
"Monday",
|
|
@@ -2989,56 +3518,87 @@ const WEEKDAY_NAMES = [
|
|
|
2989
3518
|
"Friday",
|
|
2990
3519
|
"Saturday"
|
|
2991
3520
|
];
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
3521
|
+
function readParts(fmt, date) {
|
|
3522
|
+
const parts = /* @__PURE__ */ new Map();
|
|
3523
|
+
for (const p of fmt.formatToParts(date)) parts.set(p.type, p.value);
|
|
3524
|
+
return (type) => {
|
|
3525
|
+
const value = parts.get(type);
|
|
3526
|
+
if (value === void 0) throw new Error(`Intl.DateTimeFormat omitted the ${type} part`);
|
|
3527
|
+
return value;
|
|
3528
|
+
};
|
|
3529
|
+
}
|
|
3530
|
+
function buildFormatter(timeZone) {
|
|
3531
|
+
return new Intl.DateTimeFormat("en-US", {
|
|
3532
|
+
...INTL_OPTS,
|
|
3533
|
+
timeZone
|
|
3534
|
+
});
|
|
3535
|
+
}
|
|
3536
|
+
const OFFSET_ZONE = /^[+-]/;
|
|
3537
|
+
const formattersByZone = /* @__PURE__ */ new Map();
|
|
3538
|
+
function zoneFormatter(name) {
|
|
3539
|
+
const key = name.replace(/[A-Z]/g, (c) => c.toLowerCase());
|
|
3540
|
+
const cached = formattersByZone.get(key);
|
|
3541
|
+
if (cached !== void 0) return cached;
|
|
3542
|
+
const loaded = OFFSET_ZONE.test(name) ? void 0 : buildOrUnknown(name);
|
|
3543
|
+
if (loaded === void 0) return zoneFormatter("UTC");
|
|
3544
|
+
formattersByZone.set(key, loaded);
|
|
3545
|
+
return loaded;
|
|
3546
|
+
}
|
|
3547
|
+
function buildOrUnknown(name) {
|
|
3548
|
+
if (name === "UTC") return buildFormatter(name);
|
|
2995
3549
|
try {
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
|
|
3550
|
+
return buildFormatter(name);
|
|
3551
|
+
} catch (e) {
|
|
3552
|
+
if (e instanceof RangeError) return void 0;
|
|
3553
|
+
throw e;
|
|
3000
3554
|
}
|
|
3001
|
-
|
|
3002
|
-
|
|
3003
|
-
|
|
3004
|
-
|
|
3005
|
-
const
|
|
3006
|
-
const
|
|
3007
|
-
|
|
3008
|
-
|
|
3009
|
-
|
|
3010
|
-
const
|
|
3011
|
-
|
|
3012
|
-
|
|
3013
|
-
|
|
3014
|
-
const
|
|
3555
|
+
}
|
|
3556
|
+
/** Extract date parts in the requested IANA timezone. Falls back to "UTC" on invalid zone, mirroring Go sprig. */
|
|
3557
|
+
function getZoneParts(date, zone) {
|
|
3558
|
+
const part = readParts(zoneFormatter(zone), date);
|
|
3559
|
+
const eraYear = parseInt(part("year"), 10);
|
|
3560
|
+
const year = part("era") === "BC" ? 1 - eraYear : eraYear;
|
|
3561
|
+
const month = parseInt(part("month"), 10);
|
|
3562
|
+
const day = parseInt(part("day"), 10);
|
|
3563
|
+
const hour = parseInt(part("hour"), 10) % 24;
|
|
3564
|
+
const minute = parseInt(part("minute"), 10);
|
|
3565
|
+
const second = parseInt(part("second"), 10);
|
|
3566
|
+
const ms = date.getUTCMilliseconds();
|
|
3567
|
+
const offsetSeconds = (daysFromCivil(year, month, day) * 864e5 + ((hour * 60 + minute) * 60 + second) * 1e3 + ms - date.getTime()) / 1e3;
|
|
3568
|
+
const weekdayName = part("weekday");
|
|
3569
|
+
const weekday = WEEKDAY_NAMES.indexOf(weekdayName);
|
|
3570
|
+
if (weekday === -1) throw new Error(`Intl.DateTimeFormat gave an unknown weekday ${weekdayName}`);
|
|
3015
3571
|
return {
|
|
3016
|
-
year
|
|
3017
|
-
month
|
|
3018
|
-
day
|
|
3019
|
-
weekday
|
|
3572
|
+
year,
|
|
3573
|
+
month,
|
|
3574
|
+
day,
|
|
3575
|
+
weekday,
|
|
3020
3576
|
hour,
|
|
3021
3577
|
hour12: hour % 12 || 12,
|
|
3022
|
-
minute
|
|
3023
|
-
second
|
|
3024
|
-
ms
|
|
3025
|
-
tzAbbr,
|
|
3026
|
-
|
|
3578
|
+
minute,
|
|
3579
|
+
second,
|
|
3580
|
+
ms,
|
|
3581
|
+
tzAbbr: part("timeZoneName"),
|
|
3582
|
+
offsetSeconds
|
|
3027
3583
|
};
|
|
3028
3584
|
}
|
|
3029
|
-
|
|
3030
|
-
|
|
3031
|
-
|
|
3032
|
-
const
|
|
3033
|
-
|
|
3034
|
-
const
|
|
3035
|
-
|
|
3036
|
-
const min = parseInt(m[3] ?? "0", 10);
|
|
3037
|
-
return sign * (h * 60 + min);
|
|
3585
|
+
function daysFromCivil(year, month, day) {
|
|
3586
|
+
const y = month <= 2 ? year - 1 : year;
|
|
3587
|
+
const era = Math.floor(y / 400);
|
|
3588
|
+
const yearOfEra = y - era * 400;
|
|
3589
|
+
const dayOfYear = Math.floor((153 * ((month + 9) % 12) + 2) / 5) + day - 1;
|
|
3590
|
+
const dayOfEra = yearOfEra * 365 + Math.floor(yearOfEra / 4) - Math.floor(yearOfEra / 100) + dayOfYear;
|
|
3591
|
+
return era * 146097 + dayOfEra - 719468;
|
|
3038
3592
|
}
|
|
3593
|
+
let hostZone;
|
|
3039
3594
|
/** Host timezone name (IANA string). */
|
|
3040
3595
|
function localTz() {
|
|
3041
|
-
|
|
3596
|
+
const key = `${globalThis.process?.env.TZ}|${(/* @__PURE__ */ new Date()).getTimezoneOffset()}`;
|
|
3597
|
+
if (hostZone?.key !== key) hostZone = {
|
|
3598
|
+
key,
|
|
3599
|
+
zone: Intl.DateTimeFormat().resolvedOptions().timeZone
|
|
3600
|
+
};
|
|
3601
|
+
return hostZone.zone;
|
|
3042
3602
|
}
|
|
3043
3603
|
//#endregion
|
|
3044
3604
|
//#region src/sprig/datetime/date.ts
|
|
@@ -3123,15 +3683,13 @@ function durationRound(d) {
|
|
|
3123
3683
|
//#region src/sprig/datetime/htmlDate.ts
|
|
3124
3684
|
/** `htmlDate t` — YYYY-MM-DD in the host timezone. */
|
|
3125
3685
|
function htmlDate(t) {
|
|
3126
|
-
|
|
3127
|
-
return `${z.year}-${String(z.month).padStart(2, "0")}-${String(z.day).padStart(2, "0")}`;
|
|
3686
|
+
return formatGoLayout("2006-01-02", getZoneParts(resolveDate(t), localTz()));
|
|
3128
3687
|
}
|
|
3129
3688
|
//#endregion
|
|
3130
3689
|
//#region src/sprig/datetime/htmlDateInZone.ts
|
|
3131
3690
|
/** `htmlDateInZone t zone` — YYYY-MM-DD in the named timezone. */
|
|
3132
3691
|
function htmlDateInZone(t, zone) {
|
|
3133
|
-
|
|
3134
|
-
return `${z.year}-${String(z.month).padStart(2, "0")}-${String(z.day).padStart(2, "0")}`;
|
|
3692
|
+
return formatGoLayout("2006-01-02", getZoneParts(resolveDate(t), zone));
|
|
3135
3693
|
}
|
|
3136
3694
|
//#endregion
|
|
3137
3695
|
//#region src/sprig/datetime/now.ts
|
|
@@ -3557,11 +4115,13 @@ function sprigDatetime(clock = () => /* @__PURE__ */ new Date()) {
|
|
|
3557
4115
|
return {
|
|
3558
4116
|
now: {
|
|
3559
4117
|
fn: () => now(clock),
|
|
3560
|
-
argTypes: []
|
|
4118
|
+
argTypes: [],
|
|
4119
|
+
arity: { kind: "exact" }
|
|
3561
4120
|
},
|
|
3562
4121
|
date: {
|
|
3563
4122
|
fn: (format, t) => date(format, t),
|
|
3564
|
-
argTypes: ["string", "value"]
|
|
4123
|
+
argTypes: ["string", "value"],
|
|
4124
|
+
arity: { kind: "exact" }
|
|
3565
4125
|
},
|
|
3566
4126
|
dateInZone: {
|
|
3567
4127
|
fn: (format, t, zone) => dateInZone(format, t, zone),
|
|
@@ -3569,39 +4129,48 @@ function sprigDatetime(clock = () => /* @__PURE__ */ new Date()) {
|
|
|
3569
4129
|
"string",
|
|
3570
4130
|
"value",
|
|
3571
4131
|
"string"
|
|
3572
|
-
]
|
|
4132
|
+
],
|
|
4133
|
+
arity: { kind: "exact" }
|
|
3573
4134
|
},
|
|
3574
4135
|
dateModify: {
|
|
3575
4136
|
fn: (dur, t) => dateModify(dur, t),
|
|
3576
|
-
argTypes: ["string", "value"]
|
|
4137
|
+
argTypes: ["string", "value"],
|
|
4138
|
+
arity: { kind: "exact" }
|
|
3577
4139
|
},
|
|
3578
4140
|
htmlDate: {
|
|
3579
4141
|
fn: (t) => htmlDate(t),
|
|
3580
|
-
argTypes: ["value"]
|
|
4142
|
+
argTypes: ["value"],
|
|
4143
|
+
arity: { kind: "exact" }
|
|
3581
4144
|
},
|
|
3582
4145
|
htmlDateInZone: {
|
|
3583
4146
|
fn: (t, zone) => htmlDateInZone(t, zone),
|
|
3584
|
-
argTypes: ["value", "string"]
|
|
4147
|
+
argTypes: ["value", "string"],
|
|
4148
|
+
arity: { kind: "exact" }
|
|
3585
4149
|
},
|
|
3586
4150
|
duration: {
|
|
3587
4151
|
fn: (sec) => duration(sec),
|
|
3588
|
-
argTypes: ["value"]
|
|
4152
|
+
argTypes: ["value"],
|
|
4153
|
+
arity: { kind: "exact" }
|
|
3589
4154
|
},
|
|
3590
4155
|
durationRound: {
|
|
3591
4156
|
fn: (d) => durationRound(d),
|
|
3592
|
-
argTypes: ["value"]
|
|
4157
|
+
argTypes: ["value"],
|
|
4158
|
+
arity: { kind: "exact" }
|
|
3593
4159
|
},
|
|
3594
4160
|
toDate: {
|
|
3595
4161
|
fn: (format, s) => toDate(format, s),
|
|
3596
|
-
argTypes: ["string", "string"]
|
|
4162
|
+
argTypes: ["string", "string"],
|
|
4163
|
+
arity: { kind: "exact" }
|
|
3597
4164
|
},
|
|
3598
4165
|
ago: {
|
|
3599
4166
|
fn: (t) => ago(t, clock),
|
|
3600
|
-
argTypes: ["value"]
|
|
4167
|
+
argTypes: ["value"],
|
|
4168
|
+
arity: { kind: "exact" }
|
|
3601
4169
|
},
|
|
3602
4170
|
unixEpoch: {
|
|
3603
4171
|
fn: (t) => unixEpoch(t),
|
|
3604
|
-
argTypes: ["value"]
|
|
4172
|
+
argTypes: ["value"],
|
|
4173
|
+
arity: { kind: "exact" }
|
|
3605
4174
|
}
|
|
3606
4175
|
};
|
|
3607
4176
|
}
|
|
@@ -3700,15 +4269,18 @@ function sprigDefaults() {
|
|
|
3700
4269
|
return {
|
|
3701
4270
|
default: {
|
|
3702
4271
|
fn: (a, b) => defaultFn(a, b),
|
|
3703
|
-
argTypes: ["value", "truthy"]
|
|
4272
|
+
argTypes: ["value", "truthy"],
|
|
4273
|
+
arity: { kind: "variadic" }
|
|
3704
4274
|
},
|
|
3705
4275
|
empty: {
|
|
3706
4276
|
fn: (v) => empty(v),
|
|
3707
|
-
argTypes: ["truthy"]
|
|
4277
|
+
argTypes: ["truthy"],
|
|
4278
|
+
arity: { kind: "exact" }
|
|
3708
4279
|
},
|
|
3709
4280
|
coalesce: {
|
|
3710
4281
|
fn: (...vs) => coalesce(...vs),
|
|
3711
|
-
argTypes: ["truthy"]
|
|
4282
|
+
argTypes: ["truthy"],
|
|
4283
|
+
arity: { kind: "variadic" }
|
|
3712
4284
|
},
|
|
3713
4285
|
ternary: {
|
|
3714
4286
|
fn: (a, b, c) => ternary(a, b, c),
|
|
@@ -3716,20 +4288,24 @@ function sprigDefaults() {
|
|
|
3716
4288
|
"value",
|
|
3717
4289
|
"value",
|
|
3718
4290
|
"truthy"
|
|
3719
|
-
]
|
|
4291
|
+
],
|
|
4292
|
+
arity: { kind: "exact" }
|
|
3720
4293
|
},
|
|
3721
4294
|
fromJson: {
|
|
3722
4295
|
fn: (s) => fromJson(s),
|
|
3723
|
-
argTypes: ["string"]
|
|
4296
|
+
argTypes: ["string"],
|
|
4297
|
+
arity: { kind: "exact" }
|
|
3724
4298
|
},
|
|
3725
4299
|
toJson: {
|
|
3726
4300
|
fn: (v) => toJson(v),
|
|
3727
4301
|
argTypes: ["serializable"],
|
|
4302
|
+
arity: { kind: "exact" },
|
|
3728
4303
|
returnType: "string"
|
|
3729
4304
|
},
|
|
3730
4305
|
toPrettyJson: {
|
|
3731
4306
|
fn: (v) => toPrettyJson(v),
|
|
3732
4307
|
argTypes: ["serializable"],
|
|
4308
|
+
arity: { kind: "exact" },
|
|
3733
4309
|
returnType: "string"
|
|
3734
4310
|
}
|
|
3735
4311
|
};
|
|
@@ -3919,11 +4495,15 @@ function sprigDicts() {
|
|
|
3919
4495
|
dict: {
|
|
3920
4496
|
fn: (...kv) => dict(...kv),
|
|
3921
4497
|
argTypes: ["string", "value"],
|
|
3922
|
-
|
|
4498
|
+
arity: {
|
|
4499
|
+
kind: "alternating",
|
|
4500
|
+
minimum: 0
|
|
4501
|
+
}
|
|
3923
4502
|
},
|
|
3924
4503
|
get: {
|
|
3925
4504
|
fn: (d, k) => get(d, k),
|
|
3926
|
-
argTypes: ["dict", "string"]
|
|
4505
|
+
argTypes: ["dict", "string"],
|
|
4506
|
+
arity: { kind: "exact" }
|
|
3927
4507
|
},
|
|
3928
4508
|
set: {
|
|
3929
4509
|
fn: (d, k, v) => set(d, k, v),
|
|
@@ -3931,43 +4511,55 @@ function sprigDicts() {
|
|
|
3931
4511
|
"dict",
|
|
3932
4512
|
"string",
|
|
3933
4513
|
"value"
|
|
3934
|
-
]
|
|
4514
|
+
],
|
|
4515
|
+
arity: { kind: "exact" },
|
|
4516
|
+
writes: true
|
|
3935
4517
|
},
|
|
3936
4518
|
unset: {
|
|
3937
4519
|
fn: (d, k) => unset(d, k),
|
|
3938
|
-
argTypes: ["dict", "string"]
|
|
4520
|
+
argTypes: ["dict", "string"],
|
|
4521
|
+
arity: { kind: "exact" },
|
|
4522
|
+
writes: true
|
|
3939
4523
|
},
|
|
3940
4524
|
keys: {
|
|
3941
4525
|
fn: (d) => keys(d),
|
|
3942
|
-
argTypes: ["dict"]
|
|
4526
|
+
argTypes: ["dict"],
|
|
4527
|
+
arity: { kind: "variadic" }
|
|
3943
4528
|
},
|
|
3944
4529
|
values: {
|
|
3945
4530
|
fn: (d) => values(d),
|
|
3946
|
-
argTypes: ["dict"]
|
|
4531
|
+
argTypes: ["dict"],
|
|
4532
|
+
arity: { kind: "exact" }
|
|
3947
4533
|
},
|
|
3948
4534
|
pluck: {
|
|
3949
4535
|
fn: (k, ...d) => pluck(k, ...d),
|
|
3950
|
-
argTypes: ["string", "dict"]
|
|
4536
|
+
argTypes: ["string", "dict"],
|
|
4537
|
+
arity: { kind: "variadic" }
|
|
3951
4538
|
},
|
|
3952
4539
|
pick: {
|
|
3953
4540
|
fn: (d, ...k) => pick(d, ...k),
|
|
3954
|
-
argTypes: ["dict", "string"]
|
|
4541
|
+
argTypes: ["dict", "string"],
|
|
4542
|
+
arity: { kind: "variadic" }
|
|
3955
4543
|
},
|
|
3956
4544
|
omit: {
|
|
3957
4545
|
fn: (d, ...k) => omit(d, ...k),
|
|
3958
|
-
argTypes: ["dict", "string"]
|
|
4546
|
+
argTypes: ["dict", "string"],
|
|
4547
|
+
arity: { kind: "variadic" }
|
|
3959
4548
|
},
|
|
3960
4549
|
hasKey: {
|
|
3961
4550
|
fn: (d, k) => hasKey(d, k),
|
|
3962
|
-
argTypes: ["dict", "string"]
|
|
4551
|
+
argTypes: ["dict", "string"],
|
|
4552
|
+
arity: { kind: "exact" }
|
|
3963
4553
|
},
|
|
3964
4554
|
merge: {
|
|
3965
4555
|
fn: (d, ...s) => merge(d, ...s),
|
|
3966
|
-
argTypes: ["dict"]
|
|
4556
|
+
argTypes: ["dict", "dict"],
|
|
4557
|
+
arity: { kind: "variadic" }
|
|
3967
4558
|
},
|
|
3968
4559
|
mergeOverwrite: {
|
|
3969
4560
|
fn: (d, ...s) => mergeOverwrite(d, ...s),
|
|
3970
|
-
argTypes: ["dict"]
|
|
4561
|
+
argTypes: ["dict", "dict"],
|
|
4562
|
+
arity: { kind: "variadic" }
|
|
3971
4563
|
}
|
|
3972
4564
|
};
|
|
3973
4565
|
}
|
|
@@ -3992,7 +4584,8 @@ function fail(msg) {
|
|
|
3992
4584
|
function sprigFlow() {
|
|
3993
4585
|
return { fail: {
|
|
3994
4586
|
fn: (msg) => fail(msg),
|
|
3995
|
-
argTypes: ["string"]
|
|
4587
|
+
argTypes: ["string"],
|
|
4588
|
+
arity: { kind: "exact" }
|
|
3996
4589
|
} };
|
|
3997
4590
|
}
|
|
3998
4591
|
//#endregion
|
|
@@ -4124,39 +4717,48 @@ function sprigHash() {
|
|
|
4124
4717
|
return {
|
|
4125
4718
|
b64enc: {
|
|
4126
4719
|
fn: (s) => b64enc(s),
|
|
4127
|
-
argTypes: ["string"]
|
|
4720
|
+
argTypes: ["string"],
|
|
4721
|
+
arity: { kind: "exact" }
|
|
4128
4722
|
},
|
|
4129
4723
|
b64dec: {
|
|
4130
4724
|
fn: (s) => b64dec(s),
|
|
4131
|
-
argTypes: ["string"]
|
|
4725
|
+
argTypes: ["string"],
|
|
4726
|
+
arity: { kind: "exact" }
|
|
4132
4727
|
},
|
|
4133
4728
|
b32enc: {
|
|
4134
4729
|
fn: (s) => b32enc(s),
|
|
4135
|
-
argTypes: ["string"]
|
|
4730
|
+
argTypes: ["string"],
|
|
4731
|
+
arity: { kind: "exact" }
|
|
4136
4732
|
},
|
|
4137
4733
|
b32dec: {
|
|
4138
4734
|
fn: (s) => b32dec(s),
|
|
4139
|
-
argTypes: ["string"]
|
|
4735
|
+
argTypes: ["string"],
|
|
4736
|
+
arity: { kind: "exact" }
|
|
4140
4737
|
},
|
|
4141
4738
|
sha1sum: {
|
|
4142
4739
|
fn: (s) => sha1sum(s),
|
|
4143
|
-
argTypes: ["string"]
|
|
4740
|
+
argTypes: ["string"],
|
|
4741
|
+
arity: { kind: "exact" }
|
|
4144
4742
|
},
|
|
4145
4743
|
sha256sum: {
|
|
4146
4744
|
fn: (s) => sha256sum(s),
|
|
4147
|
-
argTypes: ["string"]
|
|
4745
|
+
argTypes: ["string"],
|
|
4746
|
+
arity: { kind: "exact" }
|
|
4148
4747
|
},
|
|
4149
4748
|
sha512sum: {
|
|
4150
4749
|
fn: (s) => sha512sum(s),
|
|
4151
|
-
argTypes: ["string"]
|
|
4750
|
+
argTypes: ["string"],
|
|
4751
|
+
arity: { kind: "exact" }
|
|
4152
4752
|
},
|
|
4153
4753
|
adler32sum: {
|
|
4154
4754
|
fn: (s) => adler32sum(s),
|
|
4155
|
-
argTypes: ["string"]
|
|
4755
|
+
argTypes: ["string"],
|
|
4756
|
+
arity: { kind: "exact" }
|
|
4156
4757
|
},
|
|
4157
4758
|
uuidv4: {
|
|
4158
4759
|
fn: () => uuidv4(),
|
|
4159
|
-
argTypes: []
|
|
4760
|
+
argTypes: [],
|
|
4761
|
+
arity: { kind: "exact" }
|
|
4160
4762
|
}
|
|
4161
4763
|
};
|
|
4162
4764
|
}
|
|
@@ -4271,8 +4873,8 @@ function dig(...args) {
|
|
|
4271
4873
|
const dict = args[args.length - 1];
|
|
4272
4874
|
const def = args[args.length - 2];
|
|
4273
4875
|
const keys = args.slice(0, -2);
|
|
4274
|
-
if (!isPlainDict(dict)) throw bodyTypeMismatch("dig", args.length - 1, "dict",
|
|
4275
|
-
for (let i = 0; i < keys.length; i++) if (typeof keys[i] !== "string") throw bodyTypeMismatch("dig", i, "string",
|
|
4876
|
+
if (!isPlainDict(dict)) throw bodyTypeMismatch("dig", args.length - 1, "dict", typeNameOf(dict));
|
|
4877
|
+
for (let i = 0; i < keys.length; i++) if (typeof keys[i] !== "string") throw bodyTypeMismatch("dig", i, "string", typeNameOf(keys[i]));
|
|
4276
4878
|
return digFromDict(dict, def, keys, args.length);
|
|
4277
4879
|
}
|
|
4278
4880
|
function digFromDict(d, def, ks, argCount) {
|
|
@@ -4280,19 +4882,12 @@ function digFromDict(d, def, ks, argCount) {
|
|
|
4280
4882
|
if (!Object.hasOwn(d, k)) return def;
|
|
4281
4883
|
const step = d[k];
|
|
4282
4884
|
if (ks.length === 1) return step;
|
|
4283
|
-
if (!isPlainDict(step)) throw bodyTypeMismatch("dig", argCount - 1, "dict at intermediate key",
|
|
4885
|
+
if (!isPlainDict(step)) throw bodyTypeMismatch("dig", argCount - 1, "dict at intermediate key", typeNameOf(step));
|
|
4284
4886
|
return digFromDict(step, def, ks.slice(1), argCount);
|
|
4285
4887
|
}
|
|
4286
4888
|
function isPlainDict(v) {
|
|
4287
4889
|
return v !== null && typeof v === "object" && !Array.isArray(v) && !(v instanceof Map) && !(v instanceof Set);
|
|
4288
4890
|
}
|
|
4289
|
-
function describeKind(v) {
|
|
4290
|
-
if (v === null) return "null";
|
|
4291
|
-
if (Array.isArray(v)) return "array";
|
|
4292
|
-
if (v instanceof Map) return "Map";
|
|
4293
|
-
if (v instanceof Set) return "Set";
|
|
4294
|
-
return typeof v;
|
|
4295
|
-
}
|
|
4296
4891
|
//#endregion
|
|
4297
4892
|
//#region src/sprig/lists/first.ts
|
|
4298
4893
|
function first(list) {
|
|
@@ -4404,97 +4999,115 @@ function sprigLists() {
|
|
|
4404
4999
|
return {
|
|
4405
5000
|
list: {
|
|
4406
5001
|
fn: (...a) => list(...a),
|
|
4407
|
-
argTypes: ["value"]
|
|
5002
|
+
argTypes: ["value"],
|
|
5003
|
+
arity: { kind: "variadic" }
|
|
4408
5004
|
},
|
|
4409
5005
|
first: {
|
|
4410
5006
|
fn: (l) => first(l),
|
|
4411
|
-
argTypes: ["list"]
|
|
5007
|
+
argTypes: ["list"],
|
|
5008
|
+
arity: { kind: "exact" }
|
|
4412
5009
|
},
|
|
4413
5010
|
last: {
|
|
4414
5011
|
fn: (l) => last(l),
|
|
4415
|
-
argTypes: ["list"]
|
|
5012
|
+
argTypes: ["list"],
|
|
5013
|
+
arity: { kind: "exact" }
|
|
4416
5014
|
},
|
|
4417
5015
|
rest: {
|
|
4418
5016
|
fn: (l) => rest(l),
|
|
4419
|
-
argTypes: ["list"]
|
|
5017
|
+
argTypes: ["list"],
|
|
5018
|
+
arity: { kind: "exact" }
|
|
4420
5019
|
},
|
|
4421
5020
|
initial: {
|
|
4422
5021
|
fn: (l) => initial(l),
|
|
4423
|
-
argTypes: ["list"]
|
|
5022
|
+
argTypes: ["list"],
|
|
5023
|
+
arity: { kind: "exact" }
|
|
4424
5024
|
},
|
|
4425
5025
|
len: {
|
|
4426
5026
|
fn: (l) => len(l),
|
|
4427
|
-
argTypes: ["sized"]
|
|
5027
|
+
argTypes: ["sized"],
|
|
5028
|
+
arity: { kind: "exact" }
|
|
4428
5029
|
},
|
|
4429
5030
|
reverse: {
|
|
4430
5031
|
fn: (l) => reverse(l),
|
|
4431
|
-
argTypes: ["list"]
|
|
5032
|
+
argTypes: ["list"],
|
|
5033
|
+
arity: { kind: "exact" }
|
|
4432
5034
|
},
|
|
4433
5035
|
uniq: {
|
|
4434
5036
|
fn: (l) => uniq(l),
|
|
4435
|
-
argTypes: ["list"]
|
|
5037
|
+
argTypes: ["list"],
|
|
5038
|
+
arity: { kind: "exact" }
|
|
4436
5039
|
},
|
|
4437
5040
|
without: {
|
|
4438
5041
|
fn: (l, ...e) => without(l, ...e),
|
|
4439
|
-
argTypes: ["list", "value"]
|
|
5042
|
+
argTypes: ["list", "value"],
|
|
5043
|
+
arity: { kind: "variadic" }
|
|
4440
5044
|
},
|
|
4441
5045
|
has: {
|
|
4442
5046
|
fn: (i, l) => has(i, l),
|
|
4443
|
-
argTypes: ["value", "list"]
|
|
5047
|
+
argTypes: ["value", "list"],
|
|
5048
|
+
arity: { kind: "exact" }
|
|
4444
5049
|
},
|
|
4445
5050
|
compact: {
|
|
4446
5051
|
fn: (l) => compact(l),
|
|
4447
|
-
argTypes: ["list"]
|
|
5052
|
+
argTypes: ["list"],
|
|
5053
|
+
arity: { kind: "exact" }
|
|
4448
5054
|
},
|
|
4449
5055
|
slice: {
|
|
4450
5056
|
fn: (l, i, j) => slice(l, i, j),
|
|
4451
|
-
argTypes: [
|
|
4452
|
-
|
|
4453
|
-
"int",
|
|
4454
|
-
"int"
|
|
4455
|
-
]
|
|
5057
|
+
argTypes: ["list", "truncating-int"],
|
|
5058
|
+
arity: { kind: "variadic" }
|
|
4456
5059
|
},
|
|
4457
5060
|
concat: {
|
|
4458
5061
|
fn: (...l) => concat(...l),
|
|
4459
|
-
argTypes: ["list"]
|
|
5062
|
+
argTypes: ["list"],
|
|
5063
|
+
arity: { kind: "variadic" }
|
|
4460
5064
|
},
|
|
4461
5065
|
chunk: {
|
|
4462
5066
|
fn: (s, l) => chunk(s, l),
|
|
4463
|
-
argTypes: ["int", "list"]
|
|
5067
|
+
argTypes: ["int", "list"],
|
|
5068
|
+
arity: { kind: "exact" }
|
|
4464
5069
|
},
|
|
4465
5070
|
prepend: {
|
|
4466
5071
|
fn: (l, i) => prepend(l, i),
|
|
4467
|
-
argTypes: ["list", "value"]
|
|
5072
|
+
argTypes: ["list", "value"],
|
|
5073
|
+
arity: { kind: "exact" }
|
|
4468
5074
|
},
|
|
4469
5075
|
append: {
|
|
4470
5076
|
fn: (l, i) => append(l, i),
|
|
4471
|
-
argTypes: ["list", "value"]
|
|
5077
|
+
argTypes: ["list", "value"],
|
|
5078
|
+
arity: { kind: "exact" }
|
|
4472
5079
|
},
|
|
4473
5080
|
sortAlpha: {
|
|
4474
5081
|
fn: (l) => sortAlpha(l),
|
|
4475
5082
|
argTypes: ["list"],
|
|
5083
|
+
arity: { kind: "exact" },
|
|
4476
5084
|
returnType: "list"
|
|
4477
5085
|
},
|
|
4478
5086
|
push: {
|
|
4479
5087
|
fn: (l, i) => append(l, i),
|
|
4480
|
-
argTypes: ["list", "value"]
|
|
5088
|
+
argTypes: ["list", "value"],
|
|
5089
|
+
arity: { kind: "exact" }
|
|
4481
5090
|
},
|
|
4482
5091
|
tuple: {
|
|
4483
5092
|
fn: (...a) => list(...a),
|
|
4484
|
-
argTypes: ["value"]
|
|
5093
|
+
argTypes: ["value"],
|
|
5094
|
+
arity: { kind: "variadic" }
|
|
4485
5095
|
},
|
|
4486
5096
|
dig: {
|
|
4487
5097
|
fn: (...a) => dig(...a),
|
|
4488
|
-
argTypes: ["value"]
|
|
5098
|
+
argTypes: ["value"],
|
|
5099
|
+
arity: { kind: "variadic" }
|
|
4489
5100
|
},
|
|
4490
5101
|
all: {
|
|
4491
5102
|
fn: (...a) => all(...a),
|
|
4492
5103
|
argTypes: ["truthy"],
|
|
5104
|
+
arity: { kind: "variadic" },
|
|
4493
5105
|
returnType: "bool"
|
|
4494
5106
|
},
|
|
4495
5107
|
any: {
|
|
4496
5108
|
fn: (...a) => any(...a),
|
|
4497
5109
|
argTypes: ["truthy"],
|
|
5110
|
+
arity: { kind: "variadic" },
|
|
4498
5111
|
returnType: "bool"
|
|
4499
5112
|
}
|
|
4500
5113
|
};
|
|
@@ -4535,8 +5148,8 @@ function div(a, b) {
|
|
|
4535
5148
|
}
|
|
4536
5149
|
//#endregion
|
|
4537
5150
|
//#region src/sprig/math/divf.ts
|
|
4538
|
-
function divf(a,
|
|
4539
|
-
return
|
|
5151
|
+
function divf(a, ...rest) {
|
|
5152
|
+
return rest.reduce((acc, v) => acc / v, a);
|
|
4540
5153
|
}
|
|
4541
5154
|
//#endregion
|
|
4542
5155
|
//#region src/sprig/math/floor.ts
|
|
@@ -4671,8 +5284,8 @@ function sub(a, b) {
|
|
|
4671
5284
|
}
|
|
4672
5285
|
//#endregion
|
|
4673
5286
|
//#region src/sprig/math/subf.ts
|
|
4674
|
-
function subf(a,
|
|
4675
|
-
return
|
|
5287
|
+
function subf(a, ...rest) {
|
|
5288
|
+
return rest.reduce((acc, v) => acc - v, a);
|
|
4676
5289
|
}
|
|
4677
5290
|
//#endregion
|
|
4678
5291
|
//#region src/sprig/math/until.ts
|
|
@@ -4691,88 +5304,113 @@ function sprigMath() {
|
|
|
4691
5304
|
return {
|
|
4692
5305
|
add: {
|
|
4693
5306
|
fn: (...a) => add(...a),
|
|
4694
|
-
argTypes: ["int"]
|
|
5307
|
+
argTypes: ["truncating-int"],
|
|
5308
|
+
arity: { kind: "variadic" }
|
|
4695
5309
|
},
|
|
4696
5310
|
sub: {
|
|
4697
5311
|
fn: (a, b) => sub(a, b),
|
|
4698
|
-
argTypes: ["int", "int"]
|
|
5312
|
+
argTypes: ["truncating-int", "truncating-int"],
|
|
5313
|
+
arity: { kind: "exact" }
|
|
4699
5314
|
},
|
|
4700
5315
|
mul: {
|
|
4701
5316
|
fn: (...a) => mul(...a),
|
|
4702
|
-
argTypes: ["int"]
|
|
5317
|
+
argTypes: ["truncating-int", "truncating-int"],
|
|
5318
|
+
arity: { kind: "variadic" }
|
|
4703
5319
|
},
|
|
4704
5320
|
div: {
|
|
4705
5321
|
fn: (a, b) => div(a, b),
|
|
4706
|
-
argTypes: ["int", "int"]
|
|
5322
|
+
argTypes: ["truncating-int", "truncating-int"],
|
|
5323
|
+
arity: { kind: "exact" }
|
|
4707
5324
|
},
|
|
4708
5325
|
mod: {
|
|
4709
5326
|
fn: (a, b) => mod(a, b),
|
|
4710
|
-
argTypes: ["int", "int"]
|
|
5327
|
+
argTypes: ["truncating-int", "truncating-int"],
|
|
5328
|
+
arity: { kind: "exact" }
|
|
4711
5329
|
},
|
|
4712
5330
|
min: {
|
|
4713
5331
|
fn: (...a) => min(...a),
|
|
4714
|
-
argTypes: ["int"]
|
|
5332
|
+
argTypes: ["truncating-int", "truncating-int"],
|
|
5333
|
+
arity: { kind: "variadic" }
|
|
4715
5334
|
},
|
|
4716
5335
|
max: {
|
|
4717
5336
|
fn: (...a) => max(...a),
|
|
4718
|
-
argTypes: ["int"]
|
|
5337
|
+
argTypes: ["truncating-int", "truncating-int"],
|
|
5338
|
+
arity: { kind: "variadic" }
|
|
4719
5339
|
},
|
|
4720
5340
|
floor: {
|
|
4721
5341
|
fn: (a) => floor(a),
|
|
4722
|
-
argTypes: ["float"]
|
|
5342
|
+
argTypes: ["float"],
|
|
5343
|
+
arity: { kind: "exact" }
|
|
4723
5344
|
},
|
|
4724
5345
|
ceil: {
|
|
4725
5346
|
fn: (a) => ceil(a),
|
|
4726
|
-
argTypes: ["float"]
|
|
5347
|
+
argTypes: ["float"],
|
|
5348
|
+
arity: { kind: "exact" }
|
|
4727
5349
|
},
|
|
4728
5350
|
round: {
|
|
4729
5351
|
fn: (a, p) => round(a, p),
|
|
4730
|
-
argTypes: [
|
|
5352
|
+
argTypes: [
|
|
5353
|
+
"float",
|
|
5354
|
+
"int",
|
|
5355
|
+
"float"
|
|
5356
|
+
],
|
|
5357
|
+
arity: { kind: "variadic" }
|
|
4731
5358
|
},
|
|
4732
5359
|
addf: {
|
|
4733
5360
|
fn: (...a) => addf(...a),
|
|
4734
|
-
argTypes: ["float"]
|
|
5361
|
+
argTypes: ["float"],
|
|
5362
|
+
arity: { kind: "variadic" }
|
|
4735
5363
|
},
|
|
4736
5364
|
subf: {
|
|
4737
|
-
fn: (a,
|
|
4738
|
-
argTypes: ["float", "float"]
|
|
5365
|
+
fn: (a, ...rest) => subf(a, ...rest),
|
|
5366
|
+
argTypes: ["float", "float"],
|
|
5367
|
+
arity: { kind: "variadic" }
|
|
4739
5368
|
},
|
|
4740
5369
|
mulf: {
|
|
4741
5370
|
fn: (...a) => mulf(...a),
|
|
4742
|
-
argTypes: ["float"]
|
|
5371
|
+
argTypes: ["float", "float"],
|
|
5372
|
+
arity: { kind: "variadic" }
|
|
4743
5373
|
},
|
|
4744
5374
|
divf: {
|
|
4745
|
-
fn: (a,
|
|
4746
|
-
argTypes: ["float", "float"]
|
|
5375
|
+
fn: (a, ...rest) => divf(a, ...rest),
|
|
5376
|
+
argTypes: ["float", "float"],
|
|
5377
|
+
arity: { kind: "variadic" }
|
|
4747
5378
|
},
|
|
4748
5379
|
add1: {
|
|
4749
5380
|
fn: (n) => add1(n),
|
|
4750
|
-
argTypes: ["int"]
|
|
5381
|
+
argTypes: ["truncating-int"],
|
|
5382
|
+
arity: { kind: "exact" }
|
|
4751
5383
|
},
|
|
4752
5384
|
add1f: {
|
|
4753
5385
|
fn: (n) => add1f(n),
|
|
4754
|
-
argTypes: ["float"]
|
|
5386
|
+
argTypes: ["float"],
|
|
5387
|
+
arity: { kind: "exact" }
|
|
4755
5388
|
},
|
|
4756
5389
|
maxf: {
|
|
4757
5390
|
fn: (...a) => maxf(...a),
|
|
4758
|
-
argTypes: ["float"]
|
|
5391
|
+
argTypes: ["float", "float"],
|
|
5392
|
+
arity: { kind: "variadic" }
|
|
4759
5393
|
},
|
|
4760
5394
|
minf: {
|
|
4761
5395
|
fn: (...a) => minf(...a),
|
|
4762
|
-
argTypes: ["float"]
|
|
5396
|
+
argTypes: ["float", "float"],
|
|
5397
|
+
arity: { kind: "variadic" }
|
|
4763
5398
|
},
|
|
4764
5399
|
biggest: {
|
|
4765
5400
|
fn: (...a) => max(...a),
|
|
4766
|
-
argTypes: ["int"]
|
|
5401
|
+
argTypes: ["truncating-int", "truncating-int"],
|
|
5402
|
+
arity: { kind: "variadic" }
|
|
4767
5403
|
},
|
|
4768
5404
|
seq: {
|
|
4769
5405
|
fn: (...a) => seq(...a),
|
|
4770
5406
|
argTypes: ["int"],
|
|
5407
|
+
arity: { kind: "variadic" },
|
|
4771
5408
|
returnType: "string"
|
|
4772
5409
|
},
|
|
4773
5410
|
until: {
|
|
4774
5411
|
fn: (n) => until(n),
|
|
4775
5412
|
argTypes: ["int"],
|
|
5413
|
+
arity: { kind: "exact" },
|
|
4776
5414
|
returnType: "list"
|
|
4777
5415
|
},
|
|
4778
5416
|
untilStep: {
|
|
@@ -4782,6 +5420,7 @@ function sprigMath() {
|
|
|
4782
5420
|
"int",
|
|
4783
5421
|
"int"
|
|
4784
5422
|
],
|
|
5423
|
+
arity: { kind: "exact" },
|
|
4785
5424
|
returnType: "list"
|
|
4786
5425
|
}
|
|
4787
5426
|
};
|
|
@@ -4856,6 +5495,51 @@ function randNumeric(n, random) {
|
|
|
4856
5495
|
return randString(n, NUMERIC, random);
|
|
4857
5496
|
}
|
|
4858
5497
|
//#endregion
|
|
5498
|
+
//#region src/sprig/strings/runes.ts
|
|
5499
|
+
/**
|
|
5500
|
+
* The rune-level string seam: the operations Go gets for free on its
|
|
5501
|
+
* strings and JS does not.
|
|
5502
|
+
*
|
|
5503
|
+
* [LAW:parse-dont-validate] Go walks a string as runes; JS strings are
|
|
5504
|
+
* UTF-16, so a character-position operation done on the raw string
|
|
5505
|
+
* (`s.slice(0, 1)`, `s[i]`, `s.length`, `s.split("")`) splits an astral
|
|
5506
|
+
* character in half and yields a lone surrogate — `trunc 1 "𐍈x"`
|
|
5507
|
+
* returned "\ud800", for which `isWellFormed()` is false. Decoding once,
|
|
5508
|
+
* up front, is the checkpoint: every element of the returned array is a
|
|
5509
|
+
* whole character, so an index into the middle of one is unrepresentable
|
|
5510
|
+
* and no slice of it can produce a lone surrogate. Callers re-encode
|
|
5511
|
+
* with `.join("")`.
|
|
5512
|
+
*
|
|
5513
|
+
* Code points deliberately — not UTF-16 units, and not grapheme
|
|
5514
|
+
* clusters. Grapheme clustering would hold `"é"` together, a third
|
|
5515
|
+
* behaviour matching neither Go nor this engine.
|
|
5516
|
+
*
|
|
5517
|
+
* For splitting this buys exact Go parity. For slicing it buys
|
|
5518
|
+
* well-formedness instead: Go slices `trunc`/`substr`/`abbrev` by BYTE,
|
|
5519
|
+
* so `trunc 1 "𐍈x"` is the single invalid byte `\xf0` there and the
|
|
5520
|
+
* whole `𐍈` here. That residual divergence is intended and tracked
|
|
5521
|
+
* separately; a rune is the closest well-formed unit to Go's byte.
|
|
5522
|
+
*/
|
|
5523
|
+
/**
|
|
5524
|
+
* Decode `s` to its code points. The array is freshly built per call,
|
|
5525
|
+
* so callers may permute it in place (`shuffle` does).
|
|
5526
|
+
*/
|
|
5527
|
+
function runes(s) {
|
|
5528
|
+
return Array.from(s);
|
|
5529
|
+
}
|
|
5530
|
+
/**
|
|
5531
|
+
* `strings.Split(s, sep)` — an empty separator splits between runes,
|
|
5532
|
+
* where JS's `s.split("")` splits between UTF-16 units and shears
|
|
5533
|
+
* astral characters into lone surrogates.
|
|
5534
|
+
*
|
|
5535
|
+
* [LAW:single-enforcer] `split`, `splitList`, `splitn`, and `replace`
|
|
5536
|
+
* all reach for Go's split; the empty-separator rule lives here so the
|
|
5537
|
+
* four cannot drift on it.
|
|
5538
|
+
*/
|
|
5539
|
+
function goSplit(sep, s) {
|
|
5540
|
+
return sep === "" ? runes(s) : s.split(sep);
|
|
5541
|
+
}
|
|
5542
|
+
//#endregion
|
|
4859
5543
|
//#region src/sprig/random/shuffle.ts
|
|
4860
5544
|
/**
|
|
4861
5545
|
* `shuffle s` — returns a randomly permuted copy of `s`.
|
|
@@ -4863,14 +5547,14 @@ function randNumeric(n, random) {
|
|
|
4863
5547
|
* Operates on Unicode code points (matching Go sprig's rune-level Fisher-Yates).
|
|
4864
5548
|
*/
|
|
4865
5549
|
function shuffle(s, random) {
|
|
4866
|
-
const
|
|
4867
|
-
for (let i =
|
|
5550
|
+
const cps = runes(s);
|
|
5551
|
+
for (let i = cps.length - 1; i > 0; i--) {
|
|
4868
5552
|
const j = Math.floor(clampRandom(random()) * (i + 1));
|
|
4869
|
-
const tmp =
|
|
4870
|
-
|
|
4871
|
-
|
|
5553
|
+
const tmp = cps[i];
|
|
5554
|
+
cps[i] = cps[j];
|
|
5555
|
+
cps[j] = tmp;
|
|
4872
5556
|
}
|
|
4873
|
-
return
|
|
5557
|
+
return cps.join("");
|
|
4874
5558
|
}
|
|
4875
5559
|
//#endregion
|
|
4876
5560
|
//#region src/sprig/random/index.ts
|
|
@@ -4884,27 +5568,33 @@ function sprigRandom(random = Math.random) {
|
|
|
4884
5568
|
return {
|
|
4885
5569
|
randInt: {
|
|
4886
5570
|
fn: (min, max) => randInt(min, max, random),
|
|
4887
|
-
argTypes: ["int", "int"]
|
|
5571
|
+
argTypes: ["int", "int"],
|
|
5572
|
+
arity: { kind: "exact" }
|
|
4888
5573
|
},
|
|
4889
5574
|
randAlpha: {
|
|
4890
5575
|
fn: (n) => randAlpha(n, random),
|
|
4891
|
-
argTypes: ["int"]
|
|
5576
|
+
argTypes: ["int"],
|
|
5577
|
+
arity: { kind: "exact" }
|
|
4892
5578
|
},
|
|
4893
5579
|
randAlphaNum: {
|
|
4894
5580
|
fn: (n) => randAlphaNum(n, random),
|
|
4895
|
-
argTypes: ["int"]
|
|
5581
|
+
argTypes: ["int"],
|
|
5582
|
+
arity: { kind: "exact" }
|
|
4896
5583
|
},
|
|
4897
5584
|
randNumeric: {
|
|
4898
5585
|
fn: (n) => randNumeric(n, random),
|
|
4899
|
-
argTypes: ["int"]
|
|
5586
|
+
argTypes: ["int"],
|
|
5587
|
+
arity: { kind: "exact" }
|
|
4900
5588
|
},
|
|
4901
5589
|
randAscii: {
|
|
4902
5590
|
fn: (n) => randAscii(n, random),
|
|
4903
|
-
argTypes: ["int"]
|
|
5591
|
+
argTypes: ["int"],
|
|
5592
|
+
arity: { kind: "exact" }
|
|
4904
5593
|
},
|
|
4905
5594
|
shuffle: {
|
|
4906
5595
|
fn: (s) => shuffle(s, random),
|
|
4907
|
-
argTypes: ["string"]
|
|
5596
|
+
argTypes: ["string"],
|
|
5597
|
+
arity: { kind: "exact" }
|
|
4908
5598
|
}
|
|
4909
5599
|
};
|
|
4910
5600
|
}
|
|
@@ -4970,11 +5660,13 @@ function sprigRegex() {
|
|
|
4970
5660
|
return {
|
|
4971
5661
|
regexMatch: {
|
|
4972
5662
|
fn: (p, s) => regexMatch(p, s),
|
|
4973
|
-
argTypes: ["string", "string"]
|
|
5663
|
+
argTypes: ["string", "string"],
|
|
5664
|
+
arity: { kind: "exact" }
|
|
4974
5665
|
},
|
|
4975
5666
|
regexFind: {
|
|
4976
5667
|
fn: (p, s) => regexFind(p, s),
|
|
4977
|
-
argTypes: ["string", "string"]
|
|
5668
|
+
argTypes: ["string", "string"],
|
|
5669
|
+
arity: { kind: "exact" }
|
|
4978
5670
|
},
|
|
4979
5671
|
regexFindAll: {
|
|
4980
5672
|
fn: (p, s, n) => regexFindAll(p, s, n),
|
|
@@ -4982,7 +5674,8 @@ function sprigRegex() {
|
|
|
4982
5674
|
"string",
|
|
4983
5675
|
"string",
|
|
4984
5676
|
"int"
|
|
4985
|
-
]
|
|
5677
|
+
],
|
|
5678
|
+
arity: { kind: "exact" }
|
|
4986
5679
|
},
|
|
4987
5680
|
regexReplaceAll: {
|
|
4988
5681
|
fn: (p, s, r) => regexReplaceAll(p, s, r),
|
|
@@ -4990,7 +5683,8 @@ function sprigRegex() {
|
|
|
4990
5683
|
"string",
|
|
4991
5684
|
"string",
|
|
4992
5685
|
"string"
|
|
4993
|
-
]
|
|
5686
|
+
],
|
|
5687
|
+
arity: { kind: "exact" }
|
|
4994
5688
|
},
|
|
4995
5689
|
regexReplaceAllLiteral: {
|
|
4996
5690
|
fn: (p, s, r) => regexReplaceAllLiteral(p, s, r),
|
|
@@ -4998,7 +5692,8 @@ function sprigRegex() {
|
|
|
4998
5692
|
"string",
|
|
4999
5693
|
"string",
|
|
5000
5694
|
"string"
|
|
5001
|
-
]
|
|
5695
|
+
],
|
|
5696
|
+
arity: { kind: "exact" }
|
|
5002
5697
|
},
|
|
5003
5698
|
regexSplit: {
|
|
5004
5699
|
fn: (p, s, n) => regexSplit(p, s, n),
|
|
@@ -5006,7 +5701,8 @@ function sprigRegex() {
|
|
|
5006
5701
|
"string",
|
|
5007
5702
|
"string",
|
|
5008
5703
|
"int"
|
|
5009
|
-
]
|
|
5704
|
+
],
|
|
5705
|
+
arity: { kind: "exact" }
|
|
5010
5706
|
}
|
|
5011
5707
|
};
|
|
5012
5708
|
}
|
|
@@ -5194,40 +5890,72 @@ function sprigSemver() {
|
|
|
5194
5890
|
return {
|
|
5195
5891
|
semver: {
|
|
5196
5892
|
fn: (s) => semver(s),
|
|
5197
|
-
argTypes: ["string"]
|
|
5893
|
+
argTypes: ["string"],
|
|
5894
|
+
arity: { kind: "exact" }
|
|
5198
5895
|
},
|
|
5199
5896
|
semverCompare: {
|
|
5200
5897
|
fn: (constraint, version) => semverCompare(constraint, version),
|
|
5201
5898
|
argTypes: ["string", "string"],
|
|
5899
|
+
arity: { kind: "exact" },
|
|
5202
5900
|
returnType: "bool"
|
|
5203
5901
|
}
|
|
5204
5902
|
};
|
|
5205
5903
|
}
|
|
5206
5904
|
//#endregion
|
|
5905
|
+
//#region src/sprig/strings/abbreviateFull.ts
|
|
5906
|
+
/**
|
|
5907
|
+
* goutils.AbbreviateFull (Masterminds/goutils@v1.1.1 stringutils.go), the
|
|
5908
|
+
* function behind both sprig `abbrev` and `abbrevboth`: keep at most
|
|
5909
|
+
* `maxWidth` characters of `cps`, starting near `offset`, marking each cut
|
|
5910
|
+
* end with `...`.
|
|
5911
|
+
*
|
|
5912
|
+
* [LAW:one-source-of-truth] Ported whole, step for step in Go's order, so
|
|
5913
|
+
* both sprig wrappers share Go's one control flow instead of each
|
|
5914
|
+
* reconstructing the branch it noticed. Indexed by code point where Go
|
|
5915
|
+
* indexes by byte — see `runes.js`.
|
|
5916
|
+
*
|
|
5917
|
+
* Go returns an error at its two minimum-width checks; both sprig wrappers
|
|
5918
|
+
* guard against reaching them, so here they throw rather than model sprig's
|
|
5919
|
+
* swallowed error.
|
|
5920
|
+
*/
|
|
5921
|
+
function abbreviateFull(cps, offset, maxWidth) {
|
|
5922
|
+
if (cps.length === 0) return "";
|
|
5923
|
+
if (maxWidth < 4) throw new Error("stringutils illegal argument: Minimum abbreviation width is 4");
|
|
5924
|
+
if (cps.length <= maxWidth) return cps.join("");
|
|
5925
|
+
const start = Math.min(offset, cps.length - (maxWidth - 3));
|
|
5926
|
+
if (start <= 4) return `${cps.slice(0, maxWidth - 3).join("")}...`;
|
|
5927
|
+
if (maxWidth < 7) throw new Error("stringutils illegal argument: Minimum abbreviation width with offset is 7");
|
|
5928
|
+
if (start + maxWidth - 3 < cps.length) return `...${abbreviateFull(cps.slice(start), 0, maxWidth - 3)}`;
|
|
5929
|
+
return `...${cps.slice(cps.length - (maxWidth - 3)).join("")}`;
|
|
5930
|
+
}
|
|
5931
|
+
//#endregion
|
|
5207
5932
|
//#region src/sprig/strings/abbrev.ts
|
|
5208
5933
|
/**
|
|
5209
5934
|
* `abbrev width s` — truncate `s` to `width` chars, replacing the tail
|
|
5210
|
-
* with `...` when truncation occurs
|
|
5211
|
-
*
|
|
5212
|
-
*
|
|
5935
|
+
* with `...` when truncation occurs: sprig's guard around
|
|
5936
|
+
* goutils.Abbreviate, which is AbbreviateFull at offset 0. Widths below 4
|
|
5937
|
+
* cannot fit the ellipsis and return `s` unchanged.
|
|
5938
|
+
*
|
|
5939
|
+
* `width` counts code points, so the cut never splits an astral
|
|
5940
|
+
* character — see `runes.js` for why that differs from Go's bytes.
|
|
5213
5941
|
*/
|
|
5214
5942
|
function abbrev(width, s) {
|
|
5215
|
-
if (s.length <= width) return s;
|
|
5216
5943
|
if (width < 4) return s;
|
|
5217
|
-
return
|
|
5944
|
+
return abbreviateFull(runes(s), 0, width);
|
|
5218
5945
|
}
|
|
5219
5946
|
//#endregion
|
|
5220
5947
|
//#region src/sprig/strings/abbrevboth.ts
|
|
5221
5948
|
/**
|
|
5222
|
-
* `abbrevboth left right s` —
|
|
5223
|
-
*
|
|
5224
|
-
* left
|
|
5225
|
-
*
|
|
5949
|
+
* `abbrevboth left right s` — sprig's guard around goutils.AbbreviateFull
|
|
5950
|
+
* with `left` as the offset and `right` as the maximum width: a window of
|
|
5951
|
+
* `s` near `left`, with `...` at each end that was cut.
|
|
5952
|
+
*
|
|
5953
|
+
* Offsets count code points, so the window never splits an astral
|
|
5954
|
+
* character — see `runes.js` for why that differs from Go's bytes.
|
|
5226
5955
|
*/
|
|
5227
5956
|
function abbrevboth(left, right, s) {
|
|
5228
|
-
if (right <
|
|
5229
|
-
|
|
5230
|
-
return `...${s.slice(left, left + right - 6)}...`;
|
|
5957
|
+
if (right < 4 || left > 0 && right < 7) return s;
|
|
5958
|
+
return abbreviateFull(runes(s), left, right);
|
|
5231
5959
|
}
|
|
5232
5960
|
//#endregion
|
|
5233
5961
|
//#region src/sprig/strings/caseUtils.ts
|
|
@@ -5243,14 +5971,13 @@ function abbrevboth(left, right, s) {
|
|
|
5243
5971
|
*
|
|
5244
5972
|
* ## Port notes
|
|
5245
5973
|
*
|
|
5246
|
-
* Go iterates UTF-8 bytes via `utf8.DecodeRuneInString`.
|
|
5247
|
-
*
|
|
5248
|
-
*
|
|
5249
|
-
*
|
|
5250
|
-
*
|
|
5251
|
-
*
|
|
5252
|
-
*
|
|
5253
|
-
* actually exercise.
|
|
5974
|
+
* Go iterates UTF-8 bytes via `utf8.DecodeRuneInString`. The `runes`
|
|
5975
|
+
* seam yields code points the same way Go yields runes, so the
|
|
5976
|
+
* rune-level logic carries over. We intentionally do NOT attempt to
|
|
5977
|
+
* mirror xstrings' `isAlphabet` CJK-exclusion table — for ASCII/Latin
|
|
5978
|
+
* templates, which are what real users feed sprig, the simpler
|
|
5979
|
+
* `isLetter` test matches Go's output. The fixture suite pins the
|
|
5980
|
+
* cases consumers actually exercise.
|
|
5254
5981
|
*
|
|
5255
5982
|
* `unicode.ToTitle` from Go has no JS equivalent for ASCII letters
|
|
5256
5983
|
* (Title and Upper coincide for code points without a separate Title
|
|
@@ -5420,7 +6147,7 @@ function pushLowered(buf, word, connector) {
|
|
|
5420
6147
|
*/
|
|
5421
6148
|
function camelCaseToLowerCase(s, connector) {
|
|
5422
6149
|
if (s.length === 0) return "";
|
|
5423
|
-
const cps =
|
|
6150
|
+
const cps = runes(s);
|
|
5424
6151
|
const buf = [];
|
|
5425
6152
|
let { word, next: i } = nextWord(cps, 0);
|
|
5426
6153
|
while (i < cps.length) {
|
|
@@ -5474,7 +6201,7 @@ function camelCaseToLowerCase(s, connector) {
|
|
|
5474
6201
|
*/
|
|
5475
6202
|
function camelCase(s) {
|
|
5476
6203
|
if (s.length === 0) return "";
|
|
5477
|
-
const cps =
|
|
6204
|
+
const cps = runes(s);
|
|
5478
6205
|
const buf = [];
|
|
5479
6206
|
let i = 0;
|
|
5480
6207
|
let r0 = "";
|
|
@@ -5584,17 +6311,11 @@ function initials(s) {
|
|
|
5584
6311
|
function join(sep, list) {
|
|
5585
6312
|
const parts = [];
|
|
5586
6313
|
for (const v of list) {
|
|
5587
|
-
if (v !== null && v !== void 0 && typeof v === "object") throw bodyTypeMismatch("join", 2, "list of strings/primitives", `list element of type ${
|
|
6314
|
+
if (v !== null && v !== void 0 && typeof v === "object") throw bodyTypeMismatch("join", 2, "list of strings/primitives", `list element of type ${typeNameOf(v)}`);
|
|
5588
6315
|
parts.push(String(v));
|
|
5589
6316
|
}
|
|
5590
6317
|
return parts.join(sep);
|
|
5591
6318
|
}
|
|
5592
|
-
function describe(v) {
|
|
5593
|
-
if (Array.isArray(v)) return "array";
|
|
5594
|
-
if (v instanceof Map) return "Map";
|
|
5595
|
-
if (v instanceof Set) return "Set";
|
|
5596
|
-
return "object";
|
|
5597
|
-
}
|
|
5598
6319
|
//#endregion
|
|
5599
6320
|
//#region src/sprig/strings/kebabcase.ts
|
|
5600
6321
|
/**
|
|
@@ -5663,9 +6384,16 @@ function repeat(n, s) {
|
|
|
5663
6384
|
}
|
|
5664
6385
|
//#endregion
|
|
5665
6386
|
//#region src/sprig/strings/replace.ts
|
|
5666
|
-
/**
|
|
6387
|
+
/**
|
|
6388
|
+
* Replaces ALL occurrences of `old` with `nw` in `s` (Go's strings.Replace n=-1).
|
|
6389
|
+
*
|
|
6390
|
+
* An empty `old` splits between runes rather than UTF-16 units, so the
|
|
6391
|
+
* replacement is never inserted inside an astral character. Where Go
|
|
6392
|
+
* additionally inserts `nw` at both ends (`-a-b-c-` to this port's
|
|
6393
|
+
* `a-b-c`) it still differs — a separate, non-Unicode divergence.
|
|
6394
|
+
*/
|
|
5667
6395
|
function replace(old, nw, s) {
|
|
5668
|
-
return
|
|
6396
|
+
return goSplit(old, s).join(nw);
|
|
5669
6397
|
}
|
|
5670
6398
|
//#endregion
|
|
5671
6399
|
//#region src/sprig/strings/snakecase.ts
|
|
@@ -5687,7 +6415,7 @@ function snakecase(s) {
|
|
|
5687
6415
|
* split "/" "a/b/c" → { _0: "a", _1: "b", _2: "c" }
|
|
5688
6416
|
*/
|
|
5689
6417
|
function split(sep, s) {
|
|
5690
|
-
const parts =
|
|
6418
|
+
const parts = goSplit(sep, s);
|
|
5691
6419
|
const out = {};
|
|
5692
6420
|
for (let i = 0; i < parts.length; i++) out[`_${i}`] = parts[i];
|
|
5693
6421
|
return out;
|
|
@@ -5695,7 +6423,7 @@ function split(sep, s) {
|
|
|
5695
6423
|
//#endregion
|
|
5696
6424
|
//#region src/sprig/strings/splitList.ts
|
|
5697
6425
|
function splitList(sep, s) {
|
|
5698
|
-
return
|
|
6426
|
+
return goSplit(sep, s);
|
|
5699
6427
|
}
|
|
5700
6428
|
//#endregion
|
|
5701
6429
|
//#region src/sprig/strings/splitn.ts
|
|
@@ -5726,7 +6454,7 @@ function splitn(sep, n, s) {
|
|
|
5726
6454
|
}
|
|
5727
6455
|
function goSplitN(s, sep, n) {
|
|
5728
6456
|
if (n === 0) return [];
|
|
5729
|
-
const all = sep
|
|
6457
|
+
const all = goSplit(sep, s);
|
|
5730
6458
|
if (n < 0 || all.length <= n) return all;
|
|
5731
6459
|
const head = all.slice(0, n - 1);
|
|
5732
6460
|
const tail = all.slice(n - 1).join(sep);
|
|
@@ -5743,13 +6471,17 @@ function squote(...args) {
|
|
|
5743
6471
|
/**
|
|
5744
6472
|
* `substr i j s` — substring [i, j) with out-of-range clamping.
|
|
5745
6473
|
* Matches Go sprig's behavior of clamping rather than throwing:
|
|
5746
|
-
* negative `end` (and any `end
|
|
5747
|
-
* `start` clamps to `[0, end]`.
|
|
6474
|
+
* negative `end` (and any `end` past the last char) clamps to the
|
|
6475
|
+
* string's length; `start` clamps to `[0, end]`.
|
|
6476
|
+
*
|
|
6477
|
+
* Offsets count code points, so a window never splits an astral
|
|
6478
|
+
* character — see `runes.js` for why that differs from Go's bytes.
|
|
5748
6479
|
*/
|
|
5749
6480
|
function substr(start, end, s) {
|
|
5750
|
-
const
|
|
6481
|
+
const cps = runes(s);
|
|
6482
|
+
const j = end < 0 || end > cps.length ? cps.length : end;
|
|
5751
6483
|
const i = Math.min(Math.max(0, start), j);
|
|
5752
|
-
return
|
|
6484
|
+
return cps.slice(i, j).join("");
|
|
5753
6485
|
}
|
|
5754
6486
|
//#endregion
|
|
5755
6487
|
//#region src/sprig/strings/swapcase.ts
|
|
@@ -5792,15 +6524,23 @@ function trim(s) {
|
|
|
5792
6524
|
}
|
|
5793
6525
|
//#endregion
|
|
5794
6526
|
//#region src/sprig/strings/trimAll.ts
|
|
5795
|
-
/**
|
|
6527
|
+
/**
|
|
6528
|
+
* `trimAll cutset s` — strip leading + trailing chars in `cutset`.
|
|
6529
|
+
*
|
|
6530
|
+
* Both sides are compared as code points, matching Go's `strings.Trim`,
|
|
6531
|
+
* which decodes the cutset as runes. Scanning `s` by UTF-16 unit
|
|
6532
|
+
* compared half an astral character against whole ones, so an astral
|
|
6533
|
+
* cutset char never matched and the string came back untrimmed.
|
|
6534
|
+
*/
|
|
5796
6535
|
function trimAll(cutset, s) {
|
|
5797
6536
|
if (cutset.length === 0) return s;
|
|
5798
|
-
const set = new Set(
|
|
6537
|
+
const set = new Set(runes(cutset));
|
|
6538
|
+
const cps = runes(s);
|
|
5799
6539
|
let start = 0;
|
|
5800
|
-
while (start <
|
|
5801
|
-
let end =
|
|
5802
|
-
while (end > start && set.has(
|
|
5803
|
-
return
|
|
6540
|
+
while (start < cps.length && set.has(cps[start])) start += 1;
|
|
6541
|
+
let end = cps.length;
|
|
6542
|
+
while (end > start && set.has(cps[end - 1])) end -= 1;
|
|
6543
|
+
return cps.slice(start, end).join("");
|
|
5804
6544
|
}
|
|
5805
6545
|
//#endregion
|
|
5806
6546
|
//#region src/sprig/strings/trimPrefix.ts
|
|
@@ -5817,13 +6557,17 @@ function trimSuffix(suffix, s) {
|
|
|
5817
6557
|
/**
|
|
5818
6558
|
* `trunc n s` — first `n` chars when n>=0; last |n| chars when n<0.
|
|
5819
6559
|
* Strings shorter than |n| are returned unchanged. Matches Go sprig.
|
|
6560
|
+
*
|
|
6561
|
+
* `n` counts code points, so truncating never splits an astral
|
|
6562
|
+
* character — see `runes.js` for why that differs from Go's bytes.
|
|
5820
6563
|
*/
|
|
5821
6564
|
function trunc(n, s) {
|
|
6565
|
+
const cps = runes(s);
|
|
5822
6566
|
if (n < 0) {
|
|
5823
6567
|
const k = -n;
|
|
5824
|
-
return k >=
|
|
6568
|
+
return k >= cps.length ? s : cps.slice(cps.length - k).join("");
|
|
5825
6569
|
}
|
|
5826
|
-
return n >=
|
|
6570
|
+
return n >= cps.length ? s : cps.slice(0, n).join("");
|
|
5827
6571
|
}
|
|
5828
6572
|
//#endregion
|
|
5829
6573
|
//#region src/sprig/strings/untitle.ts
|
|
@@ -5875,7 +6619,8 @@ function sprigStrings() {
|
|
|
5875
6619
|
return {
|
|
5876
6620
|
abbrev: {
|
|
5877
6621
|
fn: (w, s) => abbrev(w, s),
|
|
5878
|
-
argTypes: ["int", "string"]
|
|
6622
|
+
argTypes: ["int", "string"],
|
|
6623
|
+
arity: { kind: "exact" }
|
|
5879
6624
|
},
|
|
5880
6625
|
abbrevboth: {
|
|
5881
6626
|
fn: (l, r, s) => abbrevboth(l, r, s),
|
|
@@ -5883,57 +6628,70 @@ function sprigStrings() {
|
|
|
5883
6628
|
"int",
|
|
5884
6629
|
"int",
|
|
5885
6630
|
"string"
|
|
5886
|
-
]
|
|
6631
|
+
],
|
|
6632
|
+
arity: { kind: "exact" }
|
|
5887
6633
|
},
|
|
5888
6634
|
camelcase: {
|
|
5889
6635
|
fn: (s) => camelcase(s),
|
|
5890
6636
|
argTypes: ["string"],
|
|
6637
|
+
arity: { kind: "exact" },
|
|
5891
6638
|
returnType: "string"
|
|
5892
6639
|
},
|
|
5893
6640
|
cat: {
|
|
5894
6641
|
fn: (...a) => cat(...a),
|
|
5895
|
-
argTypes: ["string"]
|
|
6642
|
+
argTypes: ["string"],
|
|
6643
|
+
arity: { kind: "variadic" }
|
|
5896
6644
|
},
|
|
5897
6645
|
contains: {
|
|
5898
6646
|
fn: (sub, s) => contains(sub, s),
|
|
5899
|
-
argTypes: ["string", "string"]
|
|
6647
|
+
argTypes: ["string", "string"],
|
|
6648
|
+
arity: { kind: "exact" }
|
|
5900
6649
|
},
|
|
5901
6650
|
hasPrefix: {
|
|
5902
6651
|
fn: (p, s) => hasPrefix(p, s),
|
|
5903
|
-
argTypes: ["string", "string"]
|
|
6652
|
+
argTypes: ["string", "string"],
|
|
6653
|
+
arity: { kind: "exact" }
|
|
5904
6654
|
},
|
|
5905
6655
|
hasSuffix: {
|
|
5906
6656
|
fn: (sf, s) => hasSuffix(sf, s),
|
|
5907
|
-
argTypes: ["string", "string"]
|
|
6657
|
+
argTypes: ["string", "string"],
|
|
6658
|
+
arity: { kind: "exact" }
|
|
5908
6659
|
},
|
|
5909
6660
|
indent: {
|
|
5910
6661
|
fn: (n, s) => indent(n, s),
|
|
5911
|
-
argTypes: ["int", "string"]
|
|
6662
|
+
argTypes: ["int", "string"],
|
|
6663
|
+
arity: { kind: "exact" }
|
|
5912
6664
|
},
|
|
5913
6665
|
initials: {
|
|
5914
6666
|
fn: (s) => initials(s),
|
|
5915
|
-
argTypes: ["string"]
|
|
6667
|
+
argTypes: ["string"],
|
|
6668
|
+
arity: { kind: "exact" }
|
|
5916
6669
|
},
|
|
5917
6670
|
join: {
|
|
5918
6671
|
fn: (sep, list) => join(sep, list),
|
|
5919
|
-
argTypes: ["string", "list"]
|
|
6672
|
+
argTypes: ["string", "list"],
|
|
6673
|
+
arity: { kind: "exact" }
|
|
5920
6674
|
},
|
|
5921
6675
|
kebabcase: {
|
|
5922
6676
|
fn: (s) => kebabcase(s),
|
|
5923
6677
|
argTypes: ["string"],
|
|
6678
|
+
arity: { kind: "exact" },
|
|
5924
6679
|
returnType: "string"
|
|
5925
6680
|
},
|
|
5926
6681
|
lower: {
|
|
5927
6682
|
fn: (s) => lower(s),
|
|
5928
|
-
argTypes: ["string"]
|
|
6683
|
+
argTypes: ["string"],
|
|
6684
|
+
arity: { kind: "exact" }
|
|
5929
6685
|
},
|
|
5930
6686
|
nindent: {
|
|
5931
6687
|
fn: (n, s) => nindent(n, s),
|
|
5932
|
-
argTypes: ["int", "string"]
|
|
6688
|
+
argTypes: ["int", "string"],
|
|
6689
|
+
arity: { kind: "exact" }
|
|
5933
6690
|
},
|
|
5934
6691
|
nospace: {
|
|
5935
6692
|
fn: (s) => nospace(s),
|
|
5936
6693
|
argTypes: ["string"],
|
|
6694
|
+
arity: { kind: "exact" },
|
|
5937
6695
|
returnType: "string"
|
|
5938
6696
|
},
|
|
5939
6697
|
plural: {
|
|
@@ -5943,20 +6701,24 @@ function sprigStrings() {
|
|
|
5943
6701
|
"string",
|
|
5944
6702
|
"int"
|
|
5945
6703
|
],
|
|
6704
|
+
arity: { kind: "exact" },
|
|
5946
6705
|
returnType: "string"
|
|
5947
6706
|
},
|
|
5948
6707
|
quote: {
|
|
5949
6708
|
fn: (...a) => quote(...a),
|
|
5950
|
-
argTypes: ["string"]
|
|
6709
|
+
argTypes: ["string"],
|
|
6710
|
+
arity: { kind: "variadic" }
|
|
5951
6711
|
},
|
|
5952
6712
|
regexQuoteMeta: {
|
|
5953
6713
|
fn: (s) => regexQuoteMeta(s),
|
|
5954
6714
|
argTypes: ["string"],
|
|
6715
|
+
arity: { kind: "exact" },
|
|
5955
6716
|
returnType: "string"
|
|
5956
6717
|
},
|
|
5957
6718
|
repeat: {
|
|
5958
6719
|
fn: (n, s) => repeat(n, s),
|
|
5959
|
-
argTypes: ["int", "string"]
|
|
6720
|
+
argTypes: ["int", "string"],
|
|
6721
|
+
arity: { kind: "exact" }
|
|
5960
6722
|
},
|
|
5961
6723
|
replace: {
|
|
5962
6724
|
fn: (o, n, s) => replace(o, n, s),
|
|
@@ -5964,20 +6726,24 @@ function sprigStrings() {
|
|
|
5964
6726
|
"string",
|
|
5965
6727
|
"string",
|
|
5966
6728
|
"string"
|
|
5967
|
-
]
|
|
6729
|
+
],
|
|
6730
|
+
arity: { kind: "exact" }
|
|
5968
6731
|
},
|
|
5969
6732
|
snakecase: {
|
|
5970
6733
|
fn: (s) => snakecase(s),
|
|
5971
6734
|
argTypes: ["string"],
|
|
6735
|
+
arity: { kind: "exact" },
|
|
5972
6736
|
returnType: "string"
|
|
5973
6737
|
},
|
|
5974
6738
|
split: {
|
|
5975
6739
|
fn: (sep, s) => split(sep, s),
|
|
5976
|
-
argTypes: ["string", "string"]
|
|
6740
|
+
argTypes: ["string", "string"],
|
|
6741
|
+
arity: { kind: "exact" }
|
|
5977
6742
|
},
|
|
5978
6743
|
splitList: {
|
|
5979
6744
|
fn: (sep, s) => splitList(sep, s),
|
|
5980
|
-
argTypes: ["string", "string"]
|
|
6745
|
+
argTypes: ["string", "string"],
|
|
6746
|
+
arity: { kind: "exact" }
|
|
5981
6747
|
},
|
|
5982
6748
|
splitn: {
|
|
5983
6749
|
fn: (sep, n, s) => splitn(sep, n, s),
|
|
@@ -5986,11 +6752,13 @@ function sprigStrings() {
|
|
|
5986
6752
|
"int",
|
|
5987
6753
|
"string"
|
|
5988
6754
|
],
|
|
6755
|
+
arity: { kind: "exact" },
|
|
5989
6756
|
returnType: "dict"
|
|
5990
6757
|
},
|
|
5991
6758
|
squote: {
|
|
5992
6759
|
fn: (...a) => squote(...a),
|
|
5993
|
-
argTypes: ["string"]
|
|
6760
|
+
argTypes: ["string"],
|
|
6761
|
+
arity: { kind: "variadic" }
|
|
5994
6762
|
},
|
|
5995
6763
|
substr: {
|
|
5996
6764
|
fn: (i, j, s) => substr(i, j, s),
|
|
@@ -5998,48 +6766,59 @@ function sprigStrings() {
|
|
|
5998
6766
|
"int",
|
|
5999
6767
|
"int",
|
|
6000
6768
|
"string"
|
|
6001
|
-
]
|
|
6769
|
+
],
|
|
6770
|
+
arity: { kind: "exact" }
|
|
6002
6771
|
},
|
|
6003
6772
|
swapcase: {
|
|
6004
6773
|
fn: (s) => swapcase(s),
|
|
6005
6774
|
argTypes: ["string"],
|
|
6775
|
+
arity: { kind: "exact" },
|
|
6006
6776
|
returnType: "string"
|
|
6007
6777
|
},
|
|
6008
6778
|
title: {
|
|
6009
6779
|
fn: (s) => title(s),
|
|
6010
|
-
argTypes: ["string"]
|
|
6780
|
+
argTypes: ["string"],
|
|
6781
|
+
arity: { kind: "exact" }
|
|
6011
6782
|
},
|
|
6012
6783
|
trim: {
|
|
6013
6784
|
fn: (s) => trim(s),
|
|
6014
|
-
argTypes: ["string"]
|
|
6785
|
+
argTypes: ["string"],
|
|
6786
|
+
arity: { kind: "exact" }
|
|
6015
6787
|
},
|
|
6016
6788
|
trimAll: {
|
|
6017
6789
|
fn: (cs, s) => trimAll(cs, s),
|
|
6018
|
-
argTypes: ["string", "string"]
|
|
6790
|
+
argTypes: ["string", "string"],
|
|
6791
|
+
arity: { kind: "exact" }
|
|
6019
6792
|
},
|
|
6020
6793
|
trimPrefix: {
|
|
6021
6794
|
fn: (p, s) => trimPrefix(p, s),
|
|
6022
|
-
argTypes: ["string", "string"]
|
|
6795
|
+
argTypes: ["string", "string"],
|
|
6796
|
+
arity: { kind: "exact" }
|
|
6023
6797
|
},
|
|
6024
6798
|
trimSuffix: {
|
|
6025
6799
|
fn: (sf, s) => trimSuffix(sf, s),
|
|
6026
|
-
argTypes: ["string", "string"]
|
|
6800
|
+
argTypes: ["string", "string"],
|
|
6801
|
+
arity: { kind: "exact" }
|
|
6027
6802
|
},
|
|
6028
6803
|
trunc: {
|
|
6029
6804
|
fn: (n, s) => trunc(n, s),
|
|
6030
|
-
argTypes: ["int", "string"]
|
|
6805
|
+
argTypes: ["int", "string"],
|
|
6806
|
+
arity: { kind: "exact" }
|
|
6031
6807
|
},
|
|
6032
6808
|
untitle: {
|
|
6033
6809
|
fn: (s) => untitle(s),
|
|
6034
|
-
argTypes: ["string"]
|
|
6810
|
+
argTypes: ["string"],
|
|
6811
|
+
arity: { kind: "exact" }
|
|
6035
6812
|
},
|
|
6036
6813
|
upper: {
|
|
6037
6814
|
fn: (s) => upper(s),
|
|
6038
|
-
argTypes: ["string"]
|
|
6815
|
+
argTypes: ["string"],
|
|
6816
|
+
arity: { kind: "exact" }
|
|
6039
6817
|
},
|
|
6040
6818
|
wrap: {
|
|
6041
6819
|
fn: (w, s) => wrap(w, s),
|
|
6042
|
-
argTypes: ["int", "string"]
|
|
6820
|
+
argTypes: ["int", "string"],
|
|
6821
|
+
arity: { kind: "exact" }
|
|
6043
6822
|
},
|
|
6044
6823
|
wrapWith: {
|
|
6045
6824
|
fn: (w, sep, s) => wrapWith(w, sep, s),
|
|
@@ -6047,7 +6826,8 @@ function sprigStrings() {
|
|
|
6047
6826
|
"int",
|
|
6048
6827
|
"string",
|
|
6049
6828
|
"string"
|
|
6050
|
-
]
|
|
6829
|
+
],
|
|
6830
|
+
arity: { kind: "exact" }
|
|
6051
6831
|
}
|
|
6052
6832
|
};
|
|
6053
6833
|
}
|
|
@@ -6119,35 +6899,42 @@ function sprigTypes() {
|
|
|
6119
6899
|
return {
|
|
6120
6900
|
kindOf: {
|
|
6121
6901
|
fn: (v) => kindOf(v),
|
|
6122
|
-
argTypes: ["reflective"]
|
|
6902
|
+
argTypes: ["reflective"],
|
|
6903
|
+
arity: { kind: "exact" }
|
|
6123
6904
|
},
|
|
6124
6905
|
kindIs: {
|
|
6125
6906
|
fn: (k, v) => kindIs(k, v),
|
|
6126
|
-
argTypes: ["string", "reflective"]
|
|
6907
|
+
argTypes: ["string", "reflective"],
|
|
6908
|
+
arity: { kind: "exact" }
|
|
6127
6909
|
},
|
|
6128
6910
|
typeOf: {
|
|
6129
6911
|
fn: (v) => typeOf(v),
|
|
6130
|
-
argTypes: ["reflective"]
|
|
6912
|
+
argTypes: ["reflective"],
|
|
6913
|
+
arity: { kind: "exact" }
|
|
6131
6914
|
},
|
|
6132
6915
|
typeIs: {
|
|
6133
6916
|
fn: (t, v) => typeIs(t, v),
|
|
6134
|
-
argTypes: ["string", "reflective"]
|
|
6917
|
+
argTypes: ["string", "reflective"],
|
|
6918
|
+
arity: { kind: "exact" }
|
|
6135
6919
|
},
|
|
6136
6920
|
typeIsLike: {
|
|
6137
6921
|
fn: (t, v) => typeIsLike(t, v),
|
|
6138
|
-
argTypes: ["string", "reflective"]
|
|
6922
|
+
argTypes: ["string", "reflective"],
|
|
6923
|
+
arity: { kind: "exact" }
|
|
6139
6924
|
},
|
|
6140
6925
|
deepEqual: {
|
|
6141
6926
|
fn: (a, b) => deepEqual(a, b),
|
|
6142
|
-
argTypes: ["value", "value"]
|
|
6927
|
+
argTypes: ["value", "value"],
|
|
6928
|
+
arity: { kind: "exact" }
|
|
6143
6929
|
},
|
|
6144
6930
|
deepCopy: {
|
|
6145
6931
|
fn: (v) => deepCopy(v),
|
|
6146
|
-
argTypes: ["value"]
|
|
6932
|
+
argTypes: ["value"],
|
|
6933
|
+
arity: { kind: "exact" }
|
|
6147
6934
|
}
|
|
6148
6935
|
};
|
|
6149
6936
|
}
|
|
6150
6937
|
//#endregion
|
|
6151
|
-
export { Defines, Engine, EvalError, FailError, FuncNotFoundError, MissingFieldError, ParseError, Template, TemplateError, TypeMismatchError, createEngine, sprigConversions, sprigDatetime, sprigDefaults, sprigDicts, sprigFlow, sprigHash, sprigLists, sprigMath, sprigRandom, sprigRegex, sprigSemver, sprigStrings, sprigTypes, staticDictEntries };
|
|
6938
|
+
export { ArgCountError, Defines, Engine, EvalError, FailError, FuncNotFoundError, MissingFieldError, NotIntegerError, ParseError, Template, TemplateError, TypeMismatchError, createEngine, include, marked, sprigConversions, sprigDatetime, sprigDefaults, sprigDicts, sprigFlow, sprigHash, sprigLists, sprigMath, sprigRandom, sprigRegex, sprigSemver, sprigStrings, sprigTypes, staticDictEntries, unmarked };
|
|
6152
6939
|
|
|
6153
6940
|
//# sourceMappingURL=index.js.map
|