@promptctl/go-template-js 0.7.0 → 0.9.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 +3 -1
- package/dist/index.d.ts +43 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +165 -106
- package/dist/index.js.map +1 -1
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -558,17 +558,59 @@ function isIdentPart(c) {
|
|
|
558
558
|
}
|
|
559
559
|
//#endregion
|
|
560
560
|
//#region src/parser/parser.ts
|
|
561
|
-
|
|
562
|
-
|
|
561
|
+
/**
|
|
562
|
+
* The named sub-templates a template can invoke: the ones its own source
|
|
563
|
+
* 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
|
|
565
|
+
* one by hand (the parser is the single enforcer of the redefinition invariant
|
|
566
|
+
* below) nor reach the AST inside — the entry lookup is `lookupDefine`, a
|
|
567
|
+
* module export index.ts never re-exports.
|
|
568
|
+
*
|
|
569
|
+
* [LAW:one-source-of-truth] Inheritance is a parent LINK, never a copy. N
|
|
570
|
+
* templates parsed against one shared preamble hold one preamble AST among
|
|
571
|
+
* them — the alternative (prepending the preamble's source to each parse)
|
|
572
|
+
* multiplies the preamble's AST by N, which is how a 2 KB helper block became
|
|
573
|
+
* hundreds of megabytes in a long-lived process.
|
|
574
|
+
*
|
|
575
|
+
* Lookup is nearest-first, but the parser forbids a template from declaring a
|
|
576
|
+
* name its inherited set already holds (the same `redefinition` error as two
|
|
577
|
+
* declarations in one source), so nearest-first never actually shadows.
|
|
578
|
+
*/
|
|
579
|
+
let chainDefines;
|
|
580
|
+
let readDefine;
|
|
581
|
+
/** Nearest-first entry lookup through the inheritance chain. Package-internal. */
|
|
582
|
+
function lookupDefine(defines, name) {
|
|
583
|
+
return readDefine(defines, name);
|
|
584
|
+
}
|
|
585
|
+
var Defines = class Defines {
|
|
586
|
+
static EMPTY = new Defines(/* @__PURE__ */ new Map(), void 0);
|
|
587
|
+
#own;
|
|
588
|
+
#parent;
|
|
589
|
+
constructor(own, parent) {
|
|
590
|
+
this.#own = own;
|
|
591
|
+
this.#parent = parent;
|
|
592
|
+
}
|
|
593
|
+
static {
|
|
594
|
+
chainDefines = (own, parent) => new Defines(own, parent);
|
|
595
|
+
readDefine = (d, name) => d.#own.get(name) ?? (d.#parent && readDefine(d.#parent, name));
|
|
596
|
+
}
|
|
597
|
+
has(name) {
|
|
598
|
+
return readDefine(this, name) !== void 0;
|
|
599
|
+
}
|
|
600
|
+
};
|
|
601
|
+
function parse(source, delims, inherit = Defines.EMPTY) {
|
|
602
|
+
return new Parser(source, delims, inherit).parseTemplate();
|
|
563
603
|
}
|
|
564
604
|
var Parser = class {
|
|
565
605
|
lex;
|
|
566
606
|
buf = [];
|
|
567
607
|
defines = /* @__PURE__ */ new Map();
|
|
608
|
+
inherit;
|
|
568
609
|
source;
|
|
569
610
|
rangeDepth = 0;
|
|
570
|
-
constructor(source, delims) {
|
|
611
|
+
constructor(source, delims, inherit) {
|
|
571
612
|
this.source = source;
|
|
613
|
+
this.inherit = inherit;
|
|
572
614
|
this.lex = delims ? new Lexer(source, delims) : new Lexer(source);
|
|
573
615
|
}
|
|
574
616
|
peek(n = 0) {
|
|
@@ -606,7 +648,8 @@ var Parser = class {
|
|
|
606
648
|
}
|
|
607
649
|
return {
|
|
608
650
|
root,
|
|
609
|
-
defines: this.defines,
|
|
651
|
+
defines: chainDefines(this.defines, this.inherit),
|
|
652
|
+
ownDefines: this.defines,
|
|
610
653
|
source: this.source
|
|
611
654
|
};
|
|
612
655
|
}
|
|
@@ -822,7 +865,10 @@ var Parser = class {
|
|
|
822
865
|
trimLeft,
|
|
823
866
|
trimRight: this.consumeEnd()
|
|
824
867
|
};
|
|
825
|
-
if (!this.defines.has(name)) this.defines.set(name,
|
|
868
|
+
if (!this.defines.has(name) && !this.inherit.has(name)) this.defines.set(name, {
|
|
869
|
+
list,
|
|
870
|
+
source: this.source
|
|
871
|
+
});
|
|
826
872
|
return pipe ? {
|
|
827
873
|
type: "Block",
|
|
828
874
|
pos: startPos,
|
|
@@ -846,8 +892,11 @@ var Parser = class {
|
|
|
846
892
|
const list = this.parseList();
|
|
847
893
|
this.rangeDepth = savedDepth;
|
|
848
894
|
this.consumeEnd();
|
|
849
|
-
if (this.defines.has(name)) throw new ParseError(`redefinition of template ${JSON.stringify(name)}`, list.pos, { source: this.source });
|
|
850
|
-
this.defines.set(name,
|
|
895
|
+
if (this.defines.has(name) || this.inherit.has(name)) throw new ParseError(`redefinition of template ${JSON.stringify(name)}`, list.pos, { source: this.source });
|
|
896
|
+
this.defines.set(name, {
|
|
897
|
+
list,
|
|
898
|
+
source: this.source
|
|
899
|
+
});
|
|
851
900
|
}
|
|
852
901
|
parseQuotedName() {
|
|
853
902
|
const t = this.next();
|
|
@@ -1355,7 +1404,7 @@ function isTruthy(value) {
|
|
|
1355
1404
|
* matching names override the built-in (intentional — gives
|
|
1356
1405
|
* consumers an escape hatch).
|
|
1357
1406
|
*/
|
|
1358
|
-
function eagerBuiltins(toString) {
|
|
1407
|
+
function eagerBuiltins(toString, isT) {
|
|
1359
1408
|
return {
|
|
1360
1409
|
eq: {
|
|
1361
1410
|
fn: (a, ...rest) => rest.some((r) => goEqPair(a, r)),
|
|
@@ -1412,10 +1461,19 @@ function eagerBuiltins(toString) {
|
|
|
1412
1461
|
returnType: "string"
|
|
1413
1462
|
},
|
|
1414
1463
|
printf: {
|
|
1415
|
-
fn: (format, ...args) => sprintf(format, args, toString),
|
|
1464
|
+
fn: (format, ...args) => sprintf(format, args, toString, isT),
|
|
1416
1465
|
argTypes: ["string", "stringifiable"],
|
|
1417
1466
|
returnType: "string"
|
|
1418
1467
|
},
|
|
1468
|
+
toString: {
|
|
1469
|
+
fn: (v) => formatV(v, toString, isT),
|
|
1470
|
+
argTypes: ["value"],
|
|
1471
|
+
returnType: "string"
|
|
1472
|
+
},
|
|
1473
|
+
toStrings: {
|
|
1474
|
+
fn: (list) => list.map((v) => formatV(v, toString, isT)),
|
|
1475
|
+
argTypes: ["list"]
|
|
1476
|
+
},
|
|
1419
1477
|
call: {
|
|
1420
1478
|
fn: (fn, ...args) => fn(...args),
|
|
1421
1479
|
argTypes: ["callable", "value"]
|
|
@@ -1467,9 +1525,9 @@ function lazyBuiltins() {
|
|
|
1467
1525
|
})
|
|
1468
1526
|
};
|
|
1469
1527
|
}
|
|
1470
|
-
function defaultBuiltins(toString) {
|
|
1528
|
+
function defaultBuiltins(toString, isT) {
|
|
1471
1529
|
return {
|
|
1472
|
-
...eagerBuiltins(toString),
|
|
1530
|
+
...eagerBuiltins(toString, isT),
|
|
1473
1531
|
...lazyBuiltins()
|
|
1474
1532
|
};
|
|
1475
1533
|
}
|
|
@@ -1592,7 +1650,7 @@ function urlQueryEscape(s) {
|
|
|
1592
1650
|
else out += `%${b.toString(16).toUpperCase().padStart(2, "0")}`;
|
|
1593
1651
|
return out;
|
|
1594
1652
|
}
|
|
1595
|
-
function sprintf(format, args, toString) {
|
|
1653
|
+
function sprintf(format, args, toString, isT) {
|
|
1596
1654
|
let out = "";
|
|
1597
1655
|
let argIdx = 0;
|
|
1598
1656
|
let i = 0;
|
|
@@ -1618,18 +1676,18 @@ function sprintf(format, args, toString) {
|
|
|
1618
1676
|
const verb = format[j];
|
|
1619
1677
|
const spec = format.slice(i + 1, j);
|
|
1620
1678
|
const arg = args[argIdx++];
|
|
1621
|
-
out += formatVerb(verb ?? "", spec, arg, toString);
|
|
1679
|
+
out += formatVerb(verb ?? "", spec, arg, toString, isT);
|
|
1622
1680
|
i = j + 1;
|
|
1623
1681
|
}
|
|
1624
1682
|
return out;
|
|
1625
1683
|
}
|
|
1626
|
-
function formatVerb(verb, spec, arg, toString) {
|
|
1684
|
+
function formatVerb(verb, spec, arg, toString, isT) {
|
|
1627
1685
|
switch (verb) {
|
|
1628
1686
|
case "s": return typeof arg === "string" ? applyWidth(spec, arg) : applyWidth(spec, stringifyForPrint(arg, toString));
|
|
1629
1687
|
case "d":
|
|
1630
1688
|
if (typeof arg !== "number" && typeof arg !== "bigint") return `%!${verb}(${goPrintfType(arg)}=${stringifyForPrint(arg, toString)})`;
|
|
1631
1689
|
return applyWidth(spec, String(typeof arg === "bigint" ? arg : Math.trunc(arg)));
|
|
1632
|
-
case "v": return applyWidth(spec, formatV(arg));
|
|
1690
|
+
case "v": return applyWidth(spec, formatV(arg, toString, isT));
|
|
1633
1691
|
case "q": return applyWidth(spec, JSON.stringify(stringifyForPrint(arg, toString)));
|
|
1634
1692
|
case "f": {
|
|
1635
1693
|
if (typeof arg !== "number" && typeof arg !== "bigint") return `%!${verb}(${goPrintfType(arg)}=${stringifyForPrint(arg, toString)})`;
|
|
@@ -1659,15 +1717,33 @@ function applyWidth(spec, body) {
|
|
|
1659
1717
|
const pad = " ".repeat(width - body.length);
|
|
1660
1718
|
return flag.includes("-") ? body + pad : pad + body;
|
|
1661
1719
|
}
|
|
1662
|
-
function formatV(value) {
|
|
1720
|
+
function formatV(value, toString, isT) {
|
|
1663
1721
|
if (value === null || value === void 0) return "<nil>";
|
|
1664
1722
|
if (typeof value === "string") return value;
|
|
1665
|
-
if (
|
|
1666
|
-
if (Array.isArray(value)) return `[${value.map(formatV).join(" ")}]`;
|
|
1667
|
-
if (value instanceof Map) return
|
|
1668
|
-
if (
|
|
1723
|
+
if (isT(value)) return toString(value);
|
|
1724
|
+
if (Array.isArray(value)) return `[${value.map((v) => formatV(v, toString, isT)).join(" ")}]`;
|
|
1725
|
+
if (value instanceof Map) return formatEntries([...value.entries()], toString, isT);
|
|
1726
|
+
if (isPlainObject(value)) return formatEntries(Object.entries(value), toString, isT);
|
|
1669
1727
|
return String(value);
|
|
1670
1728
|
}
|
|
1729
|
+
function formatEntries(entries, toString, isT) {
|
|
1730
|
+
const fmt = (v) => formatV(v, toString, isT);
|
|
1731
|
+
const isNumeric = (k) => typeof k === "number" || typeof k === "bigint";
|
|
1732
|
+
const byKey = ([a], [b]) => {
|
|
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
|
+
const [as, bs] = [fmt(a), fmt(b)];
|
|
1736
|
+
return as < bs ? -1 : as > bs ? 1 : 0;
|
|
1737
|
+
};
|
|
1738
|
+
return `map[${[...entries].sort(byKey).map(([k, v]) => `${fmt(k)}:${fmt(v)}`).join(" ")}]`;
|
|
1739
|
+
}
|
|
1740
|
+
function isPlainObject(value) {
|
|
1741
|
+
if (value === null || typeof value !== "object") return false;
|
|
1742
|
+
if (Array.isArray(value)) return false;
|
|
1743
|
+
if (value instanceof Map || value instanceof Set) return false;
|
|
1744
|
+
const proto = Object.getPrototypeOf(value);
|
|
1745
|
+
return proto === null || proto === Object.prototype;
|
|
1746
|
+
}
|
|
1671
1747
|
//#endregion
|
|
1672
1748
|
//#region src/evaluator/scope.ts
|
|
1673
1749
|
function rootScope(value) {
|
|
@@ -1775,23 +1851,39 @@ let internalCreateTemplate;
|
|
|
1775
1851
|
var Template = class Template {
|
|
1776
1852
|
source;
|
|
1777
1853
|
_evaluate;
|
|
1854
|
+
_defines;
|
|
1778
1855
|
_referencedFunctions;
|
|
1779
1856
|
_referencedCalls;
|
|
1780
|
-
constructor(source, evaluate, referencedFunctions, referencedCalls) {
|
|
1857
|
+
constructor(source, evaluate, defines, referencedFunctions, referencedCalls) {
|
|
1781
1858
|
this.source = source;
|
|
1782
1859
|
this._evaluate = evaluate;
|
|
1860
|
+
this._defines = defines;
|
|
1783
1861
|
this._referencedFunctions = referencedFunctions;
|
|
1784
1862
|
this._referencedCalls = referencedCalls;
|
|
1785
1863
|
}
|
|
1786
1864
|
static {
|
|
1787
|
-
internalCreateTemplate = (source, evaluate, referencedFunctions, referencedCalls) => new Template(source, evaluate, referencedFunctions, referencedCalls);
|
|
1865
|
+
internalCreateTemplate = (source, evaluate, defines, referencedFunctions, referencedCalls) => new Template(source, evaluate, defines, referencedFunctions, referencedCalls);
|
|
1866
|
+
}
|
|
1867
|
+
/**
|
|
1868
|
+
* The named sub-templates this template can invoke — its own `{{define}}`s
|
|
1869
|
+
* chained onto whatever it inherited at parse. Pass it to `engine.parse` as
|
|
1870
|
+
* `inherit` to let another template invoke the same set without re-parsing
|
|
1871
|
+
* (and re-allocating) it: the idiom for a shared helper preamble is
|
|
1872
|
+
* `const helpers = engine.parse(preamble).defines()` once, then
|
|
1873
|
+
* `engine.parse(src, helpers)` per template. Opaque: the AST inside is not
|
|
1874
|
+
* part of the public surface.
|
|
1875
|
+
*/
|
|
1876
|
+
defines() {
|
|
1877
|
+
return this._defines;
|
|
1788
1878
|
}
|
|
1789
1879
|
evaluate(scope) {
|
|
1790
1880
|
return this._evaluate(scope);
|
|
1791
1881
|
}
|
|
1792
1882
|
/**
|
|
1793
1883
|
* The FuncMap function names this template references — every identifier that
|
|
1794
|
-
* names a function
|
|
1884
|
+
* names a function in this parse: the template body and its own `{{ define }}`
|
|
1885
|
+
* blocks. A set inherited at parse (see {@link Template.defines}) is described
|
|
1886
|
+
* by the template that parsed it, not here.
|
|
1795
1887
|
*
|
|
1796
1888
|
* This is a STATIC fact derived from the parsed AST, not an execution trace:
|
|
1797
1889
|
* a name in the set is a function the template *can* invoke; a name absent
|
|
@@ -1809,8 +1901,8 @@ var Template = class Template {
|
|
|
1809
1901
|
return this._referencedFunctions;
|
|
1810
1902
|
}
|
|
1811
1903
|
/**
|
|
1812
|
-
* Every command-head call in the template body and its
|
|
1813
|
-
* in preorder, each paired with its positional arguments projected to literal
|
|
1904
|
+
* Every command-head call in this parse — the template body and its own
|
|
1905
|
+
* `{{ define }}` blocks, never an inherited set — in preorder, each paired with its positional arguments projected to literal
|
|
1814
1906
|
* strings (a non-string-literal argument is reported as `null`, preserving
|
|
1815
1907
|
* argument positions).
|
|
1816
1908
|
*
|
|
@@ -1837,7 +1929,7 @@ function collectReferencedFunctions(parsed) {
|
|
|
1837
1929
|
});
|
|
1838
1930
|
};
|
|
1839
1931
|
collect(parsed.root);
|
|
1840
|
-
for (const
|
|
1932
|
+
for (const entry of parsed.ownDefines.values()) collect(entry.list);
|
|
1841
1933
|
return names;
|
|
1842
1934
|
}
|
|
1843
1935
|
const DYNAMIC_ARG = { kind: "dynamic" };
|
|
@@ -1893,12 +1985,13 @@ function collectReferencedCalls(parsed) {
|
|
|
1893
1985
|
});
|
|
1894
1986
|
};
|
|
1895
1987
|
collect(parsed.root);
|
|
1896
|
-
for (const
|
|
1988
|
+
for (const entry of parsed.ownDefines.values()) collect(entry.list);
|
|
1897
1989
|
return calls;
|
|
1898
1990
|
}
|
|
1899
1991
|
var Engine = class {
|
|
1900
1992
|
fromString;
|
|
1901
1993
|
toString;
|
|
1994
|
+
isT;
|
|
1902
1995
|
funcs;
|
|
1903
1996
|
missingKey;
|
|
1904
1997
|
delims;
|
|
@@ -1906,10 +1999,11 @@ var Engine = class {
|
|
|
1906
1999
|
this.fromString = config.fromString;
|
|
1907
2000
|
const userToString = Object.hasOwn(config, "toString") ? config.toString : void 0;
|
|
1908
2001
|
this.toString = userToString ?? defaultToString;
|
|
2002
|
+
this.isT = config.isT ?? DEFAULT_IS_T;
|
|
1909
2003
|
this.missingKey = validateMissingKey(config.missingKey);
|
|
1910
2004
|
this.delims = validateDelims(config.delims);
|
|
1911
2005
|
this.funcs = {
|
|
1912
|
-
...defaultBuiltins(this.toString),
|
|
2006
|
+
...defaultBuiltins(this.toString, this.isT),
|
|
1913
2007
|
...config.funcs ?? {}
|
|
1914
2008
|
};
|
|
1915
2009
|
}
|
|
@@ -1920,9 +2014,9 @@ var Engine = class {
|
|
|
1920
2014
|
* it any number of times with different scopes is safe and avoids
|
|
1921
2015
|
* re-parsing.
|
|
1922
2016
|
*/
|
|
1923
|
-
parse(source) {
|
|
1924
|
-
const parsed = parse(source, this.delims);
|
|
1925
|
-
return internalCreateTemplate(parsed.source, (scope) => this.evalParsed(parsed, scope), collectReferencedFunctions(parsed), collectReferencedCalls(parsed));
|
|
2017
|
+
parse(source, inherit) {
|
|
2018
|
+
const parsed = parse(source, this.delims, inherit);
|
|
2019
|
+
return internalCreateTemplate(parsed.source, (scope) => this.evalParsed(parsed, scope), parsed.defines, collectReferencedFunctions(parsed), collectReferencedCalls(parsed));
|
|
1926
2020
|
}
|
|
1927
2021
|
/**
|
|
1928
2022
|
* Convenience sugar for `parse(src).evaluate.bind(template)`.
|
|
@@ -2044,15 +2138,26 @@ var Engine = class {
|
|
|
2044
2138
|
}
|
|
2045
2139
|
}
|
|
2046
2140
|
evalTemplateInvoke(node, scope, ctx) {
|
|
2047
|
-
const
|
|
2048
|
-
if (!
|
|
2141
|
+
const entry = lookupDefine(ctx.defines, node.name);
|
|
2142
|
+
if (!entry) throw new EvalError(`template ${JSON.stringify(node.name)} is not defined`, node.pos, { source: ctx.source });
|
|
2049
2143
|
const child = pushScope(scope, node.pipe ? this.evalPipe(node.pipe, scope, ctx) : scope.dot);
|
|
2050
|
-
this.evalList(
|
|
2144
|
+
this.evalList(entry.list, child, {
|
|
2145
|
+
out: ctx.out,
|
|
2146
|
+
defines: ctx.defines,
|
|
2147
|
+
source: entry.source
|
|
2148
|
+
});
|
|
2051
2149
|
}
|
|
2052
2150
|
evalBlock(node, scope, ctx) {
|
|
2053
2151
|
const child = pushScope(scope, node.pipe ? this.evalPipe(node.pipe, scope, ctx) : scope.dot);
|
|
2054
|
-
const
|
|
2055
|
-
|
|
2152
|
+
const entry = lookupDefine(ctx.defines, node.name) ?? {
|
|
2153
|
+
list: node.list,
|
|
2154
|
+
source: ctx.source
|
|
2155
|
+
};
|
|
2156
|
+
this.evalList(entry.list, child, {
|
|
2157
|
+
out: ctx.out,
|
|
2158
|
+
defines: ctx.defines,
|
|
2159
|
+
source: entry.source
|
|
2160
|
+
});
|
|
2056
2161
|
}
|
|
2057
2162
|
evalAction(node, scope, ctx) {
|
|
2058
2163
|
return this.evalPipe(node.pipe, scope, ctx);
|
|
@@ -2099,7 +2204,7 @@ var Engine = class {
|
|
|
2099
2204
|
const v = piped.value;
|
|
2100
2205
|
args.push(lazy ? () => v : v);
|
|
2101
2206
|
}
|
|
2102
|
-
enforceArgTypes(head.ident, fn.argTypes, args, cmd.pos, ctx.source, this.toString, fn.argTypePattern, this.fromString);
|
|
2207
|
+
enforceArgTypes(head.ident, fn.argTypes, args, cmd.pos, ctx.source, this.toString, fn.argTypePattern, this.fromString, this.isT);
|
|
2103
2208
|
try {
|
|
2104
2209
|
return fn.fn(...args);
|
|
2105
2210
|
} catch (e) {
|
|
@@ -2123,7 +2228,7 @@ var Engine = class {
|
|
|
2123
2228
|
source: ctx.source,
|
|
2124
2229
|
available: Object.keys(this.funcs)
|
|
2125
2230
|
});
|
|
2126
|
-
enforceArgTypes(node.ident, fn.argTypes, [], node.pos, ctx.source, this.toString, fn.argTypePattern, this.fromString);
|
|
2231
|
+
enforceArgTypes(node.ident, fn.argTypes, [], node.pos, ctx.source, this.toString, fn.argTypePattern, this.fromString, this.isT);
|
|
2127
2232
|
return fn.fn();
|
|
2128
2233
|
}
|
|
2129
2234
|
case "Chain": return this.resolveFieldChain(this.evalPrimary(node.node, scope, ctx), node.fields, node.pos, ctx);
|
|
@@ -2160,42 +2265,25 @@ var Engine = class {
|
|
|
2160
2265
|
ctx.out.push(this.fromString(String(value)));
|
|
2161
2266
|
return;
|
|
2162
2267
|
}
|
|
2163
|
-
if (
|
|
2164
|
-
ctx.out.push(
|
|
2165
|
-
return;
|
|
2166
|
-
}
|
|
2167
|
-
if (value instanceof Map) {
|
|
2168
|
-
ctx.out.push(this.fromString(formatMapLikeGo(value)));
|
|
2268
|
+
if (this.isT(value)) {
|
|
2269
|
+
ctx.out.push(value);
|
|
2169
2270
|
return;
|
|
2170
2271
|
}
|
|
2171
|
-
ctx.out.push(value);
|
|
2272
|
+
ctx.out.push(this.fromString(formatV(value, this.toString, this.isT)));
|
|
2172
2273
|
}
|
|
2173
2274
|
};
|
|
2174
|
-
|
|
2175
|
-
return `[${arr.map(formatScalarLikeGo).join(" ")}]`;
|
|
2176
|
-
}
|
|
2177
|
-
function formatMapLikeGo(m) {
|
|
2178
|
-
const parts = [];
|
|
2179
|
-
for (const [k, v] of m) parts.push(`${formatScalarLikeGo(k)}:${formatScalarLikeGo(v)}`);
|
|
2180
|
-
return `map[${parts.join(" ")}]`;
|
|
2181
|
-
}
|
|
2182
|
-
function formatScalarLikeGo(v) {
|
|
2183
|
-
if (v === null || v === void 0) return "<nil>";
|
|
2184
|
-
if (Array.isArray(v)) return formatArrayLikeGo(v);
|
|
2185
|
-
if (v instanceof Map) return formatMapLikeGo(v);
|
|
2186
|
-
return String(v);
|
|
2187
|
-
}
|
|
2275
|
+
const DEFAULT_IS_T = (v) => typeof v === "object" && v !== null && !Array.isArray(v) && !(v instanceof Map);
|
|
2188
2276
|
function createEngine(config) {
|
|
2189
2277
|
return new Engine(config);
|
|
2190
2278
|
}
|
|
2191
|
-
function enforceArgTypes(funcName, argTypes, values, pos, src, toString = defaultToString, pattern, fromString = defaultFromString) {
|
|
2279
|
+
function enforceArgTypes(funcName, argTypes, values, pos, src, toString = defaultToString, pattern, fromString = defaultFromString, isT = DEFAULT_IS_T) {
|
|
2192
2280
|
const lookup = makeSlotLookup(argTypes, pattern);
|
|
2193
2281
|
let firstOrdered = -1;
|
|
2194
2282
|
let firstComparable = -1;
|
|
2195
2283
|
for (let i = 0; i < values.length; i++) {
|
|
2196
2284
|
const declared = lookup(i);
|
|
2197
2285
|
const value = values[i];
|
|
2198
|
-
if (!matchesArgType(declared, value, toString)) throw new TypeMismatchError(funcName, i + 1, humanArgType(declared), describeValue(value), pos, { source: src });
|
|
2286
|
+
if (!matchesArgType(declared, value, toString, isT)) throw new TypeMismatchError(funcName, i + 1, humanArgType(declared), describeValue(value), pos, { source: src });
|
|
2199
2287
|
if (declared === "liftable" && typeof value === "string") values[i] = fromString(value);
|
|
2200
2288
|
if (declared === "int") values[i] = Math.trunc(Number(value));
|
|
2201
2289
|
else if (declared === "float") values[i] = Number(value);
|
|
@@ -2218,7 +2306,7 @@ function makeSlotLookup(argTypes, pattern) {
|
|
|
2218
2306
|
const trailing = argTypes[argTypes.length - 1];
|
|
2219
2307
|
return (i) => i < argTypes.length ? argTypes[i] : trailing;
|
|
2220
2308
|
}
|
|
2221
|
-
function matchesArgType(declared, value, toString) {
|
|
2309
|
+
function matchesArgType(declared, value, toString, isT) {
|
|
2222
2310
|
switch (declared) {
|
|
2223
2311
|
case "truthy":
|
|
2224
2312
|
case "reflective":
|
|
@@ -2228,7 +2316,7 @@ function matchesArgType(declared, value, toString) {
|
|
|
2228
2316
|
case "float": return typeof value === "number" || typeof value === "bigint" && Number.isFinite(Number(value));
|
|
2229
2317
|
case "bool": return typeof value === "boolean";
|
|
2230
2318
|
case "ordered": return typeof value === "string" || typeof value === "number" || typeof value === "bigint" || typeof value === "boolean";
|
|
2231
|
-
case "T": return value
|
|
2319
|
+
case "T": return isT(value);
|
|
2232
2320
|
case "list": return Array.isArray(value);
|
|
2233
2321
|
case "dict": return isPlainObject(value);
|
|
2234
2322
|
case "sized": return typeof value === "string" || Array.isArray(value) || value instanceof Map || value instanceof Set || isPlainObject(value);
|
|
@@ -2245,7 +2333,7 @@ function matchesArgType(declared, value, toString) {
|
|
|
2245
2333
|
} catch {
|
|
2246
2334
|
return false;
|
|
2247
2335
|
}
|
|
2248
|
-
case "liftable": return typeof value === "string" || value
|
|
2336
|
+
case "liftable": return typeof value === "string" || isT(value);
|
|
2249
2337
|
case "serializable": return isJsonSerializable(value);
|
|
2250
2338
|
default: throw new Error(`invalid ArgType: ${String(declared)}`);
|
|
2251
2339
|
}
|
|
@@ -2263,13 +2351,6 @@ function defaultToString(value) {
|
|
|
2263
2351
|
offset: 0
|
|
2264
2352
|
});
|
|
2265
2353
|
}
|
|
2266
|
-
function isPlainObject(value) {
|
|
2267
|
-
if (value === null || typeof value !== "object") return false;
|
|
2268
|
-
if (Array.isArray(value)) return false;
|
|
2269
|
-
if (value instanceof Map || value instanceof Set) return false;
|
|
2270
|
-
const proto = Object.getPrototypeOf(value);
|
|
2271
|
-
return proto === null || proto === Object.prototype;
|
|
2272
|
-
}
|
|
2273
2354
|
function isJsonSerializable(value) {
|
|
2274
2355
|
try {
|
|
2275
2356
|
return JSON.stringify(value) !== void 0;
|
|
@@ -2555,29 +2636,6 @@ function toRawJson(value) {
|
|
|
2555
2636
|
return JSON.stringify(value) ?? "null";
|
|
2556
2637
|
}
|
|
2557
2638
|
//#endregion
|
|
2558
|
-
//#region src/sprig/conversions/toString.ts
|
|
2559
|
-
/**
|
|
2560
|
-
* `toString v` — Go sprig's `strval`. Strings pass through; everything
|
|
2561
|
-
* else routes through Go's `%v` formatter (`fmt.Sprintf("%v", v)`).
|
|
2562
|
-
*
|
|
2563
|
-
* [LAW:one-source-of-truth] Shares `formatV` with printf's `%v` verb —
|
|
2564
|
-
* one place owns "what does Go's %v look like".
|
|
2565
|
-
*/
|
|
2566
|
-
function toString(v) {
|
|
2567
|
-
return typeof v === "string" ? v : formatV(v);
|
|
2568
|
-
}
|
|
2569
|
-
//#endregion
|
|
2570
|
-
//#region src/sprig/conversions/toStrings.ts
|
|
2571
|
-
/**
|
|
2572
|
-
* `toStrings list` — map each element through `toString`.
|
|
2573
|
-
*
|
|
2574
|
-
* The `"list"` gate validates array-ness once; this body trusts it
|
|
2575
|
-
* and only routes the per-element conversion.
|
|
2576
|
-
*/
|
|
2577
|
-
function toStrings(list) {
|
|
2578
|
-
return list.map(toString);
|
|
2579
|
-
}
|
|
2580
|
-
//#endregion
|
|
2581
2639
|
//#region src/sprig/conversions/index.ts
|
|
2582
2640
|
function sprigConversions() {
|
|
2583
2641
|
return {
|
|
@@ -2600,15 +2658,6 @@ function sprigConversions() {
|
|
|
2600
2658
|
argTypes: ["value"],
|
|
2601
2659
|
returnType: "float"
|
|
2602
2660
|
},
|
|
2603
|
-
toString: {
|
|
2604
|
-
fn: (v) => toString(v),
|
|
2605
|
-
argTypes: ["value"],
|
|
2606
|
-
returnType: "string"
|
|
2607
|
-
},
|
|
2608
|
-
toStrings: {
|
|
2609
|
-
fn: (l) => toStrings(l),
|
|
2610
|
-
argTypes: ["list"]
|
|
2611
|
-
},
|
|
2612
2661
|
toDecimal: {
|
|
2613
2662
|
fn: (s) => toDecimal(s),
|
|
2614
2663
|
argTypes: ["string"],
|
|
@@ -5505,9 +5554,19 @@ function indent(n, s) {
|
|
|
5505
5554
|
}
|
|
5506
5555
|
//#endregion
|
|
5507
5556
|
//#region src/sprig/strings/initials.ts
|
|
5508
|
-
/**
|
|
5557
|
+
/**
|
|
5558
|
+
* `initials s` — port of `goutils.Initials` (what Go sprig's `initials`
|
|
5559
|
+
* delegates to): the first character of each whitespace-separated word,
|
|
5560
|
+
* case preserved — `"Ada Lovelace"` -> `"AL"`, `"foo bar baz"` -> `"fbb"`.
|
|
5561
|
+
*
|
|
5562
|
+
* Byte-faithful: goutils indexes the word's first BYTE, so a multi-byte
|
|
5563
|
+
* leading character yields that byte as a code point — `"Ölga"` -> `"Ã"`,
|
|
5564
|
+
* `"𐍈foo"` -> `"ð"` (both verified against the Go conformance generator),
|
|
5565
|
+
* never the full glyph.
|
|
5566
|
+
*/
|
|
5509
5567
|
function initials(s) {
|
|
5510
|
-
|
|
5568
|
+
const enc = new TextEncoder();
|
|
5569
|
+
return s.split(/\s+/).filter((w) => w.length > 0).map((w) => String.fromCharCode(...enc.encode(w).slice(0, 1))).join("");
|
|
5511
5570
|
}
|
|
5512
5571
|
//#endregion
|
|
5513
5572
|
//#region src/sprig/strings/join.ts
|
|
@@ -6089,6 +6148,6 @@ function sprigTypes() {
|
|
|
6089
6148
|
};
|
|
6090
6149
|
}
|
|
6091
6150
|
//#endregion
|
|
6092
|
-
export { Engine, EvalError, FailError, FuncNotFoundError, MissingFieldError, ParseError, Template, TemplateError, TypeMismatchError, createEngine, sprigConversions, sprigDatetime, sprigDefaults, sprigDicts, sprigFlow, sprigHash, sprigLists, sprigMath, sprigRandom, sprigRegex, sprigSemver, sprigStrings, sprigTypes, staticDictEntries };
|
|
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 };
|
|
6093
6152
|
|
|
6094
6153
|
//# sourceMappingURL=index.js.map
|