@promptctl/go-template-js 0.8.0 → 0.10.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/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,29 @@ 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
+ };
58
81
  var TypeMismatchError = class extends EvalError {
59
82
  name = "TypeMismatchError";
60
83
  kind = "TypeMismatchError";
@@ -71,6 +94,21 @@ var TypeMismatchError = class extends EvalError {
71
94
  this.receivedSummary = receivedSummary;
72
95
  }
73
96
  };
97
+ /**
98
+ * An integer slot refused a number that is not a safe integer. Still a
99
+ * `TypeMismatchError` (the slot's type refused the value), but worded
100
+ * with the value and the reason: a fractional is Go's own refusal,
101
+ * `expected integer; found 2.7`, and an integer past 2^53 is
102
+ * `integer out of range: <n>`, since JS cannot carry it exactly. The
103
+ * generic wording would say a number was expected and a number arrived,
104
+ * and suggest flattening a styled value that was never there.
105
+ */
106
+ var NotIntegerError = class extends TypeMismatchError {
107
+ constructor(funcName, argIndex, value, pos, ctx = {}) {
108
+ super(funcName, argIndex, "integer", String(value), pos, ctx);
109
+ this.message = Number.isInteger(Number(value)) ? `integer out of range: ${String(value)}` : `expected integer; found ${String(value)}`;
110
+ }
111
+ };
74
112
  var MissingFieldError = class extends EvalError {
75
113
  name = "MissingFieldError";
76
114
  kind = "MissingFieldError";
@@ -1404,35 +1442,42 @@ function isTruthy(value) {
1404
1442
  * matching names override the built-in (intentional — gives
1405
1443
  * consumers an escape hatch).
1406
1444
  */
1407
- function eagerBuiltins(toString) {
1445
+ function eagerBuiltins(toString, isT) {
1408
1446
  return {
1409
1447
  eq: {
1410
1448
  fn: (a, ...rest) => rest.some((r) => goEqPair(a, r)),
1411
- argTypes: ["comparable"]
1449
+ argTypes: ["comparable", "comparable"],
1450
+ arity: { kind: "variadic" }
1412
1451
  },
1413
1452
  ne: {
1414
1453
  fn: (a, b) => !goEqPair(a, b),
1415
- argTypes: ["comparable", "comparable"]
1454
+ argTypes: ["comparable", "comparable"],
1455
+ arity: { kind: "exact" }
1416
1456
  },
1417
1457
  lt: {
1418
1458
  fn: (a, b) => compare(a, b) < 0,
1419
- argTypes: ["ordered", "ordered"]
1459
+ argTypes: ["ordered", "ordered"],
1460
+ arity: { kind: "exact" }
1420
1461
  },
1421
1462
  le: {
1422
1463
  fn: (a, b) => compare(a, b) <= 0,
1423
- argTypes: ["ordered", "ordered"]
1464
+ argTypes: ["ordered", "ordered"],
1465
+ arity: { kind: "exact" }
1424
1466
  },
1425
1467
  gt: {
1426
1468
  fn: (a, b) => compare(a, b) > 0,
1427
- argTypes: ["ordered", "ordered"]
1469
+ argTypes: ["ordered", "ordered"],
1470
+ arity: { kind: "exact" }
1428
1471
  },
1429
1472
  ge: {
1430
1473
  fn: (a, b) => compare(a, b) >= 0,
1431
- argTypes: ["ordered", "ordered"]
1474
+ argTypes: ["ordered", "ordered"],
1475
+ arity: { kind: "exact" }
1432
1476
  },
1433
1477
  len: {
1434
1478
  fn: (v) => goLen(v),
1435
- argTypes: ["sized"]
1479
+ argTypes: ["sized"],
1480
+ arity: { kind: "exact" }
1436
1481
  },
1437
1482
  index: {
1438
1483
  fn: (collection, ...keys) => {
@@ -1440,60 +1485,78 @@ function eagerBuiltins(toString) {
1440
1485
  for (const k of keys) cur = goIndex(cur, k);
1441
1486
  return cur;
1442
1487
  },
1443
- argTypes: ["collection", "index-key"]
1488
+ argTypes: ["collection", "index-key"],
1489
+ arity: { kind: "variadic" }
1444
1490
  },
1445
1491
  slice: {
1446
- fn: (collection, ...indices) => {
1447
- const i = indices.length >= 1 ? indices[0] : 0;
1448
- const j = indices.length >= 2 ? indices[1] : void 0;
1449
- return typeof collection === "string" ? collection.slice(i, j) : collection.slice(i, j);
1450
- },
1451
- argTypes: ["sliceable", "int"]
1492
+ fn: (collection, ...indices) => goSlice(collection, indices),
1493
+ argTypes: ["sliceable", "int"],
1494
+ arity: { kind: "variadic" }
1452
1495
  },
1453
1496
  print: {
1454
1497
  fn: (...args) => goPrint(args, toString),
1455
1498
  argTypes: ["stringifiable"],
1499
+ arity: { kind: "variadic" },
1456
1500
  returnType: "string"
1457
1501
  },
1458
1502
  println: {
1459
1503
  fn: (...args) => `${goPrint(args, toString)}\n`,
1460
1504
  argTypes: ["stringifiable"],
1505
+ arity: { kind: "variadic" },
1461
1506
  returnType: "string"
1462
1507
  },
1463
1508
  printf: {
1464
- fn: (format, ...args) => sprintf(format, args, toString),
1509
+ fn: (format, ...args) => sprintf(format, args, toString, isT),
1465
1510
  argTypes: ["string", "stringifiable"],
1511
+ arity: { kind: "variadic" },
1466
1512
  returnType: "string"
1467
1513
  },
1514
+ toString: {
1515
+ fn: (v) => formatV(v, toString, isT),
1516
+ argTypes: ["value"],
1517
+ arity: { kind: "exact" },
1518
+ returnType: "string"
1519
+ },
1520
+ toStrings: {
1521
+ fn: (list) => list.map((v) => formatV(v, toString, isT)),
1522
+ argTypes: ["list"],
1523
+ arity: { kind: "exact" }
1524
+ },
1468
1525
  call: {
1469
1526
  fn: (fn, ...args) => fn(...args),
1470
- argTypes: ["callable", "value"]
1527
+ argTypes: ["callable", "value"],
1528
+ arity: { kind: "variadic" }
1471
1529
  },
1472
1530
  html: {
1473
1531
  fn: (...args) => htmlEscape(goPrint(args, toString)),
1474
1532
  argTypes: ["stringifiable"],
1533
+ arity: { kind: "variadic" },
1475
1534
  returnType: "string"
1476
1535
  },
1477
1536
  js: {
1478
1537
  fn: (...args) => jsEscape(goPrint(args, toString)),
1479
1538
  argTypes: ["stringifiable"],
1539
+ arity: { kind: "variadic" },
1480
1540
  returnType: "string"
1481
1541
  },
1482
1542
  urlquery: {
1483
1543
  fn: (...args) => urlQueryEscape(goPrint(args, toString)),
1484
1544
  argTypes: ["stringifiable"],
1545
+ arity: { kind: "variadic" },
1485
1546
  returnType: "string"
1486
1547
  },
1487
1548
  not: {
1488
1549
  fn: (v) => !isTruthy(v),
1489
- argTypes: ["truthy"]
1550
+ argTypes: ["truthy"],
1551
+ arity: { kind: "exact" }
1490
1552
  }
1491
1553
  };
1492
1554
  }
1493
1555
  function lazyBuiltins() {
1494
1556
  return {
1495
1557
  and: markLazy({
1496
- argTypes: ["truthy"],
1558
+ argTypes: ["truthy", "truthy"],
1559
+ arity: { kind: "variadic" },
1497
1560
  fn: (...thunks) => {
1498
1561
  let last = true;
1499
1562
  for (const t of thunks) {
@@ -1504,7 +1567,8 @@ function lazyBuiltins() {
1504
1567
  }
1505
1568
  }),
1506
1569
  or: markLazy({
1507
- argTypes: ["truthy"],
1570
+ argTypes: ["truthy", "truthy"],
1571
+ arity: { kind: "variadic" },
1508
1572
  fn: (...thunks) => {
1509
1573
  let last = false;
1510
1574
  for (const t of thunks) {
@@ -1516,9 +1580,9 @@ function lazyBuiltins() {
1516
1580
  })
1517
1581
  };
1518
1582
  }
1519
- function defaultBuiltins(toString) {
1583
+ function defaultBuiltins(toString, isT) {
1520
1584
  return {
1521
- ...eagerBuiltins(toString),
1585
+ ...eagerBuiltins(toString, isT),
1522
1586
  ...lazyBuiltins()
1523
1587
  };
1524
1588
  }
@@ -1546,20 +1610,41 @@ function goLen(value) {
1546
1610
  return Object.keys(value).length;
1547
1611
  }
1548
1612
  function goIndex(collection, key) {
1549
- if (Array.isArray(collection)) {
1550
- const i = Number(key);
1551
- if (!Number.isInteger(i)) throw new Error(`index: array index must be integer, got ${describeType(key)}`);
1552
- if (i < 0 || i >= collection.length) throw new Error(`index: out of range [${i}] with length ${collection.length}`);
1613
+ if (Array.isArray(collection) || typeof collection === "string") {
1614
+ const i = indexArg(key, collection.length);
1615
+ if (i === collection.length) throw new Error(`index out of range: ${i}`);
1553
1616
  return collection[i];
1554
1617
  }
1555
1618
  if (collection instanceof Map) return collection.get(key);
1556
- if (typeof collection === "string") return collection[Number(key)];
1557
1619
  if (collection !== null && typeof collection === "object") {
1558
1620
  if (typeof key !== "string") throw new Error(`index: object index must be string, got ${describeType(key)}`);
1559
1621
  return collection[key];
1560
1622
  }
1561
1623
  throw new Error(`index: cannot index ${describeType(collection)}`);
1562
1624
  }
1625
+ function indexArg(key, cap) {
1626
+ const x = typeof key === "bigint" ? Number(key) : key;
1627
+ if (typeof x !== "number" || !Number.isInteger(x)) throw new Error(`cannot index slice/array with type ${goPrintfType(key)}`);
1628
+ if (x < 0 || x > cap) throw new Error(`index out of range: ${String(key)}`);
1629
+ return x;
1630
+ }
1631
+ function goSlice(collection, indices) {
1632
+ if (indices.length > 3) throw new Error(`too many slice indexes: ${indices.length}`);
1633
+ if (typeof collection === "string" && indices.length > 2) throw new Error("cannot 3-index slice a string");
1634
+ const cap = collection.length;
1635
+ const bounds = [
1636
+ 0,
1637
+ cap,
1638
+ cap
1639
+ ];
1640
+ indices.forEach((ix, n) => {
1641
+ bounds[n] = indexArg(ix, cap);
1642
+ });
1643
+ const [i, j, k] = bounds;
1644
+ if (i > j) throw new Error(`invalid slice index: ${i} > ${j}`);
1645
+ if (j > k) throw new Error(`invalid slice index: ${j} > ${k}`);
1646
+ return collection.slice(i, j);
1647
+ }
1563
1648
  function describeType(v) {
1564
1649
  if (v === null) return "null";
1565
1650
  if (v === void 0) return "undefined";
@@ -1641,7 +1726,7 @@ function urlQueryEscape(s) {
1641
1726
  else out += `%${b.toString(16).toUpperCase().padStart(2, "0")}`;
1642
1727
  return out;
1643
1728
  }
1644
- function sprintf(format, args, toString) {
1729
+ function sprintf(format, args, toString, isT) {
1645
1730
  let out = "";
1646
1731
  let argIdx = 0;
1647
1732
  let i = 0;
@@ -1667,18 +1752,18 @@ function sprintf(format, args, toString) {
1667
1752
  const verb = format[j];
1668
1753
  const spec = format.slice(i + 1, j);
1669
1754
  const arg = args[argIdx++];
1670
- out += formatVerb(verb ?? "", spec, arg, toString);
1755
+ out += formatVerb(verb ?? "", spec, arg, toString, isT);
1671
1756
  i = j + 1;
1672
1757
  }
1673
1758
  return out;
1674
1759
  }
1675
- function formatVerb(verb, spec, arg, toString) {
1760
+ function formatVerb(verb, spec, arg, toString, isT) {
1676
1761
  switch (verb) {
1677
1762
  case "s": return typeof arg === "string" ? applyWidth(spec, arg) : applyWidth(spec, stringifyForPrint(arg, toString));
1678
1763
  case "d":
1679
1764
  if (typeof arg !== "number" && typeof arg !== "bigint") return `%!${verb}(${goPrintfType(arg)}=${stringifyForPrint(arg, toString)})`;
1680
1765
  return applyWidth(spec, String(typeof arg === "bigint" ? arg : Math.trunc(arg)));
1681
- case "v": return applyWidth(spec, formatV(arg));
1766
+ case "v": return applyWidth(spec, formatV(arg, toString, isT));
1682
1767
  case "q": return applyWidth(spec, JSON.stringify(stringifyForPrint(arg, toString)));
1683
1768
  case "f": {
1684
1769
  if (typeof arg !== "number" && typeof arg !== "bigint") return `%!${verb}(${goPrintfType(arg)}=${stringifyForPrint(arg, toString)})`;
@@ -1708,15 +1793,33 @@ function applyWidth(spec, body) {
1708
1793
  const pad = " ".repeat(width - body.length);
1709
1794
  return flag.includes("-") ? body + pad : pad + body;
1710
1795
  }
1711
- function formatV(value) {
1796
+ function formatV(value, toString, isT) {
1712
1797
  if (value === null || value === void 0) return "<nil>";
1713
1798
  if (typeof value === "string") return value;
1714
- if (typeof value === "bigint") return value.toString();
1715
- if (Array.isArray(value)) return `[${value.map(formatV).join(" ")}]`;
1716
- if (value instanceof Map) return `map[${[...value.entries()].map(([k, v]) => `${formatV(k)}:${formatV(v)}`).join(" ")}]`;
1717
- if (typeof value === "object") return `{${Object.values(value).map(formatV).join(" ")}}`;
1799
+ if (isT(value)) return toString(value);
1800
+ if (Array.isArray(value)) return `[${value.map((v) => formatV(v, toString, isT)).join(" ")}]`;
1801
+ if (value instanceof Map) return formatEntries([...value.entries()], toString, isT);
1802
+ if (isPlainObject(value)) return formatEntries(Object.entries(value), toString, isT);
1718
1803
  return String(value);
1719
1804
  }
1805
+ function formatEntries(entries, toString, isT) {
1806
+ const fmt = (v) => formatV(v, toString, isT);
1807
+ const isNumeric = (k) => typeof k === "number" || typeof k === "bigint";
1808
+ const byKey = ([a], [b]) => {
1809
+ if (isNumeric(a) && isNumeric(b)) return a < b ? -1 : a > b ? 1 : 0;
1810
+ if (isNumeric(a) !== isNumeric(b)) return isNumeric(a) ? -1 : 1;
1811
+ const [as, bs] = [fmt(a), fmt(b)];
1812
+ return as < bs ? -1 : as > bs ? 1 : 0;
1813
+ };
1814
+ return `map[${[...entries].sort(byKey).map(([k, v]) => `${fmt(k)}:${fmt(v)}`).join(" ")}]`;
1815
+ }
1816
+ function isPlainObject(value) {
1817
+ if (value === null || typeof value !== "object") return false;
1818
+ if (Array.isArray(value)) return false;
1819
+ if (value instanceof Map || value instanceof Set) return false;
1820
+ const proto = Object.getPrototypeOf(value);
1821
+ return proto === null || proto === Object.prototype;
1822
+ }
1720
1823
  //#endregion
1721
1824
  //#region src/evaluator/scope.ts
1722
1825
  function rootScope(value) {
@@ -1775,6 +1878,43 @@ function declareVar(scope, name, value) {
1775
1878
  * no instance mutation, so the same Engine handles concurrent evaluate
1776
1879
  * calls (tested through the parse-once-eval-many invariant).
1777
1880
  */
1881
+ /**
1882
+ * Every kind a slot may declare.
1883
+ *
1884
+ * [LAW:one-source-of-truth] The kinds are a *list* first and a type
1885
+ * second. `validateArgSpecs` must reject a declaration naming a kind
1886
+ * that does not exist, and a runtime check needs a runtime list; writing
1887
+ * that list out beside a hand-maintained union would be the second
1888
+ * clock. So the array is authoritative and `ArgType` is derived from it
1889
+ * — one edit adds a kind to the type, to the construct-time check, and
1890
+ * to the two exhaustive switches that must then grow an arm.
1891
+ *
1892
+ * The per-kind semantics live on `ArgType` below, where a consumer
1893
+ * hovering the exported name finds them.
1894
+ */
1895
+ const ARG_TYPES = [
1896
+ "string",
1897
+ "int",
1898
+ "truncating-int",
1899
+ "float",
1900
+ "bool",
1901
+ "T",
1902
+ "ordered",
1903
+ "list",
1904
+ "dict",
1905
+ "sized",
1906
+ "comparable",
1907
+ "stringifiable",
1908
+ "liftable",
1909
+ "callable",
1910
+ "collection",
1911
+ "index-key",
1912
+ "sliceable",
1913
+ "truthy",
1914
+ "reflective",
1915
+ "value",
1916
+ "serializable"
1917
+ ];
1778
1918
  const VALID_MISSING_KEYS = new Set([
1779
1919
  "default",
1780
1920
  "zero",
@@ -1805,6 +1945,24 @@ function validateDelims(value) {
1805
1945
  right: value.right
1806
1946
  };
1807
1947
  }
1948
+ function readsSlotsFromArgTypes(arity) {
1949
+ switch (arity.kind) {
1950
+ case "exact": return false;
1951
+ case "variadic": return true;
1952
+ case "alternating": return true;
1953
+ }
1954
+ }
1955
+ function validateArgSpecs(funcs) {
1956
+ const legal = new Set(ARG_TYPES);
1957
+ for (const [name, fn] of Object.entries(funcs)) {
1958
+ fn.argTypes.forEach((declared, slot) => {
1959
+ 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(", ")}`);
1960
+ });
1961
+ 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" }`);
1962
+ 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`);
1963
+ }
1964
+ return funcs;
1965
+ }
1808
1966
  const BREAK_SIGNAL = Object.freeze({ kind: "break" });
1809
1967
  const CONTINUE_SIGNAL = Object.freeze({ kind: "continue" });
1810
1968
  const NO_PIPE = { kind: "none" };
@@ -1964,6 +2122,7 @@ function collectReferencedCalls(parsed) {
1964
2122
  var Engine = class {
1965
2123
  fromString;
1966
2124
  toString;
2125
+ isT;
1967
2126
  funcs;
1968
2127
  missingKey;
1969
2128
  delims;
@@ -1971,12 +2130,13 @@ var Engine = class {
1971
2130
  this.fromString = config.fromString;
1972
2131
  const userToString = Object.hasOwn(config, "toString") ? config.toString : void 0;
1973
2132
  this.toString = userToString ?? defaultToString;
2133
+ this.isT = config.isT ?? DEFAULT_IS_T;
1974
2134
  this.missingKey = validateMissingKey(config.missingKey);
1975
2135
  this.delims = validateDelims(config.delims);
1976
- this.funcs = {
1977
- ...defaultBuiltins(this.toString),
2136
+ this.funcs = validateArgSpecs({
2137
+ ...defaultBuiltins(this.toString, this.isT),
1978
2138
  ...config.funcs ?? {}
1979
- };
2139
+ });
1980
2140
  }
1981
2141
  /**
1982
2142
  * Parse a template source into a reusable Template handle.
@@ -2175,13 +2335,20 @@ var Engine = class {
2175
2335
  const v = piped.value;
2176
2336
  args.push(lazy ? () => v : v);
2177
2337
  }
2178
- enforceArgTypes(head.ident, fn.argTypes, args, cmd.pos, ctx.source, this.toString, fn.argTypePattern, this.fromString);
2338
+ return this.invoke(head.ident, fn, args, cmd.pos, ctx);
2339
+ }
2340
+ invoke(name, fn, args, pos, ctx) {
2341
+ enforceArgTypes(name, fn, args, pos, ctx.source, this.toString, this.fromString, this.isT);
2179
2342
  try {
2180
2343
  return fn.fn(...args);
2181
2344
  } catch (e) {
2182
- if (e instanceof TypeMismatchError) throw new TypeMismatchError(e.funcName, e.argIndex, e.expected, e.receivedSummary, cmd.pos, { source: ctx.source });
2183
- if (e instanceof FailError) throw new FailError(e.message, cmd.pos, { source: ctx.source });
2184
- throw e;
2345
+ if (e instanceof TypeMismatchError) throw new TypeMismatchError(e.funcName, e.argIndex, e.expected, e.receivedSummary, pos, { source: ctx.source });
2346
+ if (e instanceof FailError) throw new FailError(e.message, pos, { source: ctx.source });
2347
+ if (e instanceof TemplateError) throw e;
2348
+ throw new EvalError(`error calling ${name}: ${errorText(e)}`, pos, {
2349
+ source: ctx.source,
2350
+ cause: e
2351
+ });
2185
2352
  }
2186
2353
  }
2187
2354
  evalPrimary(node, scope, ctx) {
@@ -2199,8 +2366,7 @@ var Engine = class {
2199
2366
  source: ctx.source,
2200
2367
  available: Object.keys(this.funcs)
2201
2368
  });
2202
- enforceArgTypes(node.ident, fn.argTypes, [], node.pos, ctx.source, this.toString, fn.argTypePattern, this.fromString);
2203
- return fn.fn();
2369
+ return this.invoke(node.ident, fn, [], node.pos, ctx);
2204
2370
  }
2205
2371
  case "Chain": return this.resolveFieldChain(this.evalPrimary(node.node, scope, ctx), node.fields, node.pos, ctx);
2206
2372
  case "Pipe": return this.evalPipe(node, scope, ctx);
@@ -2236,45 +2402,34 @@ var Engine = class {
2236
2402
  ctx.out.push(this.fromString(String(value)));
2237
2403
  return;
2238
2404
  }
2239
- if (Array.isArray(value)) {
2240
- ctx.out.push(this.fromString(formatArrayLikeGo(value)));
2241
- return;
2242
- }
2243
- if (value instanceof Map) {
2244
- ctx.out.push(this.fromString(formatMapLikeGo(value)));
2405
+ if (this.isT(value)) {
2406
+ ctx.out.push(value);
2245
2407
  return;
2246
2408
  }
2247
- ctx.out.push(value);
2409
+ ctx.out.push(this.fromString(formatV(value, this.toString, this.isT)));
2248
2410
  }
2249
2411
  };
2250
- function formatArrayLikeGo(arr) {
2251
- return `[${arr.map(formatScalarLikeGo).join(" ")}]`;
2252
- }
2253
- function formatMapLikeGo(m) {
2254
- const parts = [];
2255
- for (const [k, v] of m) parts.push(`${formatScalarLikeGo(k)}:${formatScalarLikeGo(v)}`);
2256
- return `map[${parts.join(" ")}]`;
2257
- }
2258
- function formatScalarLikeGo(v) {
2259
- if (v === null || v === void 0) return "<nil>";
2260
- if (Array.isArray(v)) return formatArrayLikeGo(v);
2261
- if (v instanceof Map) return formatMapLikeGo(v);
2262
- return String(v);
2263
- }
2412
+ const DEFAULT_IS_T = (v) => typeof v === "object" && v !== null && !Array.isArray(v) && !(v instanceof Map);
2264
2413
  function createEngine(config) {
2265
2414
  return new Engine(config);
2266
2415
  }
2267
- function enforceArgTypes(funcName, argTypes, values, pos, src, toString = defaultToString, pattern, fromString = defaultFromString) {
2268
- const lookup = makeSlotLookup(argTypes, pattern);
2416
+ function enforceArgTypes(funcName, spec, values, pos, src, toString = defaultToString, fromString = defaultFromString, isT = DEFAULT_IS_T) {
2417
+ const { argTypes, arity } = spec;
2418
+ const accepted = acceptedArgCount(argTypes, arity);
2419
+ if (values.length < accepted.minimum || values.length > accepted.maximum) throw new ArgCountError(funcName, accepted, values.length, pos, { source: src });
2420
+ const lookup = makeSlotLookup(argTypes, arity);
2269
2421
  let firstOrdered = -1;
2270
2422
  let firstComparable = -1;
2271
2423
  for (let i = 0; i < values.length; i++) {
2272
2424
  const declared = lookup(i);
2273
2425
  const value = values[i];
2274
- if (!matchesArgType(declared, value, toString)) throw new TypeMismatchError(funcName, i + 1, humanArgType(declared), describeValue(value), pos, { source: src });
2426
+ if (!matchesArgType(declared, value, toString, isT)) {
2427
+ if ((declared === "int" || declared === "truncating-int") && (typeof value === "number" || typeof value === "bigint")) throw new NotIntegerError(funcName, i + 1, value, pos, { source: src });
2428
+ throw new TypeMismatchError(funcName, i + 1, humanArgType(declared), describeValue(value), pos, { source: src });
2429
+ }
2275
2430
  if (declared === "liftable" && typeof value === "string") values[i] = fromString(value);
2276
- if (declared === "int") values[i] = Math.trunc(Number(value));
2277
- else if (declared === "float") values[i] = Number(value);
2431
+ if (declared === "truncating-int") values[i] = Math.trunc(Number(value));
2432
+ else if (declared === "int" || declared === "float") values[i] = Number(value);
2278
2433
  if (declared === "ordered") {
2279
2434
  if (firstOrdered === -1) firstOrdered = i;
2280
2435
  else if (!sameOrderedKind(values[firstOrdered], value)) throw new TypeMismatchError(funcName, i + 1, `${humanArgType("ordered")} of the same kind as ${describeValue(values[firstOrdered])}`, describeValue(value), pos, { source: src });
@@ -2285,26 +2440,42 @@ function enforceArgTypes(funcName, argTypes, values, pos, src, toString = defaul
2285
2440
  }
2286
2441
  }
2287
2442
  }
2288
- function makeSlotLookup(argTypes, pattern) {
2289
- if (argTypes.length === 0) return () => "value";
2290
- if (pattern === "alternating") {
2291
- const len = argTypes.length;
2292
- return (i) => argTypes[i % len];
2443
+ function acceptedArgCount(argTypes, arity) {
2444
+ switch (arity.kind) {
2445
+ case "exact": return {
2446
+ minimum: argTypes.length,
2447
+ maximum: argTypes.length
2448
+ };
2449
+ case "variadic": return {
2450
+ minimum: argTypes.length - 1,
2451
+ maximum: Infinity
2452
+ };
2453
+ case "alternating": return {
2454
+ minimum: arity.minimum,
2455
+ maximum: Infinity
2456
+ };
2457
+ }
2458
+ }
2459
+ function makeSlotLookup(argTypes, arity) {
2460
+ const len = argTypes.length;
2461
+ switch (arity.kind) {
2462
+ case "exact": return (i) => argTypes[i];
2463
+ case "variadic": return (i) => argTypes[Math.min(i, len - 1)];
2464
+ case "alternating": return (i) => argTypes[i % len];
2293
2465
  }
2294
- const trailing = argTypes[argTypes.length - 1];
2295
- return (i) => i < argTypes.length ? argTypes[i] : trailing;
2296
2466
  }
2297
- function matchesArgType(declared, value, toString) {
2467
+ function matchesArgType(declared, value, toString, isT) {
2298
2468
  switch (declared) {
2299
2469
  case "truthy":
2300
2470
  case "reflective":
2301
2471
  case "value": return true;
2302
2472
  case "string": return typeof value === "string";
2303
- case "int": return typeof value === "number" && Number.isFinite(value) || typeof value === "bigint" && Number.isSafeInteger(Number(value));
2473
+ case "int": return typeof value === "number" && Number.isSafeInteger(value) || typeof value === "bigint" && Number.isSafeInteger(Number(value));
2474
+ case "truncating-int": return typeof value === "number" && Number.isFinite(value) || typeof value === "bigint" && Number.isSafeInteger(Number(value));
2304
2475
  case "float": return typeof value === "number" || typeof value === "bigint" && Number.isFinite(Number(value));
2305
2476
  case "bool": return typeof value === "boolean";
2306
2477
  case "ordered": return typeof value === "string" || typeof value === "number" || typeof value === "bigint" || typeof value === "boolean";
2307
- case "T": return value !== null && value !== void 0 && typeof value !== "string" && typeof value !== "number" && typeof value !== "boolean" && typeof value !== "bigint" && typeof value !== "symbol";
2478
+ case "T": return isT(value);
2308
2479
  case "list": return Array.isArray(value);
2309
2480
  case "dict": return isPlainObject(value);
2310
2481
  case "sized": return typeof value === "string" || Array.isArray(value) || value instanceof Map || value instanceof Set || isPlainObject(value);
@@ -2321,7 +2492,7 @@ function matchesArgType(declared, value, toString) {
2321
2492
  } catch {
2322
2493
  return false;
2323
2494
  }
2324
- case "liftable": return typeof value === "string" || value !== null && value !== void 0 && typeof value !== "number" && typeof value !== "boolean" && typeof value !== "bigint" && typeof value !== "symbol";
2495
+ case "liftable": return typeof value === "string" || isT(value);
2325
2496
  case "serializable": return isJsonSerializable(value);
2326
2497
  default: throw new Error(`invalid ArgType: ${String(declared)}`);
2327
2498
  }
@@ -2339,13 +2510,6 @@ function defaultToString(value) {
2339
2510
  offset: 0
2340
2511
  });
2341
2512
  }
2342
- function isPlainObject(value) {
2343
- if (value === null || typeof value !== "object") return false;
2344
- if (Array.isArray(value)) return false;
2345
- if (value instanceof Map || value instanceof Set) return false;
2346
- const proto = Object.getPrototypeOf(value);
2347
- return proto === null || proto === Object.prototype;
2348
- }
2349
2513
  function isJsonSerializable(value) {
2350
2514
  try {
2351
2515
  return JSON.stringify(value) !== void 0;
@@ -2377,7 +2541,8 @@ function comparableKind(v) {
2377
2541
  }
2378
2542
  function humanArgType(t) {
2379
2543
  switch (t) {
2380
- case "int": return "integer (finite number or safe-integer bigint)";
2544
+ case "int": return "integer (safe-integer number or bigint)";
2545
+ case "truncating-int": return "integer (finite number, truncated toward zero, or safe-integer bigint)";
2381
2546
  case "float": return "float (number, including NaN/Infinity, or finite-convertible bigint)";
2382
2547
  case "T": return "T (consumer-defined fragment)";
2383
2548
  case "ordered": return "orderable primitive";
@@ -2400,6 +2565,9 @@ function humanArgType(t) {
2400
2565
  default: throw new Error(`invalid ArgType: ${String(t)}`);
2401
2566
  }
2402
2567
  }
2568
+ function errorText(e) {
2569
+ return e instanceof Error ? e.message : String(e);
2570
+ }
2403
2571
  function describeValue(value) {
2404
2572
  if (value === null) return "null";
2405
2573
  if (value === void 0) return "undefined";
@@ -2631,68 +2799,42 @@ function toRawJson(value) {
2631
2799
  return JSON.stringify(value) ?? "null";
2632
2800
  }
2633
2801
  //#endregion
2634
- //#region src/sprig/conversions/toString.ts
2635
- /**
2636
- * `toString v` — Go sprig's `strval`. Strings pass through; everything
2637
- * else routes through Go's `%v` formatter (`fmt.Sprintf("%v", v)`).
2638
- *
2639
- * [LAW:one-source-of-truth] Shares `formatV` with printf's `%v` verb —
2640
- * one place owns "what does Go's %v look like".
2641
- */
2642
- function toString(v) {
2643
- return typeof v === "string" ? v : formatV(v);
2644
- }
2645
- //#endregion
2646
- //#region src/sprig/conversions/toStrings.ts
2647
- /**
2648
- * `toStrings list` — map each element through `toString`.
2649
- *
2650
- * The `"list"` gate validates array-ness once; this body trusts it
2651
- * and only routes the per-element conversion.
2652
- */
2653
- function toStrings(list) {
2654
- return list.map(toString);
2655
- }
2656
- //#endregion
2657
2802
  //#region src/sprig/conversions/index.ts
2658
2803
  function sprigConversions() {
2659
2804
  return {
2660
2805
  atoi: {
2661
2806
  fn: (s) => atoi(s),
2662
2807
  argTypes: ["string"],
2808
+ arity: { kind: "exact" },
2663
2809
  returnType: "int"
2664
2810
  },
2665
2811
  int: {
2666
2812
  fn: (v) => int(v),
2667
2813
  argTypes: ["value"],
2814
+ arity: { kind: "exact" },
2668
2815
  returnType: "int"
2669
2816
  },
2670
2817
  int64: {
2671
2818
  fn: (v) => int64(v),
2672
- argTypes: ["value"]
2819
+ argTypes: ["value"],
2820
+ arity: { kind: "exact" }
2673
2821
  },
2674
2822
  float64: {
2675
2823
  fn: (v) => float64(v),
2676
2824
  argTypes: ["value"],
2825
+ arity: { kind: "exact" },
2677
2826
  returnType: "float"
2678
2827
  },
2679
- toString: {
2680
- fn: (v) => toString(v),
2681
- argTypes: ["value"],
2682
- returnType: "string"
2683
- },
2684
- toStrings: {
2685
- fn: (l) => toStrings(l),
2686
- argTypes: ["list"]
2687
- },
2688
2828
  toDecimal: {
2689
2829
  fn: (s) => toDecimal(s),
2690
2830
  argTypes: ["string"],
2831
+ arity: { kind: "exact" },
2691
2832
  returnType: "int"
2692
2833
  },
2693
2834
  toRawJson: {
2694
2835
  fn: (v) => toRawJson(v),
2695
2836
  argTypes: ["serializable"],
2837
+ arity: { kind: "exact" },
2696
2838
  returnType: "string"
2697
2839
  }
2698
2840
  };
@@ -3584,11 +3726,13 @@ function sprigDatetime(clock = () => /* @__PURE__ */ new Date()) {
3584
3726
  return {
3585
3727
  now: {
3586
3728
  fn: () => now(clock),
3587
- argTypes: []
3729
+ argTypes: [],
3730
+ arity: { kind: "exact" }
3588
3731
  },
3589
3732
  date: {
3590
3733
  fn: (format, t) => date(format, t),
3591
- argTypes: ["string", "value"]
3734
+ argTypes: ["string", "value"],
3735
+ arity: { kind: "exact" }
3592
3736
  },
3593
3737
  dateInZone: {
3594
3738
  fn: (format, t, zone) => dateInZone(format, t, zone),
@@ -3596,39 +3740,48 @@ function sprigDatetime(clock = () => /* @__PURE__ */ new Date()) {
3596
3740
  "string",
3597
3741
  "value",
3598
3742
  "string"
3599
- ]
3743
+ ],
3744
+ arity: { kind: "exact" }
3600
3745
  },
3601
3746
  dateModify: {
3602
3747
  fn: (dur, t) => dateModify(dur, t),
3603
- argTypes: ["string", "value"]
3748
+ argTypes: ["string", "value"],
3749
+ arity: { kind: "exact" }
3604
3750
  },
3605
3751
  htmlDate: {
3606
3752
  fn: (t) => htmlDate(t),
3607
- argTypes: ["value"]
3753
+ argTypes: ["value"],
3754
+ arity: { kind: "exact" }
3608
3755
  },
3609
3756
  htmlDateInZone: {
3610
3757
  fn: (t, zone) => htmlDateInZone(t, zone),
3611
- argTypes: ["value", "string"]
3758
+ argTypes: ["value", "string"],
3759
+ arity: { kind: "exact" }
3612
3760
  },
3613
3761
  duration: {
3614
3762
  fn: (sec) => duration(sec),
3615
- argTypes: ["value"]
3763
+ argTypes: ["value"],
3764
+ arity: { kind: "exact" }
3616
3765
  },
3617
3766
  durationRound: {
3618
3767
  fn: (d) => durationRound(d),
3619
- argTypes: ["value"]
3768
+ argTypes: ["value"],
3769
+ arity: { kind: "exact" }
3620
3770
  },
3621
3771
  toDate: {
3622
3772
  fn: (format, s) => toDate(format, s),
3623
- argTypes: ["string", "string"]
3773
+ argTypes: ["string", "string"],
3774
+ arity: { kind: "exact" }
3624
3775
  },
3625
3776
  ago: {
3626
3777
  fn: (t) => ago(t, clock),
3627
- argTypes: ["value"]
3778
+ argTypes: ["value"],
3779
+ arity: { kind: "exact" }
3628
3780
  },
3629
3781
  unixEpoch: {
3630
3782
  fn: (t) => unixEpoch(t),
3631
- argTypes: ["value"]
3783
+ argTypes: ["value"],
3784
+ arity: { kind: "exact" }
3632
3785
  }
3633
3786
  };
3634
3787
  }
@@ -3727,15 +3880,18 @@ function sprigDefaults() {
3727
3880
  return {
3728
3881
  default: {
3729
3882
  fn: (a, b) => defaultFn(a, b),
3730
- argTypes: ["value", "truthy"]
3883
+ argTypes: ["value", "truthy"],
3884
+ arity: { kind: "variadic" }
3731
3885
  },
3732
3886
  empty: {
3733
3887
  fn: (v) => empty(v),
3734
- argTypes: ["truthy"]
3888
+ argTypes: ["truthy"],
3889
+ arity: { kind: "exact" }
3735
3890
  },
3736
3891
  coalesce: {
3737
3892
  fn: (...vs) => coalesce(...vs),
3738
- argTypes: ["truthy"]
3893
+ argTypes: ["truthy"],
3894
+ arity: { kind: "variadic" }
3739
3895
  },
3740
3896
  ternary: {
3741
3897
  fn: (a, b, c) => ternary(a, b, c),
@@ -3743,20 +3899,24 @@ function sprigDefaults() {
3743
3899
  "value",
3744
3900
  "value",
3745
3901
  "truthy"
3746
- ]
3902
+ ],
3903
+ arity: { kind: "exact" }
3747
3904
  },
3748
3905
  fromJson: {
3749
3906
  fn: (s) => fromJson(s),
3750
- argTypes: ["string"]
3907
+ argTypes: ["string"],
3908
+ arity: { kind: "exact" }
3751
3909
  },
3752
3910
  toJson: {
3753
3911
  fn: (v) => toJson(v),
3754
3912
  argTypes: ["serializable"],
3913
+ arity: { kind: "exact" },
3755
3914
  returnType: "string"
3756
3915
  },
3757
3916
  toPrettyJson: {
3758
3917
  fn: (v) => toPrettyJson(v),
3759
3918
  argTypes: ["serializable"],
3919
+ arity: { kind: "exact" },
3760
3920
  returnType: "string"
3761
3921
  }
3762
3922
  };
@@ -3946,11 +4106,15 @@ function sprigDicts() {
3946
4106
  dict: {
3947
4107
  fn: (...kv) => dict(...kv),
3948
4108
  argTypes: ["string", "value"],
3949
- argTypePattern: "alternating"
4109
+ arity: {
4110
+ kind: "alternating",
4111
+ minimum: 0
4112
+ }
3950
4113
  },
3951
4114
  get: {
3952
4115
  fn: (d, k) => get(d, k),
3953
- argTypes: ["dict", "string"]
4116
+ argTypes: ["dict", "string"],
4117
+ arity: { kind: "exact" }
3954
4118
  },
3955
4119
  set: {
3956
4120
  fn: (d, k, v) => set(d, k, v),
@@ -3958,43 +4122,53 @@ function sprigDicts() {
3958
4122
  "dict",
3959
4123
  "string",
3960
4124
  "value"
3961
- ]
4125
+ ],
4126
+ arity: { kind: "exact" }
3962
4127
  },
3963
4128
  unset: {
3964
4129
  fn: (d, k) => unset(d, k),
3965
- argTypes: ["dict", "string"]
4130
+ argTypes: ["dict", "string"],
4131
+ arity: { kind: "exact" }
3966
4132
  },
3967
4133
  keys: {
3968
4134
  fn: (d) => keys(d),
3969
- argTypes: ["dict"]
4135
+ argTypes: ["dict"],
4136
+ arity: { kind: "variadic" }
3970
4137
  },
3971
4138
  values: {
3972
4139
  fn: (d) => values(d),
3973
- argTypes: ["dict"]
4140
+ argTypes: ["dict"],
4141
+ arity: { kind: "exact" }
3974
4142
  },
3975
4143
  pluck: {
3976
4144
  fn: (k, ...d) => pluck(k, ...d),
3977
- argTypes: ["string", "dict"]
4145
+ argTypes: ["string", "dict"],
4146
+ arity: { kind: "variadic" }
3978
4147
  },
3979
4148
  pick: {
3980
4149
  fn: (d, ...k) => pick(d, ...k),
3981
- argTypes: ["dict", "string"]
4150
+ argTypes: ["dict", "string"],
4151
+ arity: { kind: "variadic" }
3982
4152
  },
3983
4153
  omit: {
3984
4154
  fn: (d, ...k) => omit(d, ...k),
3985
- argTypes: ["dict", "string"]
4155
+ argTypes: ["dict", "string"],
4156
+ arity: { kind: "variadic" }
3986
4157
  },
3987
4158
  hasKey: {
3988
4159
  fn: (d, k) => hasKey(d, k),
3989
- argTypes: ["dict", "string"]
4160
+ argTypes: ["dict", "string"],
4161
+ arity: { kind: "exact" }
3990
4162
  },
3991
4163
  merge: {
3992
4164
  fn: (d, ...s) => merge(d, ...s),
3993
- argTypes: ["dict"]
4165
+ argTypes: ["dict", "dict"],
4166
+ arity: { kind: "variadic" }
3994
4167
  },
3995
4168
  mergeOverwrite: {
3996
4169
  fn: (d, ...s) => mergeOverwrite(d, ...s),
3997
- argTypes: ["dict"]
4170
+ argTypes: ["dict", "dict"],
4171
+ arity: { kind: "variadic" }
3998
4172
  }
3999
4173
  };
4000
4174
  }
@@ -4019,7 +4193,8 @@ function fail(msg) {
4019
4193
  function sprigFlow() {
4020
4194
  return { fail: {
4021
4195
  fn: (msg) => fail(msg),
4022
- argTypes: ["string"]
4196
+ argTypes: ["string"],
4197
+ arity: { kind: "exact" }
4023
4198
  } };
4024
4199
  }
4025
4200
  //#endregion
@@ -4151,39 +4326,48 @@ function sprigHash() {
4151
4326
  return {
4152
4327
  b64enc: {
4153
4328
  fn: (s) => b64enc(s),
4154
- argTypes: ["string"]
4329
+ argTypes: ["string"],
4330
+ arity: { kind: "exact" }
4155
4331
  },
4156
4332
  b64dec: {
4157
4333
  fn: (s) => b64dec(s),
4158
- argTypes: ["string"]
4334
+ argTypes: ["string"],
4335
+ arity: { kind: "exact" }
4159
4336
  },
4160
4337
  b32enc: {
4161
4338
  fn: (s) => b32enc(s),
4162
- argTypes: ["string"]
4339
+ argTypes: ["string"],
4340
+ arity: { kind: "exact" }
4163
4341
  },
4164
4342
  b32dec: {
4165
4343
  fn: (s) => b32dec(s),
4166
- argTypes: ["string"]
4344
+ argTypes: ["string"],
4345
+ arity: { kind: "exact" }
4167
4346
  },
4168
4347
  sha1sum: {
4169
4348
  fn: (s) => sha1sum(s),
4170
- argTypes: ["string"]
4349
+ argTypes: ["string"],
4350
+ arity: { kind: "exact" }
4171
4351
  },
4172
4352
  sha256sum: {
4173
4353
  fn: (s) => sha256sum(s),
4174
- argTypes: ["string"]
4354
+ argTypes: ["string"],
4355
+ arity: { kind: "exact" }
4175
4356
  },
4176
4357
  sha512sum: {
4177
4358
  fn: (s) => sha512sum(s),
4178
- argTypes: ["string"]
4359
+ argTypes: ["string"],
4360
+ arity: { kind: "exact" }
4179
4361
  },
4180
4362
  adler32sum: {
4181
4363
  fn: (s) => adler32sum(s),
4182
- argTypes: ["string"]
4364
+ argTypes: ["string"],
4365
+ arity: { kind: "exact" }
4183
4366
  },
4184
4367
  uuidv4: {
4185
4368
  fn: () => uuidv4(),
4186
- argTypes: []
4369
+ argTypes: [],
4370
+ arity: { kind: "exact" }
4187
4371
  }
4188
4372
  };
4189
4373
  }
@@ -4431,97 +4615,115 @@ function sprigLists() {
4431
4615
  return {
4432
4616
  list: {
4433
4617
  fn: (...a) => list(...a),
4434
- argTypes: ["value"]
4618
+ argTypes: ["value"],
4619
+ arity: { kind: "variadic" }
4435
4620
  },
4436
4621
  first: {
4437
4622
  fn: (l) => first(l),
4438
- argTypes: ["list"]
4623
+ argTypes: ["list"],
4624
+ arity: { kind: "exact" }
4439
4625
  },
4440
4626
  last: {
4441
4627
  fn: (l) => last(l),
4442
- argTypes: ["list"]
4628
+ argTypes: ["list"],
4629
+ arity: { kind: "exact" }
4443
4630
  },
4444
4631
  rest: {
4445
4632
  fn: (l) => rest(l),
4446
- argTypes: ["list"]
4633
+ argTypes: ["list"],
4634
+ arity: { kind: "exact" }
4447
4635
  },
4448
4636
  initial: {
4449
4637
  fn: (l) => initial(l),
4450
- argTypes: ["list"]
4638
+ argTypes: ["list"],
4639
+ arity: { kind: "exact" }
4451
4640
  },
4452
4641
  len: {
4453
4642
  fn: (l) => len(l),
4454
- argTypes: ["sized"]
4643
+ argTypes: ["sized"],
4644
+ arity: { kind: "exact" }
4455
4645
  },
4456
4646
  reverse: {
4457
4647
  fn: (l) => reverse(l),
4458
- argTypes: ["list"]
4648
+ argTypes: ["list"],
4649
+ arity: { kind: "exact" }
4459
4650
  },
4460
4651
  uniq: {
4461
4652
  fn: (l) => uniq(l),
4462
- argTypes: ["list"]
4653
+ argTypes: ["list"],
4654
+ arity: { kind: "exact" }
4463
4655
  },
4464
4656
  without: {
4465
4657
  fn: (l, ...e) => without(l, ...e),
4466
- argTypes: ["list", "value"]
4658
+ argTypes: ["list", "value"],
4659
+ arity: { kind: "variadic" }
4467
4660
  },
4468
4661
  has: {
4469
4662
  fn: (i, l) => has(i, l),
4470
- argTypes: ["value", "list"]
4663
+ argTypes: ["value", "list"],
4664
+ arity: { kind: "exact" }
4471
4665
  },
4472
4666
  compact: {
4473
4667
  fn: (l) => compact(l),
4474
- argTypes: ["list"]
4668
+ argTypes: ["list"],
4669
+ arity: { kind: "exact" }
4475
4670
  },
4476
4671
  slice: {
4477
4672
  fn: (l, i, j) => slice(l, i, j),
4478
- argTypes: [
4479
- "list",
4480
- "int",
4481
- "int"
4482
- ]
4673
+ argTypes: ["list", "truncating-int"],
4674
+ arity: { kind: "variadic" }
4483
4675
  },
4484
4676
  concat: {
4485
4677
  fn: (...l) => concat(...l),
4486
- argTypes: ["list"]
4678
+ argTypes: ["list"],
4679
+ arity: { kind: "variadic" }
4487
4680
  },
4488
4681
  chunk: {
4489
4682
  fn: (s, l) => chunk(s, l),
4490
- argTypes: ["int", "list"]
4683
+ argTypes: ["int", "list"],
4684
+ arity: { kind: "exact" }
4491
4685
  },
4492
4686
  prepend: {
4493
4687
  fn: (l, i) => prepend(l, i),
4494
- argTypes: ["list", "value"]
4688
+ argTypes: ["list", "value"],
4689
+ arity: { kind: "exact" }
4495
4690
  },
4496
4691
  append: {
4497
4692
  fn: (l, i) => append(l, i),
4498
- argTypes: ["list", "value"]
4693
+ argTypes: ["list", "value"],
4694
+ arity: { kind: "exact" }
4499
4695
  },
4500
4696
  sortAlpha: {
4501
4697
  fn: (l) => sortAlpha(l),
4502
4698
  argTypes: ["list"],
4699
+ arity: { kind: "exact" },
4503
4700
  returnType: "list"
4504
4701
  },
4505
4702
  push: {
4506
4703
  fn: (l, i) => append(l, i),
4507
- argTypes: ["list", "value"]
4704
+ argTypes: ["list", "value"],
4705
+ arity: { kind: "exact" }
4508
4706
  },
4509
4707
  tuple: {
4510
4708
  fn: (...a) => list(...a),
4511
- argTypes: ["value"]
4709
+ argTypes: ["value"],
4710
+ arity: { kind: "variadic" }
4512
4711
  },
4513
4712
  dig: {
4514
4713
  fn: (...a) => dig(...a),
4515
- argTypes: ["value"]
4714
+ argTypes: ["value"],
4715
+ arity: { kind: "variadic" }
4516
4716
  },
4517
4717
  all: {
4518
4718
  fn: (...a) => all(...a),
4519
4719
  argTypes: ["truthy"],
4720
+ arity: { kind: "variadic" },
4520
4721
  returnType: "bool"
4521
4722
  },
4522
4723
  any: {
4523
4724
  fn: (...a) => any(...a),
4524
4725
  argTypes: ["truthy"],
4726
+ arity: { kind: "variadic" },
4525
4727
  returnType: "bool"
4526
4728
  }
4527
4729
  };
@@ -4562,8 +4764,8 @@ function div(a, b) {
4562
4764
  }
4563
4765
  //#endregion
4564
4766
  //#region src/sprig/math/divf.ts
4565
- function divf(a, b) {
4566
- return a / b;
4767
+ function divf(a, ...rest) {
4768
+ return rest.reduce((acc, v) => acc / v, a);
4567
4769
  }
4568
4770
  //#endregion
4569
4771
  //#region src/sprig/math/floor.ts
@@ -4698,8 +4900,8 @@ function sub(a, b) {
4698
4900
  }
4699
4901
  //#endregion
4700
4902
  //#region src/sprig/math/subf.ts
4701
- function subf(a, b) {
4702
- return a - b;
4903
+ function subf(a, ...rest) {
4904
+ return rest.reduce((acc, v) => acc - v, a);
4703
4905
  }
4704
4906
  //#endregion
4705
4907
  //#region src/sprig/math/until.ts
@@ -4718,88 +4920,113 @@ function sprigMath() {
4718
4920
  return {
4719
4921
  add: {
4720
4922
  fn: (...a) => add(...a),
4721
- argTypes: ["int"]
4923
+ argTypes: ["truncating-int"],
4924
+ arity: { kind: "variadic" }
4722
4925
  },
4723
4926
  sub: {
4724
4927
  fn: (a, b) => sub(a, b),
4725
- argTypes: ["int", "int"]
4928
+ argTypes: ["truncating-int", "truncating-int"],
4929
+ arity: { kind: "exact" }
4726
4930
  },
4727
4931
  mul: {
4728
4932
  fn: (...a) => mul(...a),
4729
- argTypes: ["int"]
4933
+ argTypes: ["truncating-int", "truncating-int"],
4934
+ arity: { kind: "variadic" }
4730
4935
  },
4731
4936
  div: {
4732
4937
  fn: (a, b) => div(a, b),
4733
- argTypes: ["int", "int"]
4938
+ argTypes: ["truncating-int", "truncating-int"],
4939
+ arity: { kind: "exact" }
4734
4940
  },
4735
4941
  mod: {
4736
4942
  fn: (a, b) => mod(a, b),
4737
- argTypes: ["int", "int"]
4943
+ argTypes: ["truncating-int", "truncating-int"],
4944
+ arity: { kind: "exact" }
4738
4945
  },
4739
4946
  min: {
4740
4947
  fn: (...a) => min(...a),
4741
- argTypes: ["int"]
4948
+ argTypes: ["truncating-int", "truncating-int"],
4949
+ arity: { kind: "variadic" }
4742
4950
  },
4743
4951
  max: {
4744
4952
  fn: (...a) => max(...a),
4745
- argTypes: ["int"]
4953
+ argTypes: ["truncating-int", "truncating-int"],
4954
+ arity: { kind: "variadic" }
4746
4955
  },
4747
4956
  floor: {
4748
4957
  fn: (a) => floor(a),
4749
- argTypes: ["float"]
4958
+ argTypes: ["float"],
4959
+ arity: { kind: "exact" }
4750
4960
  },
4751
4961
  ceil: {
4752
4962
  fn: (a) => ceil(a),
4753
- argTypes: ["float"]
4963
+ argTypes: ["float"],
4964
+ arity: { kind: "exact" }
4754
4965
  },
4755
4966
  round: {
4756
4967
  fn: (a, p) => round(a, p),
4757
- argTypes: ["float", "int"]
4968
+ argTypes: [
4969
+ "float",
4970
+ "int",
4971
+ "float"
4972
+ ],
4973
+ arity: { kind: "variadic" }
4758
4974
  },
4759
4975
  addf: {
4760
4976
  fn: (...a) => addf(...a),
4761
- argTypes: ["float"]
4977
+ argTypes: ["float"],
4978
+ arity: { kind: "variadic" }
4762
4979
  },
4763
4980
  subf: {
4764
- fn: (a, b) => subf(a, b),
4765
- argTypes: ["float", "float"]
4981
+ fn: (a, ...rest) => subf(a, ...rest),
4982
+ argTypes: ["float", "float"],
4983
+ arity: { kind: "variadic" }
4766
4984
  },
4767
4985
  mulf: {
4768
4986
  fn: (...a) => mulf(...a),
4769
- argTypes: ["float"]
4987
+ argTypes: ["float", "float"],
4988
+ arity: { kind: "variadic" }
4770
4989
  },
4771
4990
  divf: {
4772
- fn: (a, b) => divf(a, b),
4773
- argTypes: ["float", "float"]
4991
+ fn: (a, ...rest) => divf(a, ...rest),
4992
+ argTypes: ["float", "float"],
4993
+ arity: { kind: "variadic" }
4774
4994
  },
4775
4995
  add1: {
4776
4996
  fn: (n) => add1(n),
4777
- argTypes: ["int"]
4997
+ argTypes: ["truncating-int"],
4998
+ arity: { kind: "exact" }
4778
4999
  },
4779
5000
  add1f: {
4780
5001
  fn: (n) => add1f(n),
4781
- argTypes: ["float"]
5002
+ argTypes: ["float"],
5003
+ arity: { kind: "exact" }
4782
5004
  },
4783
5005
  maxf: {
4784
5006
  fn: (...a) => maxf(...a),
4785
- argTypes: ["float"]
5007
+ argTypes: ["float", "float"],
5008
+ arity: { kind: "variadic" }
4786
5009
  },
4787
5010
  minf: {
4788
5011
  fn: (...a) => minf(...a),
4789
- argTypes: ["float"]
5012
+ argTypes: ["float", "float"],
5013
+ arity: { kind: "variadic" }
4790
5014
  },
4791
5015
  biggest: {
4792
5016
  fn: (...a) => max(...a),
4793
- argTypes: ["int"]
5017
+ argTypes: ["truncating-int", "truncating-int"],
5018
+ arity: { kind: "variadic" }
4794
5019
  },
4795
5020
  seq: {
4796
5021
  fn: (...a) => seq(...a),
4797
5022
  argTypes: ["int"],
5023
+ arity: { kind: "variadic" },
4798
5024
  returnType: "string"
4799
5025
  },
4800
5026
  until: {
4801
5027
  fn: (n) => until(n),
4802
5028
  argTypes: ["int"],
5029
+ arity: { kind: "exact" },
4803
5030
  returnType: "list"
4804
5031
  },
4805
5032
  untilStep: {
@@ -4809,6 +5036,7 @@ function sprigMath() {
4809
5036
  "int",
4810
5037
  "int"
4811
5038
  ],
5039
+ arity: { kind: "exact" },
4812
5040
  returnType: "list"
4813
5041
  }
4814
5042
  };
@@ -4883,6 +5111,51 @@ function randNumeric(n, random) {
4883
5111
  return randString(n, NUMERIC, random);
4884
5112
  }
4885
5113
  //#endregion
5114
+ //#region src/sprig/strings/runes.ts
5115
+ /**
5116
+ * The rune-level string seam: the operations Go gets for free on its
5117
+ * strings and JS does not.
5118
+ *
5119
+ * [LAW:parse-dont-validate] Go walks a string as runes; JS strings are
5120
+ * UTF-16, so a character-position operation done on the raw string
5121
+ * (`s.slice(0, 1)`, `s[i]`, `s.length`, `s.split("")`) splits an astral
5122
+ * character in half and yields a lone surrogate — `trunc 1 "𐍈x"`
5123
+ * returned "\ud800", for which `isWellFormed()` is false. Decoding once,
5124
+ * up front, is the checkpoint: every element of the returned array is a
5125
+ * whole character, so an index into the middle of one is unrepresentable
5126
+ * and no slice of it can produce a lone surrogate. Callers re-encode
5127
+ * with `.join("")`.
5128
+ *
5129
+ * Code points deliberately — not UTF-16 units, and not grapheme
5130
+ * clusters. Grapheme clustering would hold `"é"` together, a third
5131
+ * behaviour matching neither Go nor this engine.
5132
+ *
5133
+ * For splitting this buys exact Go parity. For slicing it buys
5134
+ * well-formedness instead: Go slices `trunc`/`substr`/`abbrev` by BYTE,
5135
+ * so `trunc 1 "𐍈x"` is the single invalid byte `\xf0` there and the
5136
+ * whole `𐍈` here. That residual divergence is intended and tracked
5137
+ * separately; a rune is the closest well-formed unit to Go's byte.
5138
+ */
5139
+ /**
5140
+ * Decode `s` to its code points. The array is freshly built per call,
5141
+ * so callers may permute it in place (`shuffle` does).
5142
+ */
5143
+ function runes(s) {
5144
+ return Array.from(s);
5145
+ }
5146
+ /**
5147
+ * `strings.Split(s, sep)` — an empty separator splits between runes,
5148
+ * where JS's `s.split("")` splits between UTF-16 units and shears
5149
+ * astral characters into lone surrogates.
5150
+ *
5151
+ * [LAW:single-enforcer] `split`, `splitList`, `splitn`, and `replace`
5152
+ * all reach for Go's split; the empty-separator rule lives here so the
5153
+ * four cannot drift on it.
5154
+ */
5155
+ function goSplit(sep, s) {
5156
+ return sep === "" ? runes(s) : s.split(sep);
5157
+ }
5158
+ //#endregion
4886
5159
  //#region src/sprig/random/shuffle.ts
4887
5160
  /**
4888
5161
  * `shuffle s` — returns a randomly permuted copy of `s`.
@@ -4890,14 +5163,14 @@ function randNumeric(n, random) {
4890
5163
  * Operates on Unicode code points (matching Go sprig's rune-level Fisher-Yates).
4891
5164
  */
4892
5165
  function shuffle(s, random) {
4893
- const runes = [...s];
4894
- for (let i = runes.length - 1; i > 0; i--) {
5166
+ const cps = runes(s);
5167
+ for (let i = cps.length - 1; i > 0; i--) {
4895
5168
  const j = Math.floor(clampRandom(random()) * (i + 1));
4896
- const tmp = runes[i];
4897
- runes[i] = runes[j];
4898
- runes[j] = tmp;
5169
+ const tmp = cps[i];
5170
+ cps[i] = cps[j];
5171
+ cps[j] = tmp;
4899
5172
  }
4900
- return runes.join("");
5173
+ return cps.join("");
4901
5174
  }
4902
5175
  //#endregion
4903
5176
  //#region src/sprig/random/index.ts
@@ -4911,27 +5184,33 @@ function sprigRandom(random = Math.random) {
4911
5184
  return {
4912
5185
  randInt: {
4913
5186
  fn: (min, max) => randInt(min, max, random),
4914
- argTypes: ["int", "int"]
5187
+ argTypes: ["int", "int"],
5188
+ arity: { kind: "exact" }
4915
5189
  },
4916
5190
  randAlpha: {
4917
5191
  fn: (n) => randAlpha(n, random),
4918
- argTypes: ["int"]
5192
+ argTypes: ["int"],
5193
+ arity: { kind: "exact" }
4919
5194
  },
4920
5195
  randAlphaNum: {
4921
5196
  fn: (n) => randAlphaNum(n, random),
4922
- argTypes: ["int"]
5197
+ argTypes: ["int"],
5198
+ arity: { kind: "exact" }
4923
5199
  },
4924
5200
  randNumeric: {
4925
5201
  fn: (n) => randNumeric(n, random),
4926
- argTypes: ["int"]
5202
+ argTypes: ["int"],
5203
+ arity: { kind: "exact" }
4927
5204
  },
4928
5205
  randAscii: {
4929
5206
  fn: (n) => randAscii(n, random),
4930
- argTypes: ["int"]
5207
+ argTypes: ["int"],
5208
+ arity: { kind: "exact" }
4931
5209
  },
4932
5210
  shuffle: {
4933
5211
  fn: (s) => shuffle(s, random),
4934
- argTypes: ["string"]
5212
+ argTypes: ["string"],
5213
+ arity: { kind: "exact" }
4935
5214
  }
4936
5215
  };
4937
5216
  }
@@ -4997,11 +5276,13 @@ function sprigRegex() {
4997
5276
  return {
4998
5277
  regexMatch: {
4999
5278
  fn: (p, s) => regexMatch(p, s),
5000
- argTypes: ["string", "string"]
5279
+ argTypes: ["string", "string"],
5280
+ arity: { kind: "exact" }
5001
5281
  },
5002
5282
  regexFind: {
5003
5283
  fn: (p, s) => regexFind(p, s),
5004
- argTypes: ["string", "string"]
5284
+ argTypes: ["string", "string"],
5285
+ arity: { kind: "exact" }
5005
5286
  },
5006
5287
  regexFindAll: {
5007
5288
  fn: (p, s, n) => regexFindAll(p, s, n),
@@ -5009,7 +5290,8 @@ function sprigRegex() {
5009
5290
  "string",
5010
5291
  "string",
5011
5292
  "int"
5012
- ]
5293
+ ],
5294
+ arity: { kind: "exact" }
5013
5295
  },
5014
5296
  regexReplaceAll: {
5015
5297
  fn: (p, s, r) => regexReplaceAll(p, s, r),
@@ -5017,7 +5299,8 @@ function sprigRegex() {
5017
5299
  "string",
5018
5300
  "string",
5019
5301
  "string"
5020
- ]
5302
+ ],
5303
+ arity: { kind: "exact" }
5021
5304
  },
5022
5305
  regexReplaceAllLiteral: {
5023
5306
  fn: (p, s, r) => regexReplaceAllLiteral(p, s, r),
@@ -5025,7 +5308,8 @@ function sprigRegex() {
5025
5308
  "string",
5026
5309
  "string",
5027
5310
  "string"
5028
- ]
5311
+ ],
5312
+ arity: { kind: "exact" }
5029
5313
  },
5030
5314
  regexSplit: {
5031
5315
  fn: (p, s, n) => regexSplit(p, s, n),
@@ -5033,7 +5317,8 @@ function sprigRegex() {
5033
5317
  "string",
5034
5318
  "string",
5035
5319
  "int"
5036
- ]
5320
+ ],
5321
+ arity: { kind: "exact" }
5037
5322
  }
5038
5323
  };
5039
5324
  }
@@ -5221,11 +5506,13 @@ function sprigSemver() {
5221
5506
  return {
5222
5507
  semver: {
5223
5508
  fn: (s) => semver(s),
5224
- argTypes: ["string"]
5509
+ argTypes: ["string"],
5510
+ arity: { kind: "exact" }
5225
5511
  },
5226
5512
  semverCompare: {
5227
5513
  fn: (constraint, version) => semverCompare(constraint, version),
5228
5514
  argTypes: ["string", "string"],
5515
+ arity: { kind: "exact" },
5229
5516
  returnType: "bool"
5230
5517
  }
5231
5518
  };
@@ -5237,11 +5524,15 @@ function sprigSemver() {
5237
5524
  * with `...` when truncation occurs. Width must be at least 4 to fit
5238
5525
  * the ellipsis; smaller widths return `s` unchanged (Go sprig
5239
5526
  * behavior).
5527
+ *
5528
+ * `width` counts code points, so the cut never splits an astral
5529
+ * character — see `runes.js` for why that differs from Go's bytes.
5240
5530
  */
5241
5531
  function abbrev(width, s) {
5242
- if (s.length <= width) return s;
5532
+ const cps = runes(s);
5533
+ if (cps.length <= width) return s;
5243
5534
  if (width < 4) return s;
5244
- return `${s.slice(0, width - 3)}...`;
5535
+ return `${cps.slice(0, width - 3).join("")}...`;
5245
5536
  }
5246
5537
  //#endregion
5247
5538
  //#region src/sprig/strings/abbrevboth.ts
@@ -5250,11 +5541,15 @@ function abbrev(width, s) {
5250
5541
  * around the visible window. Mirrors Go sprig's abbrevboth semantics:
5251
5542
  * left/right specify offsets (0-indexed); the result has `...` around
5252
5543
  * a slice taken between them.
5544
+ *
5545
+ * Offsets count code points, so the window never splits an astral
5546
+ * character — see `runes.js` for why that differs from Go's bytes.
5253
5547
  */
5254
5548
  function abbrevboth(left, right, s) {
5255
- if (right < 7 || s.length < right) return s;
5256
- if (left < 0 || left > s.length - right + 1) return s;
5257
- return `...${s.slice(left, left + right - 6)}...`;
5549
+ const cps = runes(s);
5550
+ if (right < 7 || cps.length < right) return s;
5551
+ if (left < 0 || left > cps.length - right + 1) return s;
5552
+ return `...${cps.slice(left, left + right - 6).join("")}...`;
5258
5553
  }
5259
5554
  //#endregion
5260
5555
  //#region src/sprig/strings/caseUtils.ts
@@ -5270,14 +5565,13 @@ function abbrevboth(left, right, s) {
5270
5565
  *
5271
5566
  * ## Port notes
5272
5567
  *
5273
- * Go iterates UTF-8 bytes via `utf8.DecodeRuneInString`. JS strings
5274
- * are UTF-16; iterating with `for…of` (or `Array.from(s)`) yields
5275
- * code points the same way Go yields runes, so the rune-level logic
5276
- * carries over. We intentionally do NOT attempt to mirror xstrings'
5277
- * `isAlphabet` CJK-exclusion table — for ASCII/Latin templates, which
5278
- * are what real users feed sprig, the simpler `isLetter` test
5279
- * matches Go's output. The fixture suite pins the cases consumers
5280
- * actually exercise.
5568
+ * Go iterates UTF-8 bytes via `utf8.DecodeRuneInString`. The `runes`
5569
+ * seam yields code points the same way Go yields runes, so the
5570
+ * rune-level logic carries over. We intentionally do NOT attempt to
5571
+ * mirror xstrings' `isAlphabet` CJK-exclusion table — for ASCII/Latin
5572
+ * templates, which are what real users feed sprig, the simpler
5573
+ * `isLetter` test matches Go's output. The fixture suite pins the
5574
+ * cases consumers actually exercise.
5281
5575
  *
5282
5576
  * `unicode.ToTitle` from Go has no JS equivalent for ASCII letters
5283
5577
  * (Title and Upper coincide for code points without a separate Title
@@ -5447,7 +5741,7 @@ function pushLowered(buf, word, connector) {
5447
5741
  */
5448
5742
  function camelCaseToLowerCase(s, connector) {
5449
5743
  if (s.length === 0) return "";
5450
- const cps = Array.from(s);
5744
+ const cps = runes(s);
5451
5745
  const buf = [];
5452
5746
  let { word, next: i } = nextWord(cps, 0);
5453
5747
  while (i < cps.length) {
@@ -5501,7 +5795,7 @@ function camelCaseToLowerCase(s, connector) {
5501
5795
  */
5502
5796
  function camelCase(s) {
5503
5797
  if (s.length === 0) return "";
5504
- const cps = Array.from(s);
5798
+ const cps = runes(s);
5505
5799
  const buf = [];
5506
5800
  let i = 0;
5507
5801
  let r0 = "";
@@ -5690,9 +5984,16 @@ function repeat(n, s) {
5690
5984
  }
5691
5985
  //#endregion
5692
5986
  //#region src/sprig/strings/replace.ts
5693
- /** Replaces ALL occurrences of `old` with `nw` in `s` (Go's strings.Replace n=-1). */
5987
+ /**
5988
+ * Replaces ALL occurrences of `old` with `nw` in `s` (Go's strings.Replace n=-1).
5989
+ *
5990
+ * An empty `old` splits between runes rather than UTF-16 units, so the
5991
+ * replacement is never inserted inside an astral character. Where Go
5992
+ * additionally inserts `nw` at both ends (`-a-b-c-` to this port's
5993
+ * `a-b-c`) it still differs — a separate, non-Unicode divergence.
5994
+ */
5694
5995
  function replace(old, nw, s) {
5695
- return s.split(old).join(nw);
5996
+ return goSplit(old, s).join(nw);
5696
5997
  }
5697
5998
  //#endregion
5698
5999
  //#region src/sprig/strings/snakecase.ts
@@ -5714,7 +6015,7 @@ function snakecase(s) {
5714
6015
  * split "/" "a/b/c" → { _0: "a", _1: "b", _2: "c" }
5715
6016
  */
5716
6017
  function split(sep, s) {
5717
- const parts = s.split(sep);
6018
+ const parts = goSplit(sep, s);
5718
6019
  const out = {};
5719
6020
  for (let i = 0; i < parts.length; i++) out[`_${i}`] = parts[i];
5720
6021
  return out;
@@ -5722,7 +6023,7 @@ function split(sep, s) {
5722
6023
  //#endregion
5723
6024
  //#region src/sprig/strings/splitList.ts
5724
6025
  function splitList(sep, s) {
5725
- return s.split(sep);
6026
+ return goSplit(sep, s);
5726
6027
  }
5727
6028
  //#endregion
5728
6029
  //#region src/sprig/strings/splitn.ts
@@ -5753,7 +6054,7 @@ function splitn(sep, n, s) {
5753
6054
  }
5754
6055
  function goSplitN(s, sep, n) {
5755
6056
  if (n === 0) return [];
5756
- const all = sep === "" ? Array.from(s) : s.split(sep);
6057
+ const all = goSplit(sep, s);
5757
6058
  if (n < 0 || all.length <= n) return all;
5758
6059
  const head = all.slice(0, n - 1);
5759
6060
  const tail = all.slice(n - 1).join(sep);
@@ -5770,13 +6071,17 @@ function squote(...args) {
5770
6071
  /**
5771
6072
  * `substr i j s` — substring [i, j) with out-of-range clamping.
5772
6073
  * Matches Go sprig's behavior of clamping rather than throwing:
5773
- * negative `end` (and any `end > s.length`) clamps to `s.length`;
5774
- * `start` clamps to `[0, end]`.
6074
+ * negative `end` (and any `end` past the last char) clamps to the
6075
+ * string's length; `start` clamps to `[0, end]`.
6076
+ *
6077
+ * Offsets count code points, so a window never splits an astral
6078
+ * character — see `runes.js` for why that differs from Go's bytes.
5775
6079
  */
5776
6080
  function substr(start, end, s) {
5777
- const j = end < 0 || end > s.length ? s.length : end;
6081
+ const cps = runes(s);
6082
+ const j = end < 0 || end > cps.length ? cps.length : end;
5778
6083
  const i = Math.min(Math.max(0, start), j);
5779
- return s.slice(i, j);
6084
+ return cps.slice(i, j).join("");
5780
6085
  }
5781
6086
  //#endregion
5782
6087
  //#region src/sprig/strings/swapcase.ts
@@ -5819,15 +6124,23 @@ function trim(s) {
5819
6124
  }
5820
6125
  //#endregion
5821
6126
  //#region src/sprig/strings/trimAll.ts
5822
- /** `trimAll cutset s` — strip leading + trailing chars in `cutset`. */
6127
+ /**
6128
+ * `trimAll cutset s` — strip leading + trailing chars in `cutset`.
6129
+ *
6130
+ * Both sides are compared as code points, matching Go's `strings.Trim`,
6131
+ * which decodes the cutset as runes. Scanning `s` by UTF-16 unit
6132
+ * compared half an astral character against whole ones, so an astral
6133
+ * cutset char never matched and the string came back untrimmed.
6134
+ */
5823
6135
  function trimAll(cutset, s) {
5824
6136
  if (cutset.length === 0) return s;
5825
- const set = new Set([...cutset]);
6137
+ const set = new Set(runes(cutset));
6138
+ const cps = runes(s);
5826
6139
  let start = 0;
5827
- while (start < s.length && set.has(s[start])) start += 1;
5828
- let end = s.length;
5829
- while (end > start && set.has(s[end - 1])) end -= 1;
5830
- return s.slice(start, end);
6140
+ while (start < cps.length && set.has(cps[start])) start += 1;
6141
+ let end = cps.length;
6142
+ while (end > start && set.has(cps[end - 1])) end -= 1;
6143
+ return cps.slice(start, end).join("");
5831
6144
  }
5832
6145
  //#endregion
5833
6146
  //#region src/sprig/strings/trimPrefix.ts
@@ -5844,13 +6157,17 @@ function trimSuffix(suffix, s) {
5844
6157
  /**
5845
6158
  * `trunc n s` — first `n` chars when n>=0; last |n| chars when n<0.
5846
6159
  * Strings shorter than |n| are returned unchanged. Matches Go sprig.
6160
+ *
6161
+ * `n` counts code points, so truncating never splits an astral
6162
+ * character — see `runes.js` for why that differs from Go's bytes.
5847
6163
  */
5848
6164
  function trunc(n, s) {
6165
+ const cps = runes(s);
5849
6166
  if (n < 0) {
5850
6167
  const k = -n;
5851
- return k >= s.length ? s : s.slice(s.length - k);
6168
+ return k >= cps.length ? s : cps.slice(cps.length - k).join("");
5852
6169
  }
5853
- return n >= s.length ? s : s.slice(0, n);
6170
+ return n >= cps.length ? s : cps.slice(0, n).join("");
5854
6171
  }
5855
6172
  //#endregion
5856
6173
  //#region src/sprig/strings/untitle.ts
@@ -5902,7 +6219,8 @@ function sprigStrings() {
5902
6219
  return {
5903
6220
  abbrev: {
5904
6221
  fn: (w, s) => abbrev(w, s),
5905
- argTypes: ["int", "string"]
6222
+ argTypes: ["int", "string"],
6223
+ arity: { kind: "exact" }
5906
6224
  },
5907
6225
  abbrevboth: {
5908
6226
  fn: (l, r, s) => abbrevboth(l, r, s),
@@ -5910,57 +6228,70 @@ function sprigStrings() {
5910
6228
  "int",
5911
6229
  "int",
5912
6230
  "string"
5913
- ]
6231
+ ],
6232
+ arity: { kind: "exact" }
5914
6233
  },
5915
6234
  camelcase: {
5916
6235
  fn: (s) => camelcase(s),
5917
6236
  argTypes: ["string"],
6237
+ arity: { kind: "exact" },
5918
6238
  returnType: "string"
5919
6239
  },
5920
6240
  cat: {
5921
6241
  fn: (...a) => cat(...a),
5922
- argTypes: ["string"]
6242
+ argTypes: ["string"],
6243
+ arity: { kind: "variadic" }
5923
6244
  },
5924
6245
  contains: {
5925
6246
  fn: (sub, s) => contains(sub, s),
5926
- argTypes: ["string", "string"]
6247
+ argTypes: ["string", "string"],
6248
+ arity: { kind: "exact" }
5927
6249
  },
5928
6250
  hasPrefix: {
5929
6251
  fn: (p, s) => hasPrefix(p, s),
5930
- argTypes: ["string", "string"]
6252
+ argTypes: ["string", "string"],
6253
+ arity: { kind: "exact" }
5931
6254
  },
5932
6255
  hasSuffix: {
5933
6256
  fn: (sf, s) => hasSuffix(sf, s),
5934
- argTypes: ["string", "string"]
6257
+ argTypes: ["string", "string"],
6258
+ arity: { kind: "exact" }
5935
6259
  },
5936
6260
  indent: {
5937
6261
  fn: (n, s) => indent(n, s),
5938
- argTypes: ["int", "string"]
6262
+ argTypes: ["int", "string"],
6263
+ arity: { kind: "exact" }
5939
6264
  },
5940
6265
  initials: {
5941
6266
  fn: (s) => initials(s),
5942
- argTypes: ["string"]
6267
+ argTypes: ["string"],
6268
+ arity: { kind: "exact" }
5943
6269
  },
5944
6270
  join: {
5945
6271
  fn: (sep, list) => join(sep, list),
5946
- argTypes: ["string", "list"]
6272
+ argTypes: ["string", "list"],
6273
+ arity: { kind: "exact" }
5947
6274
  },
5948
6275
  kebabcase: {
5949
6276
  fn: (s) => kebabcase(s),
5950
6277
  argTypes: ["string"],
6278
+ arity: { kind: "exact" },
5951
6279
  returnType: "string"
5952
6280
  },
5953
6281
  lower: {
5954
6282
  fn: (s) => lower(s),
5955
- argTypes: ["string"]
6283
+ argTypes: ["string"],
6284
+ arity: { kind: "exact" }
5956
6285
  },
5957
6286
  nindent: {
5958
6287
  fn: (n, s) => nindent(n, s),
5959
- argTypes: ["int", "string"]
6288
+ argTypes: ["int", "string"],
6289
+ arity: { kind: "exact" }
5960
6290
  },
5961
6291
  nospace: {
5962
6292
  fn: (s) => nospace(s),
5963
6293
  argTypes: ["string"],
6294
+ arity: { kind: "exact" },
5964
6295
  returnType: "string"
5965
6296
  },
5966
6297
  plural: {
@@ -5970,20 +6301,24 @@ function sprigStrings() {
5970
6301
  "string",
5971
6302
  "int"
5972
6303
  ],
6304
+ arity: { kind: "exact" },
5973
6305
  returnType: "string"
5974
6306
  },
5975
6307
  quote: {
5976
6308
  fn: (...a) => quote(...a),
5977
- argTypes: ["string"]
6309
+ argTypes: ["string"],
6310
+ arity: { kind: "variadic" }
5978
6311
  },
5979
6312
  regexQuoteMeta: {
5980
6313
  fn: (s) => regexQuoteMeta(s),
5981
6314
  argTypes: ["string"],
6315
+ arity: { kind: "exact" },
5982
6316
  returnType: "string"
5983
6317
  },
5984
6318
  repeat: {
5985
6319
  fn: (n, s) => repeat(n, s),
5986
- argTypes: ["int", "string"]
6320
+ argTypes: ["int", "string"],
6321
+ arity: { kind: "exact" }
5987
6322
  },
5988
6323
  replace: {
5989
6324
  fn: (o, n, s) => replace(o, n, s),
@@ -5991,20 +6326,24 @@ function sprigStrings() {
5991
6326
  "string",
5992
6327
  "string",
5993
6328
  "string"
5994
- ]
6329
+ ],
6330
+ arity: { kind: "exact" }
5995
6331
  },
5996
6332
  snakecase: {
5997
6333
  fn: (s) => snakecase(s),
5998
6334
  argTypes: ["string"],
6335
+ arity: { kind: "exact" },
5999
6336
  returnType: "string"
6000
6337
  },
6001
6338
  split: {
6002
6339
  fn: (sep, s) => split(sep, s),
6003
- argTypes: ["string", "string"]
6340
+ argTypes: ["string", "string"],
6341
+ arity: { kind: "exact" }
6004
6342
  },
6005
6343
  splitList: {
6006
6344
  fn: (sep, s) => splitList(sep, s),
6007
- argTypes: ["string", "string"]
6345
+ argTypes: ["string", "string"],
6346
+ arity: { kind: "exact" }
6008
6347
  },
6009
6348
  splitn: {
6010
6349
  fn: (sep, n, s) => splitn(sep, n, s),
@@ -6013,11 +6352,13 @@ function sprigStrings() {
6013
6352
  "int",
6014
6353
  "string"
6015
6354
  ],
6355
+ arity: { kind: "exact" },
6016
6356
  returnType: "dict"
6017
6357
  },
6018
6358
  squote: {
6019
6359
  fn: (...a) => squote(...a),
6020
- argTypes: ["string"]
6360
+ argTypes: ["string"],
6361
+ arity: { kind: "variadic" }
6021
6362
  },
6022
6363
  substr: {
6023
6364
  fn: (i, j, s) => substr(i, j, s),
@@ -6025,48 +6366,59 @@ function sprigStrings() {
6025
6366
  "int",
6026
6367
  "int",
6027
6368
  "string"
6028
- ]
6369
+ ],
6370
+ arity: { kind: "exact" }
6029
6371
  },
6030
6372
  swapcase: {
6031
6373
  fn: (s) => swapcase(s),
6032
6374
  argTypes: ["string"],
6375
+ arity: { kind: "exact" },
6033
6376
  returnType: "string"
6034
6377
  },
6035
6378
  title: {
6036
6379
  fn: (s) => title(s),
6037
- argTypes: ["string"]
6380
+ argTypes: ["string"],
6381
+ arity: { kind: "exact" }
6038
6382
  },
6039
6383
  trim: {
6040
6384
  fn: (s) => trim(s),
6041
- argTypes: ["string"]
6385
+ argTypes: ["string"],
6386
+ arity: { kind: "exact" }
6042
6387
  },
6043
6388
  trimAll: {
6044
6389
  fn: (cs, s) => trimAll(cs, s),
6045
- argTypes: ["string", "string"]
6390
+ argTypes: ["string", "string"],
6391
+ arity: { kind: "exact" }
6046
6392
  },
6047
6393
  trimPrefix: {
6048
6394
  fn: (p, s) => trimPrefix(p, s),
6049
- argTypes: ["string", "string"]
6395
+ argTypes: ["string", "string"],
6396
+ arity: { kind: "exact" }
6050
6397
  },
6051
6398
  trimSuffix: {
6052
6399
  fn: (sf, s) => trimSuffix(sf, s),
6053
- argTypes: ["string", "string"]
6400
+ argTypes: ["string", "string"],
6401
+ arity: { kind: "exact" }
6054
6402
  },
6055
6403
  trunc: {
6056
6404
  fn: (n, s) => trunc(n, s),
6057
- argTypes: ["int", "string"]
6405
+ argTypes: ["int", "string"],
6406
+ arity: { kind: "exact" }
6058
6407
  },
6059
6408
  untitle: {
6060
6409
  fn: (s) => untitle(s),
6061
- argTypes: ["string"]
6410
+ argTypes: ["string"],
6411
+ arity: { kind: "exact" }
6062
6412
  },
6063
6413
  upper: {
6064
6414
  fn: (s) => upper(s),
6065
- argTypes: ["string"]
6415
+ argTypes: ["string"],
6416
+ arity: { kind: "exact" }
6066
6417
  },
6067
6418
  wrap: {
6068
6419
  fn: (w, s) => wrap(w, s),
6069
- argTypes: ["int", "string"]
6420
+ argTypes: ["int", "string"],
6421
+ arity: { kind: "exact" }
6070
6422
  },
6071
6423
  wrapWith: {
6072
6424
  fn: (w, sep, s) => wrapWith(w, sep, s),
@@ -6074,7 +6426,8 @@ function sprigStrings() {
6074
6426
  "int",
6075
6427
  "string",
6076
6428
  "string"
6077
- ]
6429
+ ],
6430
+ arity: { kind: "exact" }
6078
6431
  }
6079
6432
  };
6080
6433
  }
@@ -6146,35 +6499,42 @@ function sprigTypes() {
6146
6499
  return {
6147
6500
  kindOf: {
6148
6501
  fn: (v) => kindOf(v),
6149
- argTypes: ["reflective"]
6502
+ argTypes: ["reflective"],
6503
+ arity: { kind: "exact" }
6150
6504
  },
6151
6505
  kindIs: {
6152
6506
  fn: (k, v) => kindIs(k, v),
6153
- argTypes: ["string", "reflective"]
6507
+ argTypes: ["string", "reflective"],
6508
+ arity: { kind: "exact" }
6154
6509
  },
6155
6510
  typeOf: {
6156
6511
  fn: (v) => typeOf(v),
6157
- argTypes: ["reflective"]
6512
+ argTypes: ["reflective"],
6513
+ arity: { kind: "exact" }
6158
6514
  },
6159
6515
  typeIs: {
6160
6516
  fn: (t, v) => typeIs(t, v),
6161
- argTypes: ["string", "reflective"]
6517
+ argTypes: ["string", "reflective"],
6518
+ arity: { kind: "exact" }
6162
6519
  },
6163
6520
  typeIsLike: {
6164
6521
  fn: (t, v) => typeIsLike(t, v),
6165
- argTypes: ["string", "reflective"]
6522
+ argTypes: ["string", "reflective"],
6523
+ arity: { kind: "exact" }
6166
6524
  },
6167
6525
  deepEqual: {
6168
6526
  fn: (a, b) => deepEqual(a, b),
6169
- argTypes: ["value", "value"]
6527
+ argTypes: ["value", "value"],
6528
+ arity: { kind: "exact" }
6170
6529
  },
6171
6530
  deepCopy: {
6172
6531
  fn: (v) => deepCopy(v),
6173
- argTypes: ["value"]
6532
+ argTypes: ["value"],
6533
+ arity: { kind: "exact" }
6174
6534
  }
6175
6535
  };
6176
6536
  }
6177
6537
  //#endregion
6178
- 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 };
6538
+ export { ArgCountError, Defines, Engine, EvalError, FailError, FuncNotFoundError, MissingFieldError, NotIntegerError, ParseError, Template, TemplateError, TypeMismatchError, createEngine, sprigConversions, sprigDatetime, sprigDefaults, sprigDicts, sprigFlow, sprigHash, sprigLists, sprigMath, sprigRandom, sprigRegex, sprigSemver, sprigStrings, sprigTypes, staticDictEntries };
6179
6539
 
6180
6540
  //# sourceMappingURL=index.js.map