@promptctl/go-template-js 0.3.0 → 0.5.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 +52 -17
- package/dist/index.d.ts +32 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +182 -84
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1161,6 +1161,66 @@ function decodeRuneLiteral(raw, pos, source) {
|
|
|
1161
1161
|
return decoded.codePointAt(0) ?? 0;
|
|
1162
1162
|
}
|
|
1163
1163
|
//#endregion
|
|
1164
|
+
//#region src/parser/walk.ts
|
|
1165
|
+
/**
|
|
1166
|
+
* AST traversal helpers.
|
|
1167
|
+
*
|
|
1168
|
+
* [LAW:dataflow-not-control-flow] `children()` is one exhaustive switch on
|
|
1169
|
+
* the node's `type` discriminator that always returns a (possibly empty)
|
|
1170
|
+
* array of children. Walkers do not skip operations — they always call
|
|
1171
|
+
* `visit(n)` on every reachable node, then recurse into whatever children
|
|
1172
|
+
* the data-driven `children()` exposes.
|
|
1173
|
+
*
|
|
1174
|
+
* [LAW:single-enforcer] All AST traversal goes through `walk` /
|
|
1175
|
+
* `children`. Any caller who reinvents traversal will diverge — fix the
|
|
1176
|
+
* helper, not the callsite.
|
|
1177
|
+
*/
|
|
1178
|
+
/**
|
|
1179
|
+
* Walk an AST in preorder. The visitor sees a node before its children.
|
|
1180
|
+
*/
|
|
1181
|
+
function walk(node, visit) {
|
|
1182
|
+
if (visit(node) === false) return;
|
|
1183
|
+
for (const child of children(node)) walk(child, visit);
|
|
1184
|
+
}
|
|
1185
|
+
/**
|
|
1186
|
+
* The direct children of a node, in source order. Empty for leaves.
|
|
1187
|
+
*
|
|
1188
|
+
* Returning a fresh array each call keeps the contract simple — callers
|
|
1189
|
+
* may iterate or splat without aliasing surprises. The cost is trivial
|
|
1190
|
+
* for AST traversal.
|
|
1191
|
+
*/
|
|
1192
|
+
function children(node) {
|
|
1193
|
+
switch (node.type) {
|
|
1194
|
+
case "Text":
|
|
1195
|
+
case "Comment":
|
|
1196
|
+
case "Dot":
|
|
1197
|
+
case "Nil":
|
|
1198
|
+
case "Bool":
|
|
1199
|
+
case "Number":
|
|
1200
|
+
case "String":
|
|
1201
|
+
case "Identifier":
|
|
1202
|
+
case "Field":
|
|
1203
|
+
case "Variable":
|
|
1204
|
+
case "Break":
|
|
1205
|
+
case "Continue": return [];
|
|
1206
|
+
case "List": return node.nodes;
|
|
1207
|
+
case "Chain": return [node.node];
|
|
1208
|
+
case "Command": return node.args;
|
|
1209
|
+
case "Pipe": return [...node.decls, ...node.cmds];
|
|
1210
|
+
case "Action": return [node.pipe];
|
|
1211
|
+
case "If":
|
|
1212
|
+
case "Range":
|
|
1213
|
+
case "With": return node.elseList ? [
|
|
1214
|
+
node.pipe,
|
|
1215
|
+
node.list,
|
|
1216
|
+
node.elseList
|
|
1217
|
+
] : [node.pipe, node.list];
|
|
1218
|
+
case "Template": return node.pipe ? [node.pipe] : [];
|
|
1219
|
+
case "Block": return node.pipe ? [node.pipe, node.list] : [node.list];
|
|
1220
|
+
default: return assertNever(node);
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
//#endregion
|
|
1164
1224
|
//#region src/evaluator/access.ts
|
|
1165
1225
|
/**
|
|
1166
1226
|
* Field / index access for evaluator primaries.
|
|
@@ -1335,11 +1395,11 @@ function eagerBuiltins(toString) {
|
|
|
1335
1395
|
},
|
|
1336
1396
|
slice: {
|
|
1337
1397
|
fn: (collection, ...indices) => {
|
|
1338
|
-
const i = indices.length >= 1 ?
|
|
1339
|
-
const j = indices.length >= 2 ?
|
|
1398
|
+
const i = indices.length >= 1 ? indices[0] : 0;
|
|
1399
|
+
const j = indices.length >= 2 ? indices[1] : void 0;
|
|
1340
1400
|
return typeof collection === "string" ? collection.slice(i, j) : collection.slice(i, j);
|
|
1341
1401
|
},
|
|
1342
|
-
argTypes: ["sliceable", "
|
|
1402
|
+
argTypes: ["sliceable", "int"]
|
|
1343
1403
|
},
|
|
1344
1404
|
print: {
|
|
1345
1405
|
fn: (...args) => goPrint(args, toString),
|
|
@@ -1715,17 +1775,49 @@ let internalCreateTemplate;
|
|
|
1715
1775
|
var Template = class Template {
|
|
1716
1776
|
source;
|
|
1717
1777
|
_evaluate;
|
|
1718
|
-
|
|
1778
|
+
_referencedFunctions;
|
|
1779
|
+
constructor(source, evaluate, referencedFunctions) {
|
|
1719
1780
|
this.source = source;
|
|
1720
1781
|
this._evaluate = evaluate;
|
|
1782
|
+
this._referencedFunctions = referencedFunctions;
|
|
1721
1783
|
}
|
|
1722
1784
|
static {
|
|
1723
|
-
internalCreateTemplate = (source, evaluate) => new Template(source, evaluate);
|
|
1785
|
+
internalCreateTemplate = (source, evaluate, referencedFunctions) => new Template(source, evaluate, referencedFunctions);
|
|
1724
1786
|
}
|
|
1725
1787
|
evaluate(scope) {
|
|
1726
1788
|
return this._evaluate(scope);
|
|
1727
1789
|
}
|
|
1790
|
+
/**
|
|
1791
|
+
* The FuncMap function names this template references — every identifier that
|
|
1792
|
+
* names a function anywhere in the template body or its `{{ define }}` blocks.
|
|
1793
|
+
*
|
|
1794
|
+
* This is a STATIC fact derived from the parsed AST, not an execution trace:
|
|
1795
|
+
* a name in the set is a function the template *can* invoke; a name absent
|
|
1796
|
+
* from the set is one it provably never invokes. Use it to ask "does this
|
|
1797
|
+
* template use helper X?" without evaluating it (e.g. to discover which
|
|
1798
|
+
* templates depend on a feature func) — robust where a source-text scan is
|
|
1799
|
+
* not, because it sees through whitespace, pipelines, and field/string
|
|
1800
|
+
* lookalikes.
|
|
1801
|
+
*
|
|
1802
|
+
* Built-in operators registered for every engine (`and`, `eq`, `index`,
|
|
1803
|
+
* `printf`, …) are reported the same as consumer funcs — a referenced name is
|
|
1804
|
+
* a referenced name regardless of who registered it.
|
|
1805
|
+
*/
|
|
1806
|
+
referencedFunctions() {
|
|
1807
|
+
return this._referencedFunctions;
|
|
1808
|
+
}
|
|
1728
1809
|
};
|
|
1810
|
+
function collectReferencedFunctions(parsed) {
|
|
1811
|
+
const names = /* @__PURE__ */ new Set();
|
|
1812
|
+
const collect = (root) => {
|
|
1813
|
+
walk(root, (node) => {
|
|
1814
|
+
if (node.type === "Identifier") names.add(node.ident);
|
|
1815
|
+
});
|
|
1816
|
+
};
|
|
1817
|
+
collect(parsed.root);
|
|
1818
|
+
for (const body of parsed.defines.values()) collect(body);
|
|
1819
|
+
return names;
|
|
1820
|
+
}
|
|
1729
1821
|
var Engine = class {
|
|
1730
1822
|
fromString;
|
|
1731
1823
|
toString;
|
|
@@ -1752,7 +1844,7 @@ var Engine = class {
|
|
|
1752
1844
|
*/
|
|
1753
1845
|
parse(source) {
|
|
1754
1846
|
const parsed = parse(source, this.delims);
|
|
1755
|
-
return internalCreateTemplate(parsed.source, (scope) => this.evalParsed(parsed, scope));
|
|
1847
|
+
return internalCreateTemplate(parsed.source, (scope) => this.evalParsed(parsed, scope), collectReferencedFunctions(parsed));
|
|
1756
1848
|
}
|
|
1757
1849
|
/**
|
|
1758
1850
|
* Convenience sugar for `parse(src).evaluate.bind(template)`.
|
|
@@ -2054,7 +2146,6 @@ function matchesArgType(declared, value, toString) {
|
|
|
2054
2146
|
case "reflective":
|
|
2055
2147
|
case "value": return true;
|
|
2056
2148
|
case "string": return typeof value === "string";
|
|
2057
|
-
case "number": return typeof value === "number" || typeof value === "bigint";
|
|
2058
2149
|
case "int": return typeof value === "number" && Number.isFinite(value) || typeof value === "bigint" && Number.isSafeInteger(Number(value));
|
|
2059
2150
|
case "float": return typeof value === "number" || typeof value === "bigint" && Number.isFinite(Number(value));
|
|
2060
2151
|
case "bool": return typeof value === "boolean";
|
|
@@ -2078,6 +2169,7 @@ function matchesArgType(declared, value, toString) {
|
|
|
2078
2169
|
}
|
|
2079
2170
|
case "liftable": return typeof value === "string" || value !== null && value !== void 0 && typeof value !== "number" && typeof value !== "boolean" && typeof value !== "bigint" && typeof value !== "symbol";
|
|
2080
2171
|
case "serializable": return isJsonSerializable(value);
|
|
2172
|
+
default: throw new Error(`invalid ArgType: ${String(declared)}`);
|
|
2081
2173
|
}
|
|
2082
2174
|
}
|
|
2083
2175
|
function defaultFromString(s) {
|
|
@@ -2150,8 +2242,8 @@ function humanArgType(t) {
|
|
|
2150
2242
|
case "reflective":
|
|
2151
2243
|
case "value":
|
|
2152
2244
|
case "string":
|
|
2153
|
-
case "number":
|
|
2154
2245
|
case "bool": return t;
|
|
2246
|
+
default: throw new Error(`invalid ArgType: ${String(t)}`);
|
|
2155
2247
|
}
|
|
2156
2248
|
}
|
|
2157
2249
|
function describeValue(value) {
|
|
@@ -2216,7 +2308,7 @@ function numberValue(n) {
|
|
|
2216
2308
|
function atoi(s) {
|
|
2217
2309
|
if (!/^[+-]?\d+$/.test(s)) return 0;
|
|
2218
2310
|
const n = Number.parseInt(s, 10);
|
|
2219
|
-
return Number.
|
|
2311
|
+
return Number.isSafeInteger(n) ? n : 0;
|
|
2220
2312
|
}
|
|
2221
2313
|
//#endregion
|
|
2222
2314
|
//#region src/sprig/conversions/float64.ts
|
|
@@ -2301,13 +2393,19 @@ function parseBase0(s) {
|
|
|
2301
2393
|
* parseable → 0.
|
|
2302
2394
|
*/
|
|
2303
2395
|
function int(v) {
|
|
2304
|
-
if (typeof v === "number")
|
|
2305
|
-
|
|
2396
|
+
if (typeof v === "number") {
|
|
2397
|
+
const n = Math.trunc(v);
|
|
2398
|
+
return Number.isSafeInteger(n) ? n : 0;
|
|
2399
|
+
}
|
|
2400
|
+
if (typeof v === "bigint") {
|
|
2401
|
+
const n = Number(v);
|
|
2402
|
+
return Number.isSafeInteger(n) ? n : 0;
|
|
2403
|
+
}
|
|
2306
2404
|
if (typeof v === "string") {
|
|
2307
2405
|
const parsed = parseBase0(v);
|
|
2308
2406
|
if (parsed === null) return 0;
|
|
2309
|
-
const n = Number.parseInt(parsed.digits, parsed.base);
|
|
2310
|
-
return Number.
|
|
2407
|
+
const n = parsed.sign * Number.parseInt(parsed.digits, parsed.base);
|
|
2408
|
+
return Number.isSafeInteger(n) ? n : 0;
|
|
2311
2409
|
}
|
|
2312
2410
|
if (typeof v === "boolean") return v ? 1 : 0;
|
|
2313
2411
|
return 0;
|
|
@@ -2319,7 +2417,15 @@ function int(v) {
|
|
|
2319
2417
|
* full Go-`int64` precision survives the round-trip (a JS `number`
|
|
2320
2418
|
* loses precision past 2^53).
|
|
2321
2419
|
*
|
|
2322
|
-
*
|
|
2420
|
+
* Shares `parseBase0`'s base-detect + sign rules with `int`, so the
|
|
2421
|
+
* input shape (what string forms are recognized) is the same. The
|
|
2422
|
+
* **carrier** differs — `int` returns a JS `number` and applies a
|
|
2423
|
+
* `Number.isSafeInteger` clamp on output so the returned value
|
|
2424
|
+
* round-trips without precision loss; `int64`'s `bigint` carrier
|
|
2425
|
+
* holds the full int64 range, so no clamp is needed and string
|
|
2426
|
+
* forms like `"9007199254740993"` (past 2^53) survive unchanged.
|
|
2427
|
+
* The divergence is intentional: the carrier *forces* the precision
|
|
2428
|
+
* policy at this boundary, not the other way around.
|
|
2323
2429
|
*/
|
|
2324
2430
|
function int64(v) {
|
|
2325
2431
|
if (typeof v === "bigint") return v;
|
|
@@ -2353,8 +2459,8 @@ function toDecimal(s) {
|
|
|
2353
2459
|
if (!/^[+-]?[0-7]+$/.test(s)) return 0;
|
|
2354
2460
|
const sign = s[0] === "-" ? -1 : 1;
|
|
2355
2461
|
const body = s[0] === "+" || s[0] === "-" ? s.slice(1) : s;
|
|
2356
|
-
const n = Number.parseInt(body, 8);
|
|
2357
|
-
return Number.
|
|
2462
|
+
const n = sign * Number.parseInt(body, 8);
|
|
2463
|
+
return Number.isSafeInteger(n) ? n : 0;
|
|
2358
2464
|
}
|
|
2359
2465
|
//#endregion
|
|
2360
2466
|
//#region src/sprig/conversions/toRawJson.ts
|
|
@@ -2400,12 +2506,12 @@ function sprigConversions() {
|
|
|
2400
2506
|
atoi: {
|
|
2401
2507
|
fn: (s) => atoi(s),
|
|
2402
2508
|
argTypes: ["string"],
|
|
2403
|
-
returnType: "
|
|
2509
|
+
returnType: "int"
|
|
2404
2510
|
},
|
|
2405
2511
|
int: {
|
|
2406
2512
|
fn: (v) => int(v),
|
|
2407
2513
|
argTypes: ["value"],
|
|
2408
|
-
returnType: "
|
|
2514
|
+
returnType: "int"
|
|
2409
2515
|
},
|
|
2410
2516
|
int64: {
|
|
2411
2517
|
fn: (v) => int64(v),
|
|
@@ -2414,7 +2520,7 @@ function sprigConversions() {
|
|
|
2414
2520
|
float64: {
|
|
2415
2521
|
fn: (v) => float64(v),
|
|
2416
2522
|
argTypes: ["value"],
|
|
2417
|
-
returnType: "
|
|
2523
|
+
returnType: "float"
|
|
2418
2524
|
},
|
|
2419
2525
|
toString: {
|
|
2420
2526
|
fn: (v) => toString(v),
|
|
@@ -2428,7 +2534,7 @@ function sprigConversions() {
|
|
|
2428
2534
|
toDecimal: {
|
|
2429
2535
|
fn: (s) => toDecimal(s),
|
|
2430
2536
|
argTypes: ["string"],
|
|
2431
|
-
returnType: "
|
|
2537
|
+
returnType: "int"
|
|
2432
2538
|
},
|
|
2433
2539
|
toRawJson: {
|
|
2434
2540
|
fn: (v) => toRawJson(v),
|
|
@@ -3950,7 +4056,7 @@ function append(list, item) {
|
|
|
3950
4056
|
//#region src/sprig/lists/chunk.ts
|
|
3951
4057
|
/** `chunk size list` — split list into sub-lists of `size`. */
|
|
3952
4058
|
function chunk(size, list) {
|
|
3953
|
-
const n = Math.max(1,
|
|
4059
|
+
const n = Math.max(1, size);
|
|
3954
4060
|
const out = [];
|
|
3955
4061
|
for (let i = 0; i < list.length; i += n) out.push(list.slice(i, i + n));
|
|
3956
4062
|
return out;
|
|
@@ -4112,8 +4218,8 @@ function reverse(list) {
|
|
|
4112
4218
|
//#region src/sprig/lists/slice.ts
|
|
4113
4219
|
/** `slice list [i [j]]` — Array.prototype.slice. */
|
|
4114
4220
|
function slice(list, i, j) {
|
|
4115
|
-
const start = i === void 0 ? 0 :
|
|
4116
|
-
const end = j === void 0 ? list.length :
|
|
4221
|
+
const start = i === void 0 ? 0 : i;
|
|
4222
|
+
const end = j === void 0 ? list.length : j;
|
|
4117
4223
|
return list.slice(start, end);
|
|
4118
4224
|
}
|
|
4119
4225
|
//#endregion
|
|
@@ -4202,8 +4308,8 @@ function sprigLists() {
|
|
|
4202
4308
|
fn: (l, i, j) => slice(l, i, j),
|
|
4203
4309
|
argTypes: [
|
|
4204
4310
|
"list",
|
|
4205
|
-
"
|
|
4206
|
-
"
|
|
4311
|
+
"int",
|
|
4312
|
+
"int"
|
|
4207
4313
|
]
|
|
4208
4314
|
},
|
|
4209
4315
|
concat: {
|
|
@@ -4212,7 +4318,7 @@ function sprigLists() {
|
|
|
4212
4318
|
},
|
|
4213
4319
|
chunk: {
|
|
4214
4320
|
fn: (s, l) => chunk(s, l),
|
|
4215
|
-
argTypes: ["
|
|
4321
|
+
argTypes: ["int", "list"]
|
|
4216
4322
|
},
|
|
4217
4323
|
prepend: {
|
|
4218
4324
|
fn: (l, i) => prepend(l, i),
|
|
@@ -4576,28 +4682,28 @@ function randIntFromRange(min, max, random) {
|
|
|
4576
4682
|
//#region src/sprig/random/randAlpha.ts
|
|
4577
4683
|
/** `randAlpha n` — `n` random letters [A-Za-z]. */
|
|
4578
4684
|
function randAlpha(n, random) {
|
|
4579
|
-
return randString(
|
|
4685
|
+
return randString(n, ALPHA, random);
|
|
4580
4686
|
}
|
|
4581
4687
|
//#endregion
|
|
4582
4688
|
//#region src/sprig/random/randAlphaNum.ts
|
|
4583
4689
|
/** `randAlphaNum n` — `n` random alphanumeric characters [A-Za-z0-9]. */
|
|
4584
4690
|
function randAlphaNum(n, random) {
|
|
4585
|
-
return randString(
|
|
4691
|
+
return randString(n, ALPHANUM, random);
|
|
4586
4692
|
}
|
|
4587
4693
|
//#endregion
|
|
4588
4694
|
//#region src/sprig/random/randAscii.ts
|
|
4589
4695
|
/** `randAscii n` — `n` random printable ASCII characters (0x20–0x7E). */
|
|
4590
4696
|
function randAscii(n, random) {
|
|
4591
|
-
return randString(
|
|
4697
|
+
return randString(n, ASCII, random);
|
|
4592
4698
|
}
|
|
4593
4699
|
//#endregion
|
|
4594
4700
|
//#region src/sprig/random/randInt.ts
|
|
4595
4701
|
/** `randInt min max` — random integer in [min, max). Mirrors Go sprig. */
|
|
4596
4702
|
function randInt(min, max, random) {
|
|
4597
4703
|
try {
|
|
4598
|
-
return randIntFromRange(
|
|
4704
|
+
return randIntFromRange(min, max, random);
|
|
4599
4705
|
} catch (e) {
|
|
4600
|
-
if (e instanceof RandIntRangeError) throw bodyTypeMismatch("randInt",
|
|
4706
|
+
if (e instanceof RandIntRangeError) throw bodyTypeMismatch("randInt", 2, "max > min", `constraint violated (max=${max}, min=${min})`);
|
|
4601
4707
|
throw e;
|
|
4602
4708
|
}
|
|
4603
4709
|
}
|
|
@@ -4605,7 +4711,7 @@ function randInt(min, max, random) {
|
|
|
4605
4711
|
//#region src/sprig/random/randNumeric.ts
|
|
4606
4712
|
/** `randNumeric n` — `n` random decimal digits [0-9]. */
|
|
4607
4713
|
function randNumeric(n, random) {
|
|
4608
|
-
return randString(
|
|
4714
|
+
return randString(n, NUMERIC, random);
|
|
4609
4715
|
}
|
|
4610
4716
|
//#endregion
|
|
4611
4717
|
//#region src/sprig/random/shuffle.ts
|
|
@@ -4636,23 +4742,23 @@ function sprigRandom(random = Math.random) {
|
|
|
4636
4742
|
return {
|
|
4637
4743
|
randInt: {
|
|
4638
4744
|
fn: (min, max) => randInt(min, max, random),
|
|
4639
|
-
argTypes: ["
|
|
4745
|
+
argTypes: ["int", "int"]
|
|
4640
4746
|
},
|
|
4641
4747
|
randAlpha: {
|
|
4642
4748
|
fn: (n) => randAlpha(n, random),
|
|
4643
|
-
argTypes: ["
|
|
4749
|
+
argTypes: ["int"]
|
|
4644
4750
|
},
|
|
4645
4751
|
randAlphaNum: {
|
|
4646
4752
|
fn: (n) => randAlphaNum(n, random),
|
|
4647
|
-
argTypes: ["
|
|
4753
|
+
argTypes: ["int"]
|
|
4648
4754
|
},
|
|
4649
4755
|
randNumeric: {
|
|
4650
4756
|
fn: (n) => randNumeric(n, random),
|
|
4651
|
-
argTypes: ["
|
|
4757
|
+
argTypes: ["int"]
|
|
4652
4758
|
},
|
|
4653
4759
|
randAscii: {
|
|
4654
4760
|
fn: (n) => randAscii(n, random),
|
|
4655
|
-
argTypes: ["
|
|
4761
|
+
argTypes: ["int"]
|
|
4656
4762
|
},
|
|
4657
4763
|
shuffle: {
|
|
4658
4764
|
fn: (s) => shuffle(s, random),
|
|
@@ -4675,11 +4781,10 @@ function regexFind(pattern, s) {
|
|
|
4675
4781
|
*/
|
|
4676
4782
|
function regexFindAll(pattern, s, n = -1) {
|
|
4677
4783
|
const re = new RegExp(pattern, "g");
|
|
4678
|
-
const limit = Number(n);
|
|
4679
4784
|
const out = [];
|
|
4680
4785
|
for (const m of s.matchAll(re)) {
|
|
4786
|
+
if (n >= 0 && out.length >= n) break;
|
|
4681
4787
|
out.push(m[0]);
|
|
4682
|
-
if (limit >= 0 && out.length >= limit) break;
|
|
4683
4788
|
}
|
|
4684
4789
|
return out;
|
|
4685
4790
|
}
|
|
@@ -4714,9 +4819,8 @@ function regexReplaceAllLiteral(pattern, s, repl) {
|
|
|
4714
4819
|
/** `regexSplit pattern s n` — split by regex, optional max count. */
|
|
4715
4820
|
function regexSplit(pattern, s, n = -1) {
|
|
4716
4821
|
const re = new RegExp(pattern);
|
|
4717
|
-
|
|
4718
|
-
|
|
4719
|
-
return s.split(re).slice(0, limit);
|
|
4822
|
+
if (n < 0) return s.split(re);
|
|
4823
|
+
return s.split(re).slice(0, n);
|
|
4720
4824
|
}
|
|
4721
4825
|
//#endregion
|
|
4722
4826
|
//#region src/sprig/regex/index.ts
|
|
@@ -4735,7 +4839,7 @@ function sprigRegex() {
|
|
|
4735
4839
|
argTypes: [
|
|
4736
4840
|
"string",
|
|
4737
4841
|
"string",
|
|
4738
|
-
"
|
|
4842
|
+
"int"
|
|
4739
4843
|
]
|
|
4740
4844
|
},
|
|
4741
4845
|
regexReplaceAll: {
|
|
@@ -4759,7 +4863,7 @@ function sprigRegex() {
|
|
|
4759
4863
|
argTypes: [
|
|
4760
4864
|
"string",
|
|
4761
4865
|
"string",
|
|
4762
|
-
"
|
|
4866
|
+
"int"
|
|
4763
4867
|
]
|
|
4764
4868
|
}
|
|
4765
4869
|
};
|
|
@@ -4966,10 +5070,9 @@ function sprigSemver() {
|
|
|
4966
5070
|
* behavior).
|
|
4967
5071
|
*/
|
|
4968
5072
|
function abbrev(width, s) {
|
|
4969
|
-
|
|
4970
|
-
if (
|
|
4971
|
-
|
|
4972
|
-
return `${s.slice(0, w - 3)}...`;
|
|
5073
|
+
if (s.length <= width) return s;
|
|
5074
|
+
if (width < 4) return s;
|
|
5075
|
+
return `${s.slice(0, width - 3)}...`;
|
|
4973
5076
|
}
|
|
4974
5077
|
//#endregion
|
|
4975
5078
|
//#region src/sprig/strings/abbrevboth.ts
|
|
@@ -4980,11 +5083,9 @@ function abbrev(width, s) {
|
|
|
4980
5083
|
* a slice taken between them.
|
|
4981
5084
|
*/
|
|
4982
5085
|
function abbrevboth(left, right, s) {
|
|
4983
|
-
|
|
4984
|
-
|
|
4985
|
-
|
|
4986
|
-
if (l < 0 || l > s.length - r + 1) return s;
|
|
4987
|
-
return `...${s.slice(l, l + r - 6)}...`;
|
|
5086
|
+
if (right < 7 || s.length < right) return s;
|
|
5087
|
+
if (left < 0 || left > s.length - right + 1) return s;
|
|
5088
|
+
return `...${s.slice(left, left + right - 6)}...`;
|
|
4988
5089
|
}
|
|
4989
5090
|
//#endregion
|
|
4990
5091
|
//#region src/sprig/strings/caseUtils.ts
|
|
@@ -5306,7 +5407,7 @@ function hasSuffix(suffix, s) {
|
|
|
5306
5407
|
//#region src/sprig/strings/indent.ts
|
|
5307
5408
|
/** `indent n s` — prefix every line of `s` with `n` spaces. */
|
|
5308
5409
|
function indent(n, s) {
|
|
5309
|
-
const pad = " ".repeat(Math.max(0,
|
|
5410
|
+
const pad = " ".repeat(Math.max(0, n));
|
|
5310
5411
|
return s.split("\n").map((line) => pad + line).join("\n");
|
|
5311
5412
|
}
|
|
5312
5413
|
//#endregion
|
|
@@ -5382,7 +5483,7 @@ function nospace(s) {
|
|
|
5382
5483
|
* Mirrors Go sprig's `plural(one, many string, count int) string`.
|
|
5383
5484
|
*/
|
|
5384
5485
|
function plural(one, many, n) {
|
|
5385
|
-
return n === 1
|
|
5486
|
+
return n === 1 ? one : many;
|
|
5386
5487
|
}
|
|
5387
5488
|
//#endregion
|
|
5388
5489
|
//#region src/sprig/strings/quote.ts
|
|
@@ -5406,8 +5507,7 @@ function regexQuoteMeta(s) {
|
|
|
5406
5507
|
//#endregion
|
|
5407
5508
|
//#region src/sprig/strings/repeat.ts
|
|
5408
5509
|
function repeat(n, s) {
|
|
5409
|
-
|
|
5410
|
-
return s.repeat(count);
|
|
5510
|
+
return s.repeat(Math.max(0, n));
|
|
5411
5511
|
}
|
|
5412
5512
|
//#endregion
|
|
5413
5513
|
//#region src/sprig/strings/replace.ts
|
|
@@ -5467,7 +5567,7 @@ function splitList(sep, s) {
|
|
|
5467
5567
|
* explicitly to recover the Go behavior.
|
|
5468
5568
|
*/
|
|
5469
5569
|
function splitn(sep, n, s) {
|
|
5470
|
-
const parts = goSplitN(s, sep,
|
|
5570
|
+
const parts = goSplitN(s, sep, n);
|
|
5471
5571
|
const res = {};
|
|
5472
5572
|
for (let i = 0; i < parts.length; i++) res[`_${i}`] = parts[i] ?? "";
|
|
5473
5573
|
return res;
|
|
@@ -5490,13 +5590,13 @@ function squote(...args) {
|
|
|
5490
5590
|
//#region src/sprig/strings/substr.ts
|
|
5491
5591
|
/**
|
|
5492
5592
|
* `substr i j s` — substring [i, j) with out-of-range clamping.
|
|
5493
|
-
* Matches Go sprig's behavior of clamping rather than throwing
|
|
5593
|
+
* Matches Go sprig's behavior of clamping rather than throwing:
|
|
5594
|
+
* negative `end` (and any `end > s.length`) clamps to `s.length`;
|
|
5595
|
+
* `start` clamps to `[0, end]`.
|
|
5494
5596
|
*/
|
|
5495
5597
|
function substr(start, end, s) {
|
|
5496
|
-
|
|
5497
|
-
|
|
5498
|
-
if (j > s.length) j = s.length;
|
|
5499
|
-
if (i > j) i = j;
|
|
5598
|
+
const j = end < 0 || end > s.length ? s.length : end;
|
|
5599
|
+
const i = Math.min(Math.max(0, start), j);
|
|
5500
5600
|
return s.slice(i, j);
|
|
5501
5601
|
}
|
|
5502
5602
|
//#endregion
|
|
@@ -5567,13 +5667,11 @@ function trimSuffix(suffix, s) {
|
|
|
5567
5667
|
* Strings shorter than |n| are returned unchanged. Matches Go sprig.
|
|
5568
5668
|
*/
|
|
5569
5669
|
function trunc(n, s) {
|
|
5570
|
-
|
|
5571
|
-
|
|
5572
|
-
if (len < 0) {
|
|
5573
|
-
const k = -len;
|
|
5670
|
+
if (n < 0) {
|
|
5671
|
+
const k = -n;
|
|
5574
5672
|
return k >= s.length ? s : s.slice(s.length - k);
|
|
5575
5673
|
}
|
|
5576
|
-
return
|
|
5674
|
+
return n >= s.length ? s : s.slice(0, n);
|
|
5577
5675
|
}
|
|
5578
5676
|
//#endregion
|
|
5579
5677
|
//#region src/sprig/strings/untitle.ts
|
|
@@ -5593,10 +5691,10 @@ function upper(s) {
|
|
|
5593
5691
|
* Lines longer than width that contain no spaces are emitted as-is.
|
|
5594
5692
|
*/
|
|
5595
5693
|
function wrap(width, s) {
|
|
5596
|
-
return wrapWithSeparator(
|
|
5694
|
+
return wrapWithSeparator(width, "\n", s);
|
|
5597
5695
|
}
|
|
5598
5696
|
function wrapWithSeparator(width, sep, s) {
|
|
5599
|
-
if (
|
|
5697
|
+
if (width <= 0) return s;
|
|
5600
5698
|
const out = [];
|
|
5601
5699
|
for (const para of s.split("\n")) {
|
|
5602
5700
|
let line = "";
|
|
@@ -5617,7 +5715,7 @@ function wrapWithSeparator(width, sep, s) {
|
|
|
5617
5715
|
//#region src/sprig/strings/wrapWith.ts
|
|
5618
5716
|
/** `wrapWith width sep s` — wrap to width using `sep` instead of \n. */
|
|
5619
5717
|
function wrapWith(width, sep, s) {
|
|
5620
|
-
return wrapWithSeparator(
|
|
5718
|
+
return wrapWithSeparator(width, sep, s);
|
|
5621
5719
|
}
|
|
5622
5720
|
//#endregion
|
|
5623
5721
|
//#region src/sprig/strings/index.ts
|
|
@@ -5625,13 +5723,13 @@ function sprigStrings() {
|
|
|
5625
5723
|
return {
|
|
5626
5724
|
abbrev: {
|
|
5627
5725
|
fn: (w, s) => abbrev(w, s),
|
|
5628
|
-
argTypes: ["
|
|
5726
|
+
argTypes: ["int", "string"]
|
|
5629
5727
|
},
|
|
5630
5728
|
abbrevboth: {
|
|
5631
5729
|
fn: (l, r, s) => abbrevboth(l, r, s),
|
|
5632
5730
|
argTypes: [
|
|
5633
|
-
"
|
|
5634
|
-
"
|
|
5731
|
+
"int",
|
|
5732
|
+
"int",
|
|
5635
5733
|
"string"
|
|
5636
5734
|
]
|
|
5637
5735
|
},
|
|
@@ -5658,7 +5756,7 @@ function sprigStrings() {
|
|
|
5658
5756
|
},
|
|
5659
5757
|
indent: {
|
|
5660
5758
|
fn: (n, s) => indent(n, s),
|
|
5661
|
-
argTypes: ["
|
|
5759
|
+
argTypes: ["int", "string"]
|
|
5662
5760
|
},
|
|
5663
5761
|
initials: {
|
|
5664
5762
|
fn: (s) => initials(s),
|
|
@@ -5679,7 +5777,7 @@ function sprigStrings() {
|
|
|
5679
5777
|
},
|
|
5680
5778
|
nindent: {
|
|
5681
5779
|
fn: (n, s) => nindent(n, s),
|
|
5682
|
-
argTypes: ["
|
|
5780
|
+
argTypes: ["int", "string"]
|
|
5683
5781
|
},
|
|
5684
5782
|
nospace: {
|
|
5685
5783
|
fn: (s) => nospace(s),
|
|
@@ -5691,7 +5789,7 @@ function sprigStrings() {
|
|
|
5691
5789
|
argTypes: [
|
|
5692
5790
|
"string",
|
|
5693
5791
|
"string",
|
|
5694
|
-
"
|
|
5792
|
+
"int"
|
|
5695
5793
|
],
|
|
5696
5794
|
returnType: "string"
|
|
5697
5795
|
},
|
|
@@ -5706,7 +5804,7 @@ function sprigStrings() {
|
|
|
5706
5804
|
},
|
|
5707
5805
|
repeat: {
|
|
5708
5806
|
fn: (n, s) => repeat(n, s),
|
|
5709
|
-
argTypes: ["
|
|
5807
|
+
argTypes: ["int", "string"]
|
|
5710
5808
|
},
|
|
5711
5809
|
replace: {
|
|
5712
5810
|
fn: (o, n, s) => replace(o, n, s),
|
|
@@ -5733,7 +5831,7 @@ function sprigStrings() {
|
|
|
5733
5831
|
fn: (sep, n, s) => splitn(sep, n, s),
|
|
5734
5832
|
argTypes: [
|
|
5735
5833
|
"string",
|
|
5736
|
-
"
|
|
5834
|
+
"int",
|
|
5737
5835
|
"string"
|
|
5738
5836
|
],
|
|
5739
5837
|
returnType: "dict"
|
|
@@ -5745,8 +5843,8 @@ function sprigStrings() {
|
|
|
5745
5843
|
substr: {
|
|
5746
5844
|
fn: (i, j, s) => substr(i, j, s),
|
|
5747
5845
|
argTypes: [
|
|
5748
|
-
"
|
|
5749
|
-
"
|
|
5846
|
+
"int",
|
|
5847
|
+
"int",
|
|
5750
5848
|
"string"
|
|
5751
5849
|
]
|
|
5752
5850
|
},
|
|
@@ -5777,7 +5875,7 @@ function sprigStrings() {
|
|
|
5777
5875
|
},
|
|
5778
5876
|
trunc: {
|
|
5779
5877
|
fn: (n, s) => trunc(n, s),
|
|
5780
|
-
argTypes: ["
|
|
5878
|
+
argTypes: ["int", "string"]
|
|
5781
5879
|
},
|
|
5782
5880
|
untitle: {
|
|
5783
5881
|
fn: (s) => untitle(s),
|
|
@@ -5789,12 +5887,12 @@ function sprigStrings() {
|
|
|
5789
5887
|
},
|
|
5790
5888
|
wrap: {
|
|
5791
5889
|
fn: (w, s) => wrap(w, s),
|
|
5792
|
-
argTypes: ["
|
|
5890
|
+
argTypes: ["int", "string"]
|
|
5793
5891
|
},
|
|
5794
5892
|
wrapWith: {
|
|
5795
5893
|
fn: (w, sep, s) => wrapWith(w, sep, s),
|
|
5796
5894
|
argTypes: [
|
|
5797
|
-
"
|
|
5895
|
+
"int",
|
|
5798
5896
|
"string",
|
|
5799
5897
|
"string"
|
|
5800
5898
|
]
|