@mark1russell7/vex 0.10.0 → 0.11.1
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 +42 -7
- package/build/index.d.ts.map +1 -1
- package/build/index.js +104 -13
- package/build/index.js.map +1 -1
- package/package.json +2 -2
- package/src/builder.ts +72 -19
- package/src/evaluate.ts +48 -1
- package/src/kernel.ts +8 -1
- package/src/list.ts +38 -6
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/vex",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.11.1",
|
|
5
5
|
"description": "Vex: typed spreadsheet formulas over domain objects. The kernel and the typed builder.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"typescript",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"access": "public"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@mark1russell7/coeval": "^0.6.
|
|
52
|
+
"@mark1russell7/coeval": "^0.6.3"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@mark1russell7/cue": "github:mark1russell7/cue",
|
package/src/builder.ts
CHANGED
|
@@ -7,10 +7,10 @@ import type { VexError } from "./errors.ts";
|
|
|
7
7
|
import { evaluate, explain, type EvalOptions, type ExtHandler, type FreeFn } from "./evaluate.ts";
|
|
8
8
|
import { app, call as callExpr, each, ext, fn as fnExpr, fold as foldExpr, index as indexMove, iterate as iterateExpr, key as keyMove, let_, lit, offset as offsetMove, other as otherMove, origin as originMove, parent as parentMove, rec, ref, toPath, v, type Axis, type Expr, type FixedMove, type Move } from "./ir.ts";
|
|
9
9
|
import { cell as cellRef, SheetRun, type CellFormulas } from "./sheet.ts";
|
|
10
|
-
import { toOptional, type Optional, type Result } from "./result.ts";
|
|
10
|
+
import { ok, toOptional, type Optional, type Result } from "./result.ts";
|
|
11
11
|
import type { Space } from "./space.ts";
|
|
12
12
|
import type { Trace } from "./trace.ts";
|
|
13
|
-
import type
|
|
13
|
+
import { vexList, type VexList } from "./list.ts";
|
|
14
14
|
import { traversal, type Traversal } from "./traversal.ts";
|
|
15
15
|
|
|
16
16
|
// ---------------------------------------------------------------- argument tokens
|
|
@@ -211,8 +211,16 @@ export interface ChainBase<Ctx extends ChainContext, T> {
|
|
|
211
211
|
fork<const B extends Readonly<Record<string, (c: ChainOf<Ctx, T>) => ChainBase<Ctx, unknown>>>>(branches: B): ChainOf<Ctx, { readonly [N in keyof B]: ValueOf<ReturnType<B[N]>> }>;
|
|
212
212
|
/**
|
|
213
213
|
* This method makes up to `limit` steps (ITERATE.LIMIT). `init` gives the names and their first values, and `step`
|
|
214
|
-
* gives the next values. A true `until` stops the steps (ITERATE.UNTIL). `result` gives the value of the chain.
|
|
214
|
+
* gives the next values. A true `until` stops the steps (ITERATE.UNTIL). `result` gives the value of the chain. A
|
|
215
|
+
* name with a list chain as its first value is a list chain (BUILD.LIST.LOOP), and a list result gives a list chain.
|
|
215
216
|
*/
|
|
217
|
+
iterate<const I extends Readonly<Record<string, unknown>>, U>(
|
|
218
|
+
limit: number,
|
|
219
|
+
init: I,
|
|
220
|
+
step: (vars: LoopVars<Ctx, I>) => NoInfer<LoopStep<Ctx, I>>,
|
|
221
|
+
result: (vars: LoopVars<Ctx, I>) => ListChain<Ctx, U>,
|
|
222
|
+
opts?: { readonly until?: (vars: LoopVars<Ctx, I>) => ArgInput<boolean, Ctx, false> },
|
|
223
|
+
): ListChain<Ctx, U>;
|
|
216
224
|
iterate<const I extends Readonly<Record<string, unknown>>, R>(
|
|
217
225
|
limit: number,
|
|
218
226
|
init: I,
|
|
@@ -309,15 +317,29 @@ export type ArgValue<Ctx extends ChainContext, A> = A extends Arg<infer T>
|
|
|
309
317
|
? NonNullable<Ctx["record"][A]>
|
|
310
318
|
: A;
|
|
311
319
|
|
|
312
|
-
/** A chain whose value is a list: the result of an axis. */
|
|
313
320
|
/** The type of a value of a name of a loop: a literal of `init` gives its primitive type. */
|
|
314
321
|
type LoopValue<Ctx extends ChainContext, A> = ArgValue<Ctx, A> extends infer T ? (T extends number ? number : T extends string ? string : T extends boolean ? boolean : T) : never;
|
|
315
322
|
|
|
323
|
+
/** The type of the items of a list chain, or `never` for another value. */
|
|
324
|
+
type ItemsOf<A> = A extends { readonly kind: "vex.list-chain"; readonly [CHAIN_VALUE]?: VexList<infer U> } ? U : never;
|
|
325
|
+
|
|
326
|
+
/** A value of `init` that is a list chain: then the name of the loop holds a list (LOOP.LIST). */
|
|
327
|
+
type IsList<A> = A extends { readonly kind: "vex.list-chain" } ? true : false;
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* The chain of a name of a loop: a list chain for a list chain of `init` (BUILD.LIST.LOOP), and else a chain of its
|
|
331
|
+
* value.
|
|
332
|
+
*/
|
|
333
|
+
export type LoopVar<Ctx extends ChainContext, A> = IsList<A> extends true ? ListChain<Ctx, ItemsOf<A>> : ChainOf<Ctx, LoopValue<Ctx, A>>;
|
|
334
|
+
|
|
316
335
|
/** The names of a loop of the builder: a chain of the value of each name of `init` (LOOP.NAMES). */
|
|
317
|
-
export type LoopVars<Ctx extends ChainContext, I> = { readonly [N in keyof I]:
|
|
336
|
+
export type LoopVars<Ctx extends ChainContext, I> = { readonly [N in keyof I]: LoopVar<Ctx, I[N]> };
|
|
337
|
+
|
|
338
|
+
/** The next value of a name of a loop. A name of a list takes a list chain with items of the same type. */
|
|
339
|
+
export type LoopNext<Ctx extends ChainContext, A> = IsList<A> extends true ? ListChain<Ctx, ItemsOf<A>> : ArgInput<LoopValue<Ctx, A>, Ctx, false>;
|
|
318
340
|
|
|
319
341
|
/** The next values of a step of a loop: a value for some of the names of `init`. */
|
|
320
|
-
export type LoopStep<Ctx extends ChainContext, I> = { readonly [N in keyof I]?:
|
|
342
|
+
export type LoopStep<Ctx extends ChainContext, I> = { readonly [N in keyof I]?: LoopNext<Ctx, I[N]> };
|
|
321
343
|
|
|
322
344
|
/** The kind of a parameter of a function of the builder (BUILD.FN): a primitive type, or `any`. */
|
|
323
345
|
export type FnParamKind = PrimitiveKind | "any";
|
|
@@ -354,6 +376,7 @@ export interface ListBody<U> {
|
|
|
354
376
|
readonly program: Expr;
|
|
355
377
|
}
|
|
356
378
|
|
|
379
|
+
/** A chain whose value is a list: the result of an axis, of a list op or of a list literal. */
|
|
357
380
|
export interface ListChain<Ctx extends ChainContext, U> {
|
|
358
381
|
readonly kind: "vex.list-chain";
|
|
359
382
|
/** This phantom member carries the type of the list. It is not present at run time. */
|
|
@@ -364,6 +387,11 @@ export interface ListChain<Ctx extends ChainContext, U> {
|
|
|
364
387
|
* This method folds the items in the order of the axis (FOLD.ORDER). `init` gives the names and their first values.
|
|
365
388
|
* `step` gives the next values from the names and the item, a chain at the target. `result` gives the value.
|
|
366
389
|
*/
|
|
390
|
+
fold<const I extends Readonly<Record<string, unknown>>, V>(
|
|
391
|
+
init: I,
|
|
392
|
+
step: (vars: LoopVars<Ctx, I>, item: ChainOf<Ctx, U>) => NoInfer<LoopStep<Ctx, I>>,
|
|
393
|
+
result: (vars: LoopVars<Ctx, I>) => ListChain<Ctx, V>,
|
|
394
|
+
): ListChain<Ctx, V>;
|
|
367
395
|
fold<const I extends Readonly<Record<string, unknown>>, R>(
|
|
368
396
|
init: I,
|
|
369
397
|
step: (vars: LoopVars<Ctx, I>, item: ChainOf<Ctx, U>) => NoInfer<LoopStep<Ctx, I>>,
|
|
@@ -390,6 +418,11 @@ export interface ListChain<Ctx extends ChainContext, U> {
|
|
|
390
418
|
take(n: number, opts?: ListOpts): ListChain<Ctx, U>;
|
|
391
419
|
/** The value of the item at the position `i`, from 0 (LIST.NTH). */
|
|
392
420
|
nth(i: number, opts?: ListOpts): ChainOf<Ctx, U>;
|
|
421
|
+
/**
|
|
422
|
+
* The value of the first item with the key `k` (LIST.GET). A chain of a text gives the key, and it evaluates at the
|
|
423
|
+
* focus. A key that no item has gives `#N/A`. Thus a step reads the old value of each key.
|
|
424
|
+
*/
|
|
425
|
+
get(k: Ctx["keys"] | ChainBase<Ctx, string>, opts?: ListOpts): ChainOf<Ctx, U>;
|
|
393
426
|
/** The items after the first `n` items (LIST.DROP). Thus `drop(1)` removes the first item of a stack. */
|
|
394
427
|
drop(n: number, opts?: ListOpts): ListChain<Ctx, U>;
|
|
395
428
|
/** The items of this list, and then the items of `other` (LIST.APPEND). Two items can have the same key. */
|
|
@@ -438,6 +471,11 @@ export interface Root<Ctx extends ChainContext> {
|
|
|
438
471
|
ofPath<T>(k: Ctx["keys"], path: string): Arg<T>;
|
|
439
472
|
/** This method makes a record argument from field arguments. */
|
|
440
473
|
rec<const F extends Readonly<Record<string, unknown>>>(fields: F): Arg<{ readonly [N in keyof F]: ArgValue<Ctx, F[N]> }>;
|
|
474
|
+
/**
|
|
475
|
+
* This method makes a list literal (LIST.LITERAL, BUILD.LIST.LOOP): an item for each pair of a key and a value, in
|
|
476
|
+
* order. Without pairs, it gives the empty list. Thus a loop can start a stack or a seen set.
|
|
477
|
+
*/
|
|
478
|
+
list<U = unknown>(items?: readonly (readonly [Ctx["keys"], U])[]): ListChain<Ctx, U>;
|
|
441
479
|
/**
|
|
442
480
|
* This method makes an extension argument: the node `ext(kind, data)`. The handler of `kind` in the options of
|
|
443
481
|
* `withOptions` gives its value. The caller gives the type `T` of that value.
|
|
@@ -723,16 +761,18 @@ class ChainImpl {
|
|
|
723
761
|
* This method gives the names of a loop. Each name of `init` gets a name that is unique at the depth of the chain.
|
|
724
762
|
* It also gets its first value at the address of the chain, and a chain of its value.
|
|
725
763
|
*/
|
|
726
|
-
loopNames(init: Readonly<Record<string, unknown>>): { readonly unique: ReadonlyMap<string, string>; readonly first: Readonly<Record<string, Expr>>; readonly vars: Readonly<Record<string, ChainImpl>> } {
|
|
764
|
+
loopNames(init: Readonly<Record<string, unknown>>): { readonly unique: ReadonlyMap<string, string>; readonly first: Readonly<Record<string, Expr>>; readonly vars: Readonly<Record<string, ChainImpl | ListChainImpl>> } {
|
|
727
765
|
const depth = this.#s.depth;
|
|
728
766
|
const unique = new Map<string, string>();
|
|
729
767
|
const first: Record<string, Expr> = {};
|
|
730
|
-
const vars: Record<string, ChainImpl> = {};
|
|
768
|
+
const vars: Record<string, ChainImpl | ListChainImpl> = {};
|
|
731
769
|
for (const [n, a] of Object.entries(init)) {
|
|
732
770
|
const name = `${n}$${depth}`;
|
|
733
771
|
unique.set(n, name);
|
|
734
772
|
first[name] = toExpr(a, this.#s.addr);
|
|
735
|
-
|
|
773
|
+
// A name of a list is a list chain, so a step can use the list ops on it (BUILD.LIST.LOOP).
|
|
774
|
+
const s: State = { ...this.#s, expr: v(name), addr: [], depth: depth + 1 };
|
|
775
|
+
vars[n] = kindOf(a) === "vex.list-chain" ? new ListChainImpl(s) : new ChainImpl(s);
|
|
736
776
|
}
|
|
737
777
|
return { unique, first, vars };
|
|
738
778
|
}
|
|
@@ -740,14 +780,15 @@ class ChainImpl {
|
|
|
740
780
|
iterate(
|
|
741
781
|
limit: number,
|
|
742
782
|
init: Readonly<Record<string, unknown>>,
|
|
743
|
-
step: (vars: Readonly<Record<string, ChainImpl>>) => Readonly<Record<string, unknown>>,
|
|
744
|
-
result: (vars: Readonly<Record<string, ChainImpl>>) => unknown,
|
|
745
|
-
opts?: { readonly until?: (vars: Readonly<Record<string, ChainImpl>>) => unknown },
|
|
746
|
-
): ChainImpl {
|
|
783
|
+
step: (vars: Readonly<Record<string, ChainImpl | ListChainImpl>>) => Readonly<Record<string, unknown>>,
|
|
784
|
+
result: (vars: Readonly<Record<string, ChainImpl | ListChainImpl>>) => unknown,
|
|
785
|
+
opts?: { readonly until?: (vars: Readonly<Record<string, ChainImpl | ListChainImpl>>) => unknown },
|
|
786
|
+
): ChainImpl | ListChainImpl {
|
|
747
787
|
const { unique, first, vars } = this.loopNames(init);
|
|
748
788
|
const next = stepExprs(step(vars), unique, this.#s.addr);
|
|
749
789
|
const until = opts?.until === undefined ? undefined : toExpr(opts.until(vars), this.#s.addr);
|
|
750
|
-
|
|
790
|
+
const r = result(vars);
|
|
791
|
+
return chainOf({ ...this.#s, expr: iterateExpr(limit, first, next, toExpr(r, this.#s.addr), until) }, r);
|
|
751
792
|
}
|
|
752
793
|
|
|
753
794
|
#axis(a: Axis, body: (c: ChainImpl) => { readonly program: Expr }, opts?: AxisOptsImpl): ListChainImpl {
|
|
@@ -798,6 +839,9 @@ class ChainImpl {
|
|
|
798
839
|
}
|
|
799
840
|
}
|
|
800
841
|
|
|
842
|
+
/** This function gives the chain of a loop: a list chain when its result is a list chain (BUILD.LIST.LOOP). */
|
|
843
|
+
const chainOf = (s: State, result: unknown): ChainImpl | ListChainImpl => (kindOf(result) === "vex.list-chain" ? new ListChainImpl(s) : new ChainImpl(s));
|
|
844
|
+
|
|
801
845
|
/** This function gives the options of an evaluation of a chain or a list chain at the origin `k`. */
|
|
802
846
|
function evalOptions(s: State, k: string): EvalOptions {
|
|
803
847
|
const { env } = s;
|
|
@@ -823,9 +867,9 @@ class ListChainImpl {
|
|
|
823
867
|
}
|
|
824
868
|
fold(
|
|
825
869
|
init: Readonly<Record<string, unknown>>,
|
|
826
|
-
step: (vars: Readonly<Record<string, ChainImpl>>, item: ChainImpl) => Readonly<Record<string, unknown>>,
|
|
827
|
-
result: (vars: Readonly<Record<string, ChainImpl>>) => unknown,
|
|
828
|
-
): ChainImpl {
|
|
870
|
+
step: (vars: Readonly<Record<string, ChainImpl | ListChainImpl>>, item: ChainImpl) => Readonly<Record<string, unknown>>,
|
|
871
|
+
result: (vars: Readonly<Record<string, ChainImpl | ListChainImpl>>) => unknown,
|
|
872
|
+
): ChainImpl | ListChainImpl {
|
|
829
873
|
// The list of an axis is a let of the start value around the axis, and the list of `each` of a list is the axis
|
|
830
874
|
// alone. The fold keeps the let, and the body of the axis is the item of each step.
|
|
831
875
|
const e = this.#s.expr;
|
|
@@ -834,8 +878,9 @@ class ListChainImpl {
|
|
|
834
878
|
const names = new ChainImpl({ ...this.#s, expr: lit(null) }).loopNames(init);
|
|
835
879
|
const item = new ChainImpl({ ...this.#s, expr: loop.body, addr: [] });
|
|
836
880
|
const next = stepExprs(step(names.vars, item), names.unique, []);
|
|
837
|
-
const
|
|
838
|
-
|
|
881
|
+
const r = result(names.vars);
|
|
882
|
+
const folded = foldExpr(loop.axis, names.first, next, toExpr(r, this.#s.addr), loop.at);
|
|
883
|
+
return chainOf({ ...this.#s, expr: e.tag === "let" ? let_(e.bind, folded) : folded }, r);
|
|
839
884
|
}
|
|
840
885
|
|
|
841
886
|
#op(op: string, extra: readonly Expr[], opts?: ListOpts & { readonly domain?: string }): ChainImpl {
|
|
@@ -894,6 +939,10 @@ class ListChainImpl {
|
|
|
894
939
|
nth(i: number, opts?: ListOpts): ChainImpl {
|
|
895
940
|
return this.#op("nth", [lit(i)], opts);
|
|
896
941
|
}
|
|
942
|
+
get(k: string | { readonly program: Expr }, opts?: ListOpts): ChainImpl {
|
|
943
|
+
// A text is a key, not a field reference, as for the move to(k).
|
|
944
|
+
return this.#op("get", [typeof k === "string" ? lit(k) : k.program], opts);
|
|
945
|
+
}
|
|
897
946
|
drop(n: number, opts?: ListOpts): ListChainImpl {
|
|
898
947
|
return this.#list("drop", [lit(n)], opts);
|
|
899
948
|
}
|
|
@@ -968,6 +1017,10 @@ class RootImpl {
|
|
|
968
1017
|
for (const [n, a] of Object.entries(fields)) out[n] = toExpr(a, []);
|
|
969
1018
|
return argToken(rec(out));
|
|
970
1019
|
}
|
|
1020
|
+
list(items: readonly (readonly [string, unknown])[] = []): ListChainImpl {
|
|
1021
|
+
// LIST.LITERAL: a literal of the list, with a value for each item.
|
|
1022
|
+
return new ListChainImpl({ env: this.#env, expr: lit(vexList(items.map(([key, value]) => ({ key, result: ok(value) })))), addr: [], depth: 0 });
|
|
1023
|
+
}
|
|
971
1024
|
}
|
|
972
1025
|
|
|
973
1026
|
class SheetRootImpl extends RootImpl {
|
package/src/evaluate.ts
CHANGED
|
@@ -195,6 +195,8 @@ class Compiler {
|
|
|
195
195
|
readonly #domains: readonly AnyDomain[];
|
|
196
196
|
readonly #fns: Readonly<Record<string, FreeFn>> | undefined;
|
|
197
197
|
readonly #extensions: Readonly<Record<string, ExtHandler>> | undefined;
|
|
198
|
+
/** The index of the keys of each list that `get` read (LIST.GET). A list does not change, so its index stays right. */
|
|
199
|
+
readonly #keyIndex = new WeakMap<VexList, KeyIndex>();
|
|
198
200
|
|
|
199
201
|
constructor(opts: CompileOptions) {
|
|
200
202
|
// Stryker disable next-line ArrayDeclaration: a list with a value that is not a domain matches no value
|
|
@@ -732,6 +734,7 @@ class Compiler {
|
|
|
732
734
|
// The options come after the second argument of an op with one, for example the count of take.
|
|
733
735
|
const optsArg = WITH_ARGUMENT.has(op) ? rest[1] : rest[0];
|
|
734
736
|
const strict = (optsArg as ListOptions | null | undefined)?.strict === true;
|
|
737
|
+
if (op === "get") return this.#get(list, rest[0], strict, f, path);
|
|
735
738
|
const good = goodItems(list, strict);
|
|
736
739
|
if (!good.ok) return good;
|
|
737
740
|
const okItems = good.value;
|
|
@@ -819,6 +822,24 @@ class Compiler {
|
|
|
819
822
|
}
|
|
820
823
|
}
|
|
821
824
|
|
|
825
|
+
/**
|
|
826
|
+
* This method gives the value of the first item with a key (LIST.GET). LIST.LENIENT applies, so an error item is not
|
|
827
|
+
* an item of the key. The program keeps the index of each list, so a step that reads each key of a list reads it once.
|
|
828
|
+
*/
|
|
829
|
+
#get(list: VexList, key: unknown, strict: boolean, f: Frame, path: readonly number[]): Result<unknown> {
|
|
830
|
+
let index = this.#keyIndex.get(list);
|
|
831
|
+
// Stryker disable next-line ConditionalExpression: a new index of the same list has the same values
|
|
832
|
+
if (index === undefined) {
|
|
833
|
+
index = keyIndexOf(list);
|
|
834
|
+
this.#keyIndex.set(list, index);
|
|
835
|
+
}
|
|
836
|
+
// As goodItems: #DEPTH! comes first, and with strict, the first error item is the result.
|
|
837
|
+
const stop = index.depth ?? (strict ? index.first : undefined);
|
|
838
|
+
if (stop !== undefined) return fail(stop);
|
|
839
|
+
if (typeof key !== "string") return this.#err("kind-mismatch", `"get" needs a text key, not a ${describeType(key)}`, f, path, { op: "get" });
|
|
840
|
+
return index.values.has(key) ? ok(index.values.get(key)) : this.#err("missing-field", `the list has no item with the key ${JSON.stringify(key)}`, f, path, { op: "get" });
|
|
841
|
+
}
|
|
842
|
+
|
|
822
843
|
/**
|
|
823
844
|
* This method joins the lists that are the values of the items of a list, in order (LIST.FLAT). Each list follows the
|
|
824
845
|
* rules of LIST.BUILD. A value that is not a list gives `#VALUE!`.
|
|
@@ -886,7 +907,7 @@ class Compiler {
|
|
|
886
907
|
}
|
|
887
908
|
|
|
888
909
|
/** The list ops with a second argument before their options: a count, a position, an op name or a list. */
|
|
889
|
-
const WITH_ARGUMENT: ReadonlySet<ListOp> = new Set<ListOp>(["reduce", "take", "nth", "drop", "append", "without"]);
|
|
910
|
+
const WITH_ARGUMENT: ReadonlySet<ListOp> = new Set<ListOp>(["reduce", "take", "nth", "drop", "append", "without", "get"]);
|
|
890
911
|
|
|
891
912
|
/**
|
|
892
913
|
* This function gives the items of a list that have a value (LIST.LENIENT). A list op does not skip `#DEPTH!`, so that
|
|
@@ -900,6 +921,32 @@ function goodItems(list: VexList, strict: boolean): Result<readonly ListItem[]>
|
|
|
900
921
|
return ok(list.items.filter((it) => it.result.ok));
|
|
901
922
|
}
|
|
902
923
|
|
|
924
|
+
/** The index of the keys of a list (LIST.GET): the value of the first item of each key, and the first errors. */
|
|
925
|
+
interface KeyIndex {
|
|
926
|
+
readonly values: ReadonlyMap<string, unknown>;
|
|
927
|
+
/** The first error item. With strict, it is the result. */
|
|
928
|
+
readonly first: VexError | undefined;
|
|
929
|
+
/** The first item of `#DEPTH!`. A list op does not skip it (EVAL.DEPTH). */
|
|
930
|
+
readonly depth: VexError | undefined;
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
/** This function makes the index of the keys of a list. An error item is not an item of its key (LIST.LENIENT). */
|
|
934
|
+
function keyIndexOf(list: VexList): KeyIndex {
|
|
935
|
+
const values = new Map<string, unknown>();
|
|
936
|
+
let first: VexError | undefined;
|
|
937
|
+
let depth: VexError | undefined;
|
|
938
|
+
for (const it of list.items) {
|
|
939
|
+
if (it.result.ok) {
|
|
940
|
+
// The first item of a key gives its value. A later item with the same key does not replace it.
|
|
941
|
+
if (!values.has(it.key)) values.set(it.key, it.result.value);
|
|
942
|
+
} else {
|
|
943
|
+
first ??= it.result.error;
|
|
944
|
+
if (isDepthError(it.result.error)) depth ??= it.result.error;
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
return { values, first, depth };
|
|
948
|
+
}
|
|
949
|
+
|
|
903
950
|
const FORBIDDEN_FIELDS = new Set(["__proto__", "constructor", "prototype"]);
|
|
904
951
|
/** This function tells if a segment of a path can name a field. A compiler uses the same rule as the interpreter. */
|
|
905
952
|
export const isFieldName = (seg: string): boolean => seg.length > 0 && !FORBIDDEN_FIELDS.has(seg);
|
package/src/kernel.ts
CHANGED
|
@@ -628,7 +628,7 @@ function callFound(found: Found, op: string, self: unknown, args: readonly unkno
|
|
|
628
628
|
}
|
|
629
629
|
|
|
630
630
|
/** The list ops with a second argument before their options: a count, a position, an op name or a list. */
|
|
631
|
-
const WITH_ARGUMENT: ReadonlySet<string> = /* @__PURE__ */ new Set(["reduce", "take", "nth", "drop", "append", "without"]);
|
|
631
|
+
const WITH_ARGUMENT: ReadonlySet<string> = /* @__PURE__ */ new Set(["reduce", "take", "nth", "drop", "append", "without", "get"]);
|
|
632
632
|
|
|
633
633
|
/**
|
|
634
634
|
* This function gives the items of a list that have a value (LIST.LENIENT). A list op does not skip `#DEPTH!`, so that
|
|
@@ -699,6 +699,13 @@ function listOp(op: string, items: readonly ListItem[], rest: readonly unknown[]
|
|
|
699
699
|
}
|
|
700
700
|
case "flat":
|
|
701
701
|
return flat(vs, strict);
|
|
702
|
+
case "get": {
|
|
703
|
+
// The first item of the key gives its value. An error item is not an item of its key (LIST.GET).
|
|
704
|
+
const key = rest[0];
|
|
705
|
+
if (typeof key !== "string") return stop("kind-mismatch", `"get" needs a text key, not a ${describe(key)}`);
|
|
706
|
+
const i = good.findIndex((it) => it.key === key);
|
|
707
|
+
return i >= 0 ? vs[i] : stop("missing-field", `the list has no item with the key ${JSON.stringify(key)}`);
|
|
708
|
+
}
|
|
702
709
|
case "sum":
|
|
703
710
|
// Stryker disable next-line ArrayDeclaration: a sum needs no domain, and a text in the list accepts no value
|
|
704
711
|
return checked(nums().reduce((s, x) => s + x, 0), op, []);
|
package/src/list.ts
CHANGED
|
@@ -15,18 +15,50 @@ export interface VexList<T = unknown> {
|
|
|
15
15
|
/** This function makes a list. */
|
|
16
16
|
export const vexList = <T>(items: readonly ListItem<T>[]): VexList<T> => Object.freeze({ kind: "vex.list", items: Object.freeze([...items]) });
|
|
17
17
|
|
|
18
|
-
/**
|
|
19
|
-
|
|
20
|
-
|
|
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
|
+
/** This function tells if a value is an error value: a text `code`, `kind` and `message`, and a `path` (LIST.SHAPE). */
|
|
22
|
+
const isErrorValue = (u: unknown): boolean => {
|
|
23
|
+
if (typeof u !== "object" || u === null) return false;
|
|
24
|
+
const e = u as { code?: unknown; kind?: unknown; message?: unknown; path?: unknown };
|
|
25
|
+
return typeof e.code === "string" && typeof e.kind === "string" && typeof e.message === "string" && Array.isArray(e.path);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/** This function tells if a value is an item of a list: a text key, and a value or an error value (LIST.SHAPE). */
|
|
29
|
+
const isListItem = (u: unknown): boolean => {
|
|
30
|
+
if (typeof u !== "object" || u === null) return false;
|
|
31
|
+
const { key, result } = u as { key?: unknown; result?: unknown };
|
|
32
|
+
if (typeof key !== "string" || typeof result !== "object" || result === null) return false;
|
|
33
|
+
const r = result as { ok?: unknown; value?: unknown; error?: unknown };
|
|
34
|
+
return r.ok === true ? r.value !== undefined : r.ok === false && isErrorValue(r.error);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* This function tells if a value is a Vex list (LIST.SHAPE). A list has the kind `vex.list`, and each item has a text key
|
|
39
|
+
* and a result. Another value with the kind `vex.list` is a record, so no list op applies to it. A frozen
|
|
40
|
+
* list passes the check once.
|
|
41
|
+
*/
|
|
42
|
+
export const isVexList = (u: unknown): u is VexList => {
|
|
43
|
+
if (typeof u !== "object" || u === null) return false;
|
|
44
|
+
// Stryker disable next-line ConditionalExpression,BlockStatement: the cache holds only lists that pass the check again
|
|
45
|
+
if (CHECKED.has(u)) return true;
|
|
46
|
+
const { kind, items } = u as { kind?: unknown; items?: unknown };
|
|
47
|
+
if (kind !== "vex.list" || !Array.isArray(items)) return false;
|
|
48
|
+
// A loop over the positions, not `every`, so a hole of a sparse array is not an item.
|
|
49
|
+
for (let i = 0; i < items.length; i++) if (!isListItem(items[i])) return false;
|
|
50
|
+
if (Object.isFrozen(u) && Object.isFrozen(items)) CHECKED.add(u);
|
|
51
|
+
return true;
|
|
52
|
+
};
|
|
21
53
|
|
|
22
54
|
/**
|
|
23
55
|
* The name of a core list op. `sortBy`, `take`, `drop`, `append`, `flat` and `without` give a list (LIST.BUILD), and
|
|
24
|
-
* the other ops give a value.
|
|
56
|
+
* the other ops give a value. `get` gives the value at a key (LIST.GET).
|
|
25
57
|
*/
|
|
26
|
-
export type ListOp = "count" | "sum" | "min" | "max" | "mean" | "any" | "all" | "none" | "values" | "keys" | "reduce" | "first" | "errors" | "sortBy" | "take" | "nth" | "drop" | "append" | "flat" | "without";
|
|
58
|
+
export type ListOp = "count" | "sum" | "min" | "max" | "mean" | "any" | "all" | "none" | "values" | "keys" | "reduce" | "first" | "errors" | "sortBy" | "take" | "nth" | "drop" | "append" | "flat" | "without" | "get";
|
|
27
59
|
|
|
28
60
|
/** The names of the core list ops. They apply when the first argument is a list. */
|
|
29
|
-
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"]);
|
|
61
|
+
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"]);
|
|
30
62
|
|
|
31
63
|
/** This function tells if a name is the name of a core list op. */
|
|
32
64
|
export const isListOp = (op: string): op is ListOp => LIST_OPS.has(op);
|