@mark1russell7/vex 0.13.4 → 0.15.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/build/index.d.ts +37 -16
- package/build/index.d.ts.map +1 -1
- package/build/index.js +467 -94
- package/build/index.js.map +1 -1
- package/package.json +2 -2
- package/src/builder.ts +76 -13
- package/src/checked.ts +6 -0
- package/src/deps.ts +3 -1
- package/src/document.ts +24 -7
- package/src/evaluate.ts +271 -68
- package/src/ir.ts +25 -10
- package/src/item-names.ts +29 -0
- package/src/kernel.ts +83 -11
- package/src/list.ts +4 -6
- package/src/write.ts +7 -2
package/src/ir.ts
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
* shape as the expressions of `render`.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
import { itemNamesError } from "./item-names.ts";
|
|
7
|
+
|
|
6
8
|
/**
|
|
7
9
|
* One move of an address. The moves of an address apply in order, from the focus of the evaluation. The key of `key`
|
|
8
10
|
* and the position of `index` can be an expression, which evaluates before the moves (NAV.COMPUTED).
|
|
@@ -48,8 +50,8 @@ export type Expr =
|
|
|
48
50
|
| { readonly tag: "let"; readonly bind: Readonly<Record<string, Expr>>; readonly body: Expr }
|
|
49
51
|
| { readonly tag: "var"; readonly name: string }
|
|
50
52
|
| { readonly tag: "rec"; readonly fields: Readonly<Record<string, Expr>> }
|
|
51
|
-
| { readonly tag: "each"; readonly axis: Axis; readonly body: Expr; readonly at?: Addr }
|
|
52
|
-
| { readonly tag: "fold"; readonly axis: Axis; readonly init: Names; readonly step: Names; readonly result: Expr; readonly at?: Addr }
|
|
53
|
+
| { readonly tag: "each"; readonly axis: Axis; readonly body: Expr; readonly at?: Addr; readonly as?: ItemNames }
|
|
54
|
+
| { readonly tag: "fold"; readonly axis: Axis; readonly init: Names; readonly step: Names; readonly result: Expr; readonly at?: Addr; readonly as?: ItemNames }
|
|
53
55
|
| { readonly tag: "iterate"; readonly limit: number; readonly init: Names; readonly step: Names; readonly until?: Expr; readonly result: Expr }
|
|
54
56
|
| { readonly tag: "fn"; readonly params: readonly string[]; readonly body: Expr }
|
|
55
57
|
| { readonly tag: "call"; readonly fn: string; readonly args: readonly Expr[] }
|
|
@@ -58,6 +60,12 @@ export type Expr =
|
|
|
58
60
|
/** The names of a loop and an expression for each (LOOP.NAMES). */
|
|
59
61
|
export type Names = Readonly<Record<string, Expr>>;
|
|
60
62
|
|
|
63
|
+
/**
|
|
64
|
+
* The names of the items of a loop over the axis `list`. The first name holds the value of the item, and the second
|
|
65
|
+
* its position (AXIS.LIST.ITEM, AXIS.LIST.INDEX). The body of `each` and the steps of `fold` see them (AXIS.LIST.SCOPE).
|
|
66
|
+
*/
|
|
67
|
+
export type ItemNames = readonly [item: string] | readonly [item: string, index: string];
|
|
68
|
+
|
|
61
69
|
/** The largest number of steps of `iterate` (ITERATE.LIMIT). */
|
|
62
70
|
export const MAX_ITERATIONS = 100_000;
|
|
63
71
|
|
|
@@ -96,18 +104,23 @@ export const rec = (fields: Readonly<Record<string, Expr>>): ExprOf<"rec"> => ({
|
|
|
96
104
|
|
|
97
105
|
/**
|
|
98
106
|
* This function evaluates `body` once for each target of `axis`. The result is a list. With `at`, the axis finds its
|
|
99
|
-
* targets from the key of that address, not from the focus.
|
|
107
|
+
* targets from the key of that address, not from the focus. With `as`, the body of a loop over the axis `list` reads the
|
|
108
|
+
* value of each item and its position by these names (AXIS.LIST.ITEM, AXIS.LIST.INDEX).
|
|
100
109
|
*/
|
|
101
|
-
export function each(axis: Axis, body: Expr, at?: Addr): ExprOf<"each"> {
|
|
102
|
-
|
|
110
|
+
export function each(axis: Axis, body: Expr, at?: Addr, as?: ItemNames): ExprOf<"each"> {
|
|
111
|
+
const named = as === undefined ? {} : { as };
|
|
112
|
+
return at === undefined || at.length === 0 ? { tag: "each", axis, body, ...named } : { tag: "each", axis, body, at, ...named };
|
|
103
113
|
}
|
|
104
114
|
|
|
105
115
|
/**
|
|
106
116
|
* This function makes a fold: a step at each target of `axis`, in order. `init` gives the names and their first
|
|
107
|
-
* values, and `step` gives the next values. `result` gives the value from the last values (LOOP.NAMES, FOLD.ORDER).
|
|
117
|
+
* values, and `step` gives the next values. `result` gives the value from the last values (LOOP.NAMES, FOLD.ORDER). With
|
|
118
|
+
* `as`, the steps of a fold over the axis `list` read the value of each item and its position by these names
|
|
119
|
+
* (AXIS.LIST.ITEM, AXIS.LIST.INDEX).
|
|
108
120
|
*/
|
|
109
|
-
export function fold(axis: Axis, init: Names, step: Names, result: Expr, at?: Addr): ExprOf<"fold"> {
|
|
110
|
-
|
|
121
|
+
export function fold(axis: Axis, init: Names, step: Names, result: Expr, at?: Addr, as?: ItemNames): ExprOf<"fold"> {
|
|
122
|
+
const named = as === undefined ? {} : { as };
|
|
123
|
+
return at === undefined || at.length === 0 ? { tag: "fold", axis, init, step, result, ...named } : { tag: "fold", axis, init, step, result, at, ...named };
|
|
111
124
|
}
|
|
112
125
|
|
|
113
126
|
/**
|
|
@@ -277,6 +290,8 @@ export function shapeError(u: unknown): string | undefined {
|
|
|
277
290
|
if (bad.axis) return "the expression has an axis that is not valid";
|
|
278
291
|
if (typeof tag === "string" && !TAGS.has(tag)) return `the expression has a node with the tag "${tag}", and this version of Vex does not know it`;
|
|
279
292
|
if (tag === "fn") return "the expression has a function outside the bindings of a let, or a function with parameters that are not distinct names";
|
|
293
|
+
const named = tag === "each" || tag === "fold" ? itemNamesError(bad.u as Readonly<Record<string, unknown>>) : undefined;
|
|
294
|
+
if (named !== undefined) return `the expression has a node "${String(tag)}" with names "as" that are not valid: ${named}`;
|
|
280
295
|
return typeof tag === "string" ? `the expression has a node "${tag}" with parts that are not valid` : "the expression has a part that is not a node";
|
|
281
296
|
}
|
|
282
297
|
|
|
@@ -375,7 +390,7 @@ function badPart(todo: { readonly axis: boolean; readonly u: unknown }[], whole:
|
|
|
375
390
|
break;
|
|
376
391
|
case "each": {
|
|
377
392
|
const at = addrParts(u["at"], moveNumber);
|
|
378
|
-
if (at === undefined) return item;
|
|
393
|
+
if (at === undefined || itemNamesError(u) !== undefined) return item;
|
|
379
394
|
push(true, [u["axis"]]);
|
|
380
395
|
push(false, [u["body"], ...at]);
|
|
381
396
|
break;
|
|
@@ -383,7 +398,7 @@ function badPart(todo: { readonly axis: boolean; readonly u: unknown }[], whole:
|
|
|
383
398
|
case "fold": {
|
|
384
399
|
const at = addrParts(u["at"], moveNumber);
|
|
385
400
|
const names = loopParts(u);
|
|
386
|
-
if (at === undefined || names === undefined) return item;
|
|
401
|
+
if (at === undefined || names === undefined || itemNamesError(u) !== undefined) return item;
|
|
387
402
|
push(true, [u["axis"]]);
|
|
388
403
|
push(false, [...names, ...at]);
|
|
389
404
|
break;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The check of the names `as` of a loop over the axis `list` (AXIS.LIST.SCOPE). The package index does not export this
|
|
3
|
+
* module: `isExpr`, `shapeError` and `fromDocument` use it, so an expression and a document follow the same rule.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const isObject = (u: unknown): u is Readonly<Record<string, unknown>> => typeof u === "object" && u !== null;
|
|
7
|
+
|
|
8
|
+
/** This function tells if the base axis of an axis is `list`, through `where` and `reverse`. It uses a loop, not recursion. */
|
|
9
|
+
function isListAxis(u: unknown): boolean {
|
|
10
|
+
let x = u;
|
|
11
|
+
while (isObject(x) && x["t"] === "where") x = x["axis"];
|
|
12
|
+
if (isObject(x) && x["t"] === "reverse") x = x["axis"];
|
|
13
|
+
return isObject(x) && x["t"] === "list";
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* This function gives the reason that the names `as` of a loop are not valid, or `undefined` for valid names or no
|
|
18
|
+
* names (AXIS.LIST.SCOPE). The names are one or two distinct texts, the axis is `list`, and no name is a name of `init`.
|
|
19
|
+
*/
|
|
20
|
+
export function itemNamesError(u: Readonly<Record<string, unknown>>): string | undefined {
|
|
21
|
+
const as = u["as"];
|
|
22
|
+
if (as === undefined) return undefined;
|
|
23
|
+
if (!Array.isArray(as) || as.length === 0 || as.length > 2 || !as.every((n) => typeof n === "string")) return '"as" is a list of one or two names: the item, and then its index';
|
|
24
|
+
if (as[0] === as[1]) return `the two names of "as" are the same name "${String(as[0])}"`;
|
|
25
|
+
if (!isListAxis(u["axis"])) return '"as" needs the axis "list", also inside "reverse" and "where"';
|
|
26
|
+
const init = u["init"];
|
|
27
|
+
const clash = isObject(init) ? (as as readonly string[]).find((n) => Object.hasOwn(init, n)) : undefined;
|
|
28
|
+
return clash === undefined ? undefined : `the name "${clash}" of "as" is a name of "init" too`;
|
|
29
|
+
}
|
package/src/kernel.ts
CHANGED
|
@@ -74,6 +74,9 @@ const move = (walker: string, parts: PartShape): Kind => ({ verbs: { eval: "form
|
|
|
74
74
|
/** The scope of a loop: the names of `init`, for the steps, the test `until` and the result (LOOP.NAMES). */
|
|
75
75
|
const LOOP_SCOPE = [{ names: { keysAt: ["init"] }, in: [["step", "*"], ["until"], ["result"]] }] as const;
|
|
76
76
|
|
|
77
|
+
/** The names of the items of a loop over the axis `list`: one or two distinct texts (AXIS.LIST.SCOPE). */
|
|
78
|
+
const ITEM_NAMES: PartShape = { shape: "list", item: { shape: "text" }, distinct: true };
|
|
79
|
+
|
|
77
80
|
/**
|
|
78
81
|
* This function gives the world of Vex. The module uses it one time, and the annotation `__PURE__` lets a bundler
|
|
79
82
|
* remove the world from a program that does not use it.
|
|
@@ -101,11 +104,17 @@ function worldOfVex(): World {
|
|
|
101
104
|
summary: "A function of a let. Its body evaluates at the position of each call.",
|
|
102
105
|
},
|
|
103
106
|
call: { verbs: { eval: "vex.call" }, parts: closed({ fn: { shape: "term", kinds: [["ref"]] }, args: TERMS }, ["fn", "args"]), summary: "A call of a function of a let." },
|
|
104
|
-
each: {
|
|
107
|
+
each: {
|
|
108
|
+
verbs: { eval: "vex.each" },
|
|
109
|
+
parts: closed({ axis: AXIS, as: ITEM_NAMES, body: TERM, at: ADDRESS }, ["axis", "body"]),
|
|
110
|
+
// The names of the items are names of the body only. The test of where does not see them (AXIS.LIST.SCOPE).
|
|
111
|
+
scope: [{ names: { listAt: ["as"] }, in: [["body"]] }],
|
|
112
|
+
summary: "The body at each target of an axis: a list.",
|
|
113
|
+
},
|
|
105
114
|
fold: {
|
|
106
115
|
verbs: { eval: "vex.fold" },
|
|
107
|
-
parts: closed({ axis: AXIS, init: NAMES, step: NAMES, result: TERM, at: ADDRESS }, ["axis", "init", "step", "result"]),
|
|
108
|
-
scope: LOOP_SCOPE,
|
|
116
|
+
parts: closed({ axis: AXIS, as: ITEM_NAMES, init: NAMES, step: NAMES, result: TERM, at: ADDRESS }, ["axis", "init", "step", "result"]),
|
|
117
|
+
scope: [...LOOP_SCOPE, { names: { listAt: ["as"] }, in: [["step", "*"]] }],
|
|
109
118
|
summary: "A step at each target of an axis, in order, and a result.",
|
|
110
119
|
},
|
|
111
120
|
iterate: {
|
|
@@ -312,10 +321,15 @@ const WALKERS: Readonly<Record<string, Walker>> = {
|
|
|
312
321
|
* (`vexDefinitions`).
|
|
313
322
|
*/
|
|
314
323
|
|
|
315
|
-
/**
|
|
324
|
+
/**
|
|
325
|
+
* A target of an axis: its key, or its key and an error (AXIS.WHERE, AXIS.LIST). A target of the axis `list` also has
|
|
326
|
+
* the value of its item and its position as `nth` counts it (AXIS.LIST.ITEM, AXIS.LIST.INDEX).
|
|
327
|
+
*/
|
|
316
328
|
interface Target {
|
|
317
329
|
readonly key: string;
|
|
318
330
|
readonly error?: VexError;
|
|
331
|
+
readonly value?: unknown;
|
|
332
|
+
readonly index?: number;
|
|
319
333
|
}
|
|
320
334
|
|
|
321
335
|
/** The value of an axis document: the targets from a position. `table` names the table of the axis table. */
|
|
@@ -375,7 +389,9 @@ const axisEvaluator: EvalFn = ({ typePath, value, context }) => {
|
|
|
375
389
|
// LIST.ARRAY: an array is a list with the positions as keys.
|
|
376
390
|
const items = asList(r.value, missing);
|
|
377
391
|
if (items === undefined) return failed("kind-mismatch", `the axis "list" needs a list, not a ${describe(r.value)}`);
|
|
378
|
-
|
|
392
|
+
// The index counts the items with a value, as nth does: nth skips the error items (LIST.LENIENT).
|
|
393
|
+
let index = 0;
|
|
394
|
+
return ok(items.items.map(({ key, result }): Target => (result.ok ? { key, value: result.value, index: index++ } : { key, error: result.error })));
|
|
379
395
|
},
|
|
380
396
|
});
|
|
381
397
|
}
|
|
@@ -418,7 +434,8 @@ function whereTargets(base: Targets, test: Part, ctx: Context, from: Place): Out
|
|
|
418
434
|
}
|
|
419
435
|
const r = test.evaluate({ ...ctx, self: placeOf(base, from, t.key) });
|
|
420
436
|
if (!r.ok) out.push({ key: t.key, error: toVexError(r.error) });
|
|
421
|
-
|
|
437
|
+
// The target keeps the value and the index of its item (AXIS.LIST.ITEM).
|
|
438
|
+
else if (r.value === true) out.push(t);
|
|
422
439
|
else if (r.value !== false) out.push({ key: t.key, error: vexError("kind-mismatch", `the test of "where" gave a ${typeof r.value}, not a boolean`) });
|
|
423
440
|
}
|
|
424
441
|
return ok(out);
|
|
@@ -462,6 +479,22 @@ function bindAll(record: Part | undefined, ctx: Context, names: Readonly<Record<
|
|
|
462
479
|
return ok(out);
|
|
463
480
|
}
|
|
464
481
|
|
|
482
|
+
/**
|
|
483
|
+
* This function adds the names `as` of a loop to the names of a body or of a step. The first name holds the value of
|
|
484
|
+
* the item, and the second its index (AXIS.LIST.ITEM, AXIS.LIST.INDEX). A loop without `as` keeps its names.
|
|
485
|
+
*/
|
|
486
|
+
function withItem(names: Readonly<Record<string, unknown>>, as: readonly string[] | undefined, t: Target): Readonly<Record<string, unknown>> {
|
|
487
|
+
if (as === undefined) return names;
|
|
488
|
+
const out: Record<string, unknown> = { ...names };
|
|
489
|
+
const [item, index] = as;
|
|
490
|
+
setName(out, item as string, t.value);
|
|
491
|
+
if (index !== undefined) setName(out, index, t.index);
|
|
492
|
+
return out;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** This function gives the names `as` of a loop, or `undefined` (AXIS.LIST.SCOPE). */
|
|
496
|
+
const itemNamesOf = (value: unknown): readonly string[] | undefined => (value as { readonly as?: readonly string[] }).as;
|
|
497
|
+
|
|
465
498
|
/** The evaluators of the loops: each, fold and iterate (LOOP.NAMES, FOLD.ORDER, ITERATE.LIMIT). */
|
|
466
499
|
const LOOPS: Readonly<Record<string, EvalFn>> = {
|
|
467
500
|
"vex.each": ({ value, context }) => {
|
|
@@ -469,7 +502,11 @@ const LOOPS: Readonly<Record<string, EvalFn>> = {
|
|
|
469
502
|
if (!found.ok) return found;
|
|
470
503
|
const { axis, from, targets } = found.value;
|
|
471
504
|
const body = partAt(value, "body");
|
|
472
|
-
const
|
|
505
|
+
const as = itemNamesOf(value);
|
|
506
|
+
const items: ListItem[] = targets.map((t) => ({
|
|
507
|
+
key: t.key,
|
|
508
|
+
result: t.error === undefined ? toVexResult(body.evaluate({ ...context, self: placeOf(axis, from, t.key), names: withItem(context.names, as, t) })) : vexFail(t.error),
|
|
509
|
+
}));
|
|
473
510
|
return ok(vexList(items));
|
|
474
511
|
},
|
|
475
512
|
|
|
@@ -482,9 +519,11 @@ const LOOPS: Readonly<Record<string, EvalFn>> = {
|
|
|
482
519
|
const found = targetsOf(value, context);
|
|
483
520
|
if (!found.ok) return found;
|
|
484
521
|
const { axis, from, targets } = found.value;
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
522
|
+
const as = itemNamesOf(value);
|
|
523
|
+
for (const t of targets) {
|
|
524
|
+
if (t.error !== undefined) return { ok: false, error: toKernelError(t.error) };
|
|
525
|
+
// The step sees the names of the item, and the next names do not keep them (AXIS.LIST.SCOPE).
|
|
526
|
+
const next = bindAll(step, { ...context, self: placeOf(axis, from, t.key), names: withItem(names, as, t) }, names);
|
|
488
527
|
if (!next.ok) return next;
|
|
489
528
|
names = next.value;
|
|
490
529
|
}
|
|
@@ -634,7 +673,7 @@ function callFound(found: Found, op: string, self: unknown, args: readonly unkno
|
|
|
634
673
|
}
|
|
635
674
|
|
|
636
675
|
/** The list ops with a second argument before their options: a count, a position, an op name or a list. */
|
|
637
|
-
const WITH_ARGUMENT: ReadonlySet<string> = /* @__PURE__ */ new Set(["reduce", "take", "nth", "drop", "append", "without", "get", "indexOf"]);
|
|
676
|
+
const WITH_ARGUMENT: ReadonlySet<string> = /* @__PURE__ */ new Set(["reduce", "take", "nth", "drop", "append", "without", "get", "indexOf", "group", "join"]);
|
|
638
677
|
|
|
639
678
|
/**
|
|
640
679
|
* This function gives the items of a list that have a value (LIST.LENIENT). A list op does not skip `#DEPTH!`, so that
|
|
@@ -665,10 +704,31 @@ function flat(vs: readonly unknown[], strict: boolean): unknown {
|
|
|
665
704
|
return vexList(out);
|
|
666
705
|
}
|
|
667
706
|
|
|
707
|
+
/**
|
|
708
|
+
* This function gives the groups of `group(list, keys)` (LIST.GROUP). The item at a position has the key at the same
|
|
709
|
+
* position. A pair with an error item or an error key is not in a group.
|
|
710
|
+
*/
|
|
711
|
+
function group(items: readonly ListItem[], keys: unknown, strict: boolean): unknown {
|
|
712
|
+
goodOf(items, strict);
|
|
713
|
+
const list = asList(keys, missing);
|
|
714
|
+
if (list === undefined) return stop("kind-mismatch", `"group" needs a list of keys, not a ${describe(keys)}`);
|
|
715
|
+
goodOf(list.items, strict);
|
|
716
|
+
if (list.items.length !== items.length) return stop("kind-mismatch", '"group" needs one key for each item');
|
|
717
|
+
const groups = new Map<string, ListItem[]>();
|
|
718
|
+
items.forEach((it, i) => {
|
|
719
|
+
const k = (list.items[i] as ListItem).result;
|
|
720
|
+
if (!it.result.ok || !k.ok) return;
|
|
721
|
+
if (typeof k.value !== "string") stop("kind-mismatch", '"group" needs text keys');
|
|
722
|
+
groups.set(k.value as string, [...(groups.get(k.value as string) ?? []), it]);
|
|
723
|
+
});
|
|
724
|
+
return vexList([...groups].map(([key, g]): ListItem => ({ key, result: vexOk(vexList(g)) })));
|
|
725
|
+
}
|
|
726
|
+
|
|
668
727
|
/** This function applies a list op (LIST.*). The error items are skipped, except with `{ strict: true }`, and except `#DEPTH!`. */
|
|
669
728
|
function listOp(op: string, items: readonly ListItem[], rest: readonly unknown[], domains: readonly AnyDomain[]): unknown {
|
|
670
729
|
const o = WITH_ARGUMENT.has(op) ? rest[1] : rest[0];
|
|
671
730
|
const strict = (o as { strict?: unknown } | null | undefined)?.strict === true;
|
|
731
|
+
if (op === "group") return group(items, rest[0], strict);
|
|
672
732
|
const good = goodOf(items, strict);
|
|
673
733
|
const vs = good.map((it) => (it.result.ok ? it.result.value : undefined));
|
|
674
734
|
const nums = (): readonly number[] => (vs.every((x) => typeof x === "number") ? vs : stop("kind-mismatch", `"${op}" needs numbers`));
|
|
@@ -715,6 +775,18 @@ function listOp(op: string, items: readonly ListItem[], rest: readonly unknown[]
|
|
|
715
775
|
const i = good.findIndex((it) => it.key === key);
|
|
716
776
|
return i >= 0 ? vs[i] : stop("missing-field", `the list has no item with the key ${JSON.stringify(key)}`);
|
|
717
777
|
}
|
|
778
|
+
case "distinct": {
|
|
779
|
+
// The first item of each value, by ===, as for indexOf. Thus each NaN stays (LIST.DISTINCT).
|
|
780
|
+
if (!vs.every(isIndexValue)) return stop("kind-mismatch", '"distinct" needs numbers, texts or booleans');
|
|
781
|
+
const keep = vs.map((x, i) => vs.indexOf(x) === i || Number.isNaN(x));
|
|
782
|
+
return vexList(good.filter((_it, i) => keep[i]));
|
|
783
|
+
}
|
|
784
|
+
case "join": {
|
|
785
|
+
// The texts of the items with the text sep between them (LIST.JOIN).
|
|
786
|
+
const sep = rest[0];
|
|
787
|
+
if (typeof sep !== "string") return stop("kind-mismatch", `"join" needs a text separator, not a ${describe(sep)}`);
|
|
788
|
+
return vs.every((x) => typeof x === "string") ? vs.join(sep) : stop("kind-mismatch", '"join" needs texts');
|
|
789
|
+
}
|
|
718
790
|
case "indexOf": {
|
|
719
791
|
// The position of the first value, among the items with a value (LIST.INDEXOF).
|
|
720
792
|
const x = rest[0];
|
package/src/list.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { CHECKED } from "./checked.ts";
|
|
1
2
|
import type { Result } from "./result.ts";
|
|
2
3
|
|
|
3
4
|
/** One item of a list: the key of the target and the result of the body there. */
|
|
@@ -15,9 +16,6 @@ export interface VexList<T = unknown> {
|
|
|
15
16
|
/** This function makes a list. */
|
|
16
17
|
export const vexList = <T>(items: readonly ListItem<T>[]): VexList<T> => Object.freeze({ kind: "vex.list", items: Object.freeze([...items]) });
|
|
17
18
|
|
|
18
|
-
/** The lists that passed the check of `isVexList` and cannot change. Each list of `vexList` is frozen. */
|
|
19
|
-
const CHECKED = new WeakSet<object>();
|
|
20
|
-
|
|
21
19
|
/** This function tells if a value is an error value: a text `code`, `kind` and `message`, and a `path` (LIST.SHAPE). */
|
|
22
20
|
const isErrorValue = (u: unknown): boolean => {
|
|
23
21
|
if (typeof u !== "object" || u === null) return false;
|
|
@@ -54,15 +52,15 @@ export const isVexList = (u: unknown): u is VexList => {
|
|
|
54
52
|
/**
|
|
55
53
|
* The name of a core list op. `sortBy`, `take`, `drop`, `append`, `flat` and `without` give a list (LIST.BUILD), and
|
|
56
54
|
* the other ops give a value. `get` gives the value at a key (LIST.GET), and `indexOf` gives the position of a value
|
|
57
|
-
* (LIST.INDEXOF).
|
|
55
|
+
* (LIST.INDEXOF). `distinct` and `group` give a list, and `join` gives a text (LIST.DISTINCT, LIST.GROUP, LIST.JOIN).
|
|
58
56
|
*/
|
|
59
|
-
export type ListOp = "count" | "sum" | "min" | "max" | "mean" | "any" | "all" | "none" | "values" | "keys" | "reduce" | "first" | "errors" | "sortBy" | "take" | "nth" | "drop" | "append" | "flat" | "without" | "get" | "indexOf";
|
|
57
|
+
export type ListOp = "count" | "sum" | "min" | "max" | "mean" | "any" | "all" | "none" | "values" | "keys" | "reduce" | "first" | "errors" | "sortBy" | "take" | "nth" | "drop" | "append" | "flat" | "without" | "get" | "indexOf" | "distinct" | "group" | "join";
|
|
60
58
|
|
|
61
59
|
/**
|
|
62
60
|
* The names of the core list ops. They apply when the first argument is a list. They apply to an array too, when no
|
|
63
61
|
* domain and no function of the host has the op for it (LIST.ARRAY).
|
|
64
62
|
*/
|
|
65
|
-
export const LIST_OPS: ReadonlySet<string> = new Set<ListOp>(["count", "sum", "min", "max", "mean", "any", "all", "none", "values", "keys", "reduce", "first", "errors", "sortBy", "take", "nth", "drop", "append", "flat", "without", "get", "indexOf"]);
|
|
63
|
+
export const LIST_OPS: ReadonlySet<string> = new Set<ListOp>(["count", "sum", "min", "max", "mean", "any", "all", "none", "values", "keys", "reduce", "first", "errors", "sortBy", "take", "nth", "drop", "append", "flat", "without", "get", "indexOf", "distinct", "group", "join"]);
|
|
66
64
|
|
|
67
65
|
/** This function tells if a name is the name of a core list op. */
|
|
68
66
|
export const isListOp = (op: string): op is ListOp => LIST_OPS.has(op);
|
package/src/write.ts
CHANGED
|
@@ -50,6 +50,9 @@ const NONE: readonly Job[] = [];
|
|
|
50
50
|
const term = (e: Expr): Job => ({ t: "term", e });
|
|
51
51
|
const envelope = (e: Expr): Job => ({ t: "envelope", e });
|
|
52
52
|
|
|
53
|
+
/** This function gives the field `as` of a loop: a new list of its names, or no field (AXIS.LIST.SCOPE). */
|
|
54
|
+
const itemsOf = (as: readonly string[] | undefined): { readonly as?: Json } => (as === undefined ? {} : { as: [...as] });
|
|
55
|
+
|
|
53
56
|
/** This function gives a record from names and the documents of their values, from an index of the documents. */
|
|
54
57
|
function namesOf(names: readonly string[], docs: readonly Json[], from: number): JsonRecord {
|
|
55
58
|
const out: Record<string, Json> = {};
|
|
@@ -148,7 +151,8 @@ class Writer {
|
|
|
148
151
|
const axis = docs[0] as Json;
|
|
149
152
|
const body = docs[1] as Json;
|
|
150
153
|
const address = at.make(docs.slice(2));
|
|
151
|
-
|
|
154
|
+
const as = itemsOf(e.as);
|
|
155
|
+
return this.#keep(typed("each", reads ? { body, axis, ...as, ...address } : { axis, ...as, body, ...address }), e);
|
|
152
156
|
},
|
|
153
157
|
};
|
|
154
158
|
}
|
|
@@ -164,7 +168,8 @@ class Writer {
|
|
|
164
168
|
const names = { init: namesOf(init, docs, 1), step: namesOf(step, docs, 1 + init.length) };
|
|
165
169
|
const result = docs[after] as Json;
|
|
166
170
|
const address = at.make(docs.slice(after + 1));
|
|
167
|
-
|
|
171
|
+
const as = itemsOf(e.as);
|
|
172
|
+
return this.#keep(typed("fold", reads ? { ...names, axis, ...as, ...address, result } : { axis, ...as, ...names, result, ...address }), e);
|
|
168
173
|
},
|
|
169
174
|
};
|
|
170
175
|
}
|