@mark1russell7/vex 0.19.0 → 0.20.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 +3 -2
- package/build/index.d.ts.map +1 -1
- package/build/index.js +47 -4
- package/build/index.js.map +1 -1
- package/package.json +3 -2
- package/src/evaluate.ts +25 -1
- package/src/kernel.ts +19 -1
- package/src/list.ts +5 -3
- package/src/size.ts +1 -0
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.20.0",
|
|
5
5
|
"description": "Vex: typed spreadsheet formulas over domain objects. The kernel and the typed builder.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"typescript",
|
|
@@ -54,7 +54,8 @@
|
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@mark1russell7/cue": "github:mark1russell7/cue#9c99ad65d51cefcba5a8b6dc89358d7a94758ca7",
|
|
56
56
|
"@mark1russell7/optional": "github:mark1russell7/optional#1c649e1ea49991d1574a74f0c402220502e219a7",
|
|
57
|
-
"@mark1russell7/rigor-bench": "^0.2.
|
|
57
|
+
"@mark1russell7/rigor-bench": "^0.2.59",
|
|
58
|
+
"@mark1russell7/rigor-statistics": "^0.2.11",
|
|
58
59
|
"@types/node": "^26.6.4",
|
|
59
60
|
"typescript": "^7.0.2",
|
|
60
61
|
"vitest": "^5.0.3"
|
package/src/evaluate.ts
CHANGED
|
@@ -1127,6 +1127,8 @@ class Compiler {
|
|
|
1127
1127
|
const i = okValues.indexOf(x);
|
|
1128
1128
|
return i >= 0 ? ok(i) : this.#err("missing-field", `the list has no item with the value ${JSON.stringify(x)}`, f, path, { op });
|
|
1129
1129
|
}
|
|
1130
|
+
case "includes":
|
|
1131
|
+
return this.#includes(rest[0], okValues, f, path);
|
|
1130
1132
|
case "nth": {
|
|
1131
1133
|
const i = whole();
|
|
1132
1134
|
if (!i.ok) return i;
|
|
@@ -1293,6 +1295,28 @@ class Compiler {
|
|
|
1293
1295
|
return ok(out);
|
|
1294
1296
|
}
|
|
1295
1297
|
|
|
1298
|
+
/**
|
|
1299
|
+
* This method tells if an item of a list equals a value (LIST.INCLUDES). The op `eq` of the first domain that accepts
|
|
1300
|
+
* the value compares the value with each item, in the order of the list. The first `true` stops the walk, and the first
|
|
1301
|
+
* error is the result. A value without such an op gives `#NAME?`, also for an empty list.
|
|
1302
|
+
*/
|
|
1303
|
+
#includes(x: unknown, values: readonly unknown[], f: Frame, path: readonly number[]): Result<boolean> {
|
|
1304
|
+
if (x === undefined) return this.#at(errorText.needsArgument("includes", 1), f, path);
|
|
1305
|
+
let op: ResolvedOp | undefined;
|
|
1306
|
+
for (const d of this.#domains) {
|
|
1307
|
+
op = safeIs(d, x) ? resolveOp(d, x, "eq") : undefined;
|
|
1308
|
+
if (op !== undefined) break;
|
|
1309
|
+
}
|
|
1310
|
+
if (op === undefined) return this.#at(errorText.noStep("eq", x), f, path);
|
|
1311
|
+
for (const item of values) {
|
|
1312
|
+
const r = this.#callResolved(op, x, "eq", [item], f, path);
|
|
1313
|
+
if (!r.ok) return r;
|
|
1314
|
+
if (typeof r.value !== "boolean") return this.#err("kind-mismatch", `"includes" needs a boolean from "eq", but "eq" gave a ${describeType(r.value)}`, f, path, { op: "includes" });
|
|
1315
|
+
if (r.value) return ok(true);
|
|
1316
|
+
}
|
|
1317
|
+
return ok(false);
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1296
1320
|
/**
|
|
1297
1321
|
* This method folds the values with a domain op, from the left. An empty list gives the identity of the op, if one
|
|
1298
1322
|
* domain has an identity for the op (LIST.EMPTY). The option `domain` names that domain when more domains have one.
|
|
@@ -1327,7 +1351,7 @@ class Compiler {
|
|
|
1327
1351
|
}
|
|
1328
1352
|
|
|
1329
1353
|
/** The list ops with a second argument before their options: a count, a position, an op name or a list. */
|
|
1330
|
-
const WITH_ARGUMENT: ReadonlySet<ListOp> = new Set<ListOp>(["reduce", "take", "nth", "drop", "append", "without", "get", "indexOf", "group", "join"]);
|
|
1354
|
+
const WITH_ARGUMENT: ReadonlySet<ListOp> = new Set<ListOp>(["reduce", "take", "nth", "drop", "append", "without", "get", "indexOf", "group", "join", "includes"]);
|
|
1331
1355
|
|
|
1332
1356
|
/**
|
|
1333
1357
|
* This function gives the items of a list that have a value (LIST.LENIENT). A list op does not skip `#DEPTH!`, so that
|
package/src/kernel.ts
CHANGED
|
@@ -779,7 +779,7 @@ function callFound(found: Found, op: string, self: unknown, args: readonly unkno
|
|
|
779
779
|
}
|
|
780
780
|
|
|
781
781
|
/** The list ops with a second argument before their options: a count, a position, an op name or a list. */
|
|
782
|
-
const WITH_ARGUMENT: ReadonlySet<string> = /* @__PURE__ */ new Set(["reduce", "take", "nth", "drop", "append", "without", "get", "indexOf", "group", "join"]);
|
|
782
|
+
const WITH_ARGUMENT: ReadonlySet<string> = /* @__PURE__ */ new Set(["reduce", "take", "nth", "drop", "append", "without", "get", "indexOf", "group", "join", "includes"]);
|
|
783
783
|
|
|
784
784
|
/**
|
|
785
785
|
* This function gives the items of a list that have a value (LIST.LENIENT). A list op does not skip `#DEPTH!`, so that
|
|
@@ -910,6 +910,8 @@ function listOp(op: string, items: readonly ListItem[], rest: readonly unknown[]
|
|
|
910
910
|
const i = vs.indexOf(x);
|
|
911
911
|
return i >= 0 ? i : stop("missing-field", `the list has no item with the value ${JSON.stringify(x)}`);
|
|
912
912
|
}
|
|
913
|
+
case "includes":
|
|
914
|
+
return includes(rest[0], vs, domains);
|
|
913
915
|
case "sum":
|
|
914
916
|
// Stryker disable next-line ArrayDeclaration: a sum needs no domain, and a text in the list accepts no value
|
|
915
917
|
return checked(nums().reduce((s, x) => s + x, 0), op, []);
|
|
@@ -948,6 +950,22 @@ function sortValues(o: unknown, vs: readonly unknown[], domains: readonly AnyDom
|
|
|
948
950
|
});
|
|
949
951
|
}
|
|
950
952
|
|
|
953
|
+
/**
|
|
954
|
+
* This function tells if an item equals the value `x` by the op `eq` of the first domain that accepts `x`
|
|
955
|
+
* (LIST.INCLUDES). The walk goes in the order of the list, and it stops at the first `true` or the first error.
|
|
956
|
+
*/
|
|
957
|
+
function includes(x: unknown, vs: readonly unknown[], domains: readonly AnyDomain[]): boolean {
|
|
958
|
+
if (x === undefined) return stop("bad-expression", '"includes" needs argument 2');
|
|
959
|
+
const found = domains.filter((d) => accepts(d, x)).map((d) => opOf(d, x, "eq")).find((f) => f !== undefined);
|
|
960
|
+
if (found === undefined) return stop("unknown-op", `no domain gives "eq" for a ${describe(x)}`);
|
|
961
|
+
for (const item of vs) {
|
|
962
|
+
const same = callFound(found, "eq", x, [item], domains);
|
|
963
|
+
if (typeof same !== "boolean") return stop("kind-mismatch", '"includes" needs a boolean from "eq"');
|
|
964
|
+
if (same) return true;
|
|
965
|
+
}
|
|
966
|
+
return false;
|
|
967
|
+
}
|
|
968
|
+
|
|
951
969
|
/** This function compares two numbers or two texts for `sortBy`. Equal values keep their order (LIST.SORT). */
|
|
952
970
|
const compare = (a: unknown, b: unknown): number => ((a as number) < (b as number) ? -1 : (a as number) > (b as number) ? 1 : 0);
|
|
953
971
|
|
package/src/list.ts
CHANGED
|
@@ -54,15 +54,16 @@ export const isVexList = (u: unknown): u is VexList => {
|
|
|
54
54
|
* The name of a core list op. `sortBy`, `take`, `drop`, `append`, `flat` and `without` give a list (LIST.BUILD), and
|
|
55
55
|
* the other ops give a value. `get` gives the value at a key (LIST.GET), and `indexOf` gives the position of a value
|
|
56
56
|
* (LIST.INDEXOF). `distinct` and `group` give a list, and `join` gives a text (LIST.DISTINCT, LIST.GROUP, LIST.JOIN).
|
|
57
|
-
* `setNth` gives a list with a new value at one position (LIST.SETNTH).
|
|
57
|
+
* `setNth` gives a list with a new value at one position (LIST.SETNTH). `includes` tells if an item equals a value by
|
|
58
|
+
* the op `eq` of a domain (LIST.INCLUDES).
|
|
58
59
|
*/
|
|
59
|
-
export type ListOp = "count" | "sum" | "min" | "max" | "mean" | "any" | "all" | "none" | "values" | "keys" | "reduce" | "first" | "errors" | "sortBy" | "take" | "nth" | "setNth" | "drop" | "append" | "flat" | "without" | "get" | "indexOf" | "distinct" | "group" | "join";
|
|
60
|
+
export type ListOp = "count" | "sum" | "min" | "max" | "mean" | "any" | "all" | "none" | "values" | "keys" | "reduce" | "first" | "errors" | "sortBy" | "take" | "nth" | "setNth" | "drop" | "append" | "flat" | "without" | "get" | "indexOf" | "distinct" | "group" | "join" | "includes";
|
|
60
61
|
|
|
61
62
|
/**
|
|
62
63
|
* 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
64
|
* domain and no function of the host has the op for it (LIST.ARRAY).
|
|
64
65
|
*/
|
|
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", "setNth", "drop", "append", "flat", "without", "get", "indexOf", "distinct", "group", "join"]);
|
|
66
|
+
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", "setNth", "drop", "append", "flat", "without", "get", "indexOf", "distinct", "group", "join", "includes"]);
|
|
66
67
|
|
|
67
68
|
/**
|
|
68
69
|
* The size rules of the core list ops and of `fill` (LOOP.SIZE.OPS). Each op that gives a list has a rule here. An
|
|
@@ -78,6 +79,7 @@ export const LIST_SIZES: Readonly<Record<ListOp | "fill", SizeRule>> = Object.fr
|
|
|
78
79
|
all: "scalar",
|
|
79
80
|
none: "scalar",
|
|
80
81
|
indexOf: "scalar",
|
|
82
|
+
includes: "scalar",
|
|
81
83
|
values: { size: [1], count: [1], inner: [1], item: [1], part: [1], keys: "position" },
|
|
82
84
|
keys: { size: [1], count: [1], keys: "position" },
|
|
83
85
|
errors: { size: ["unbounded"] },
|