@mark1russell7/vex 0.13.3 → 0.13.4
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 +0 -34
- package/build/index.d.ts.map +1 -1
- package/build/index.js +74 -51
- package/build/index.js.map +1 -1
- package/package.json +2 -2
- package/src/evaluate.ts +39 -10
- package/src/kernel.ts +18 -3
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.13.
|
|
4
|
+
"version": "0.13.4",
|
|
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.
|
|
52
|
+
"@mark1russell7/coeval": "^0.8.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
55
|
"@mark1russell7/cue": "github:mark1russell7/cue",
|
package/src/evaluate.ts
CHANGED
|
@@ -777,7 +777,7 @@ class Compiler {
|
|
|
777
777
|
};
|
|
778
778
|
switch (op) {
|
|
779
779
|
case "sortBy":
|
|
780
|
-
return this.#sortBy(okItems, okValues, f, path);
|
|
780
|
+
return this.#sortBy(okItems, okValues, optsArg, f, path);
|
|
781
781
|
case "take":
|
|
782
782
|
case "drop": {
|
|
783
783
|
const n = whole();
|
|
@@ -882,16 +882,24 @@ class Compiler {
|
|
|
882
882
|
}
|
|
883
883
|
|
|
884
884
|
/**
|
|
885
|
-
* This method sorts the items of a list by their values, from the smallest (LIST.SORT). The sort is stable. The
|
|
886
|
-
* are all numbers without NaN, or all texts. Texts compare by their UTF-16 code units.
|
|
885
|
+
* This method sorts the items of a list by their sort values, from the smallest (LIST.SORT). The sort is stable. The
|
|
886
|
+
* sort values are all numbers without NaN, or all texts. Texts compare by their UTF-16 code units. Without the option
|
|
887
|
+
* `by`, the sort value of an item is its value (LIST.SORT.BY).
|
|
887
888
|
*/
|
|
888
|
-
#sortBy(items: readonly ListItem[], values: readonly unknown[], f: Frame, path: readonly number[]): Result<VexList> {
|
|
889
|
-
const
|
|
890
|
-
const
|
|
891
|
-
if (
|
|
892
|
-
const
|
|
893
|
-
|
|
894
|
-
|
|
889
|
+
#sortBy(items: readonly ListItem[], values: readonly unknown[], opts: unknown, f: Frame, path: readonly number[]): Result<VexList> {
|
|
890
|
+
const by = (opts as { readonly by?: unknown } | null | undefined)?.by;
|
|
891
|
+
const sorted = by === undefined ? ok(values) : this.#sortValues(by, values, f, path);
|
|
892
|
+
if (!sorted.ok) return sorted;
|
|
893
|
+
const keys = sorted.value;
|
|
894
|
+
const kind = typeof keys[0];
|
|
895
|
+
const bad = keys.findIndex((x) => typeof x !== kind || (kind !== "number" && kind !== "string") || Number.isNaN(x));
|
|
896
|
+
if (bad >= 0) {
|
|
897
|
+
const what = typeof by === "string" ? `"${by}" of an item` : "an item";
|
|
898
|
+
return this.#err("kind-mismatch", `"sortBy" needs numbers without NaN or texts, all of one kind, but ${what} is ${Number.isNaN(keys[bad]) ? "NaN" : `a ${describeType(keys[bad])}`}`, f, path, { op: "sortBy" });
|
|
899
|
+
}
|
|
900
|
+
const order = keys.map((_x, i) => i).toSorted((i, j) => {
|
|
901
|
+
const a = keys[i] as number | string;
|
|
902
|
+
const b = keys[j] as number | string;
|
|
895
903
|
// The sort of Node reads only a negative result, and the sort of another engine needs the whole comparison.
|
|
896
904
|
// Stryker disable next-line ConditionalExpression,EqualityOperator: see the comment above
|
|
897
905
|
return a < b ? -1 : a > b ? 1 : 0;
|
|
@@ -899,6 +907,27 @@ class Compiler {
|
|
|
899
907
|
return ok(vexList(order.map((i) => items[i] as ListItem)));
|
|
900
908
|
}
|
|
901
909
|
|
|
910
|
+
/**
|
|
911
|
+
* This method gives the sort value of each item for the option `by` of `sortBy` (LIST.SORT.BY). The op of the name
|
|
912
|
+
* `by` applies to the value of each item, in the order of the list. The first error is the result.
|
|
913
|
+
*/
|
|
914
|
+
#sortValues(by: unknown, values: readonly unknown[], f: Frame, path: readonly number[]): Result<readonly unknown[]> {
|
|
915
|
+
if (typeof by !== "string") return this.#err("kind-mismatch", `the option "by" of "sortBy" needs an op name, but got a ${describeType(by)}`, f, path, { op: "sortBy" });
|
|
916
|
+
const out: unknown[] = [];
|
|
917
|
+
for (const x of values) {
|
|
918
|
+
let op: ResolvedOp | undefined;
|
|
919
|
+
for (const d of this.#domains) {
|
|
920
|
+
op = safeIs(d, x) ? resolveOp(d, x, by) : undefined;
|
|
921
|
+
if (op !== undefined) break;
|
|
922
|
+
}
|
|
923
|
+
if (op === undefined) return this.#err("unknown-op", `no domain gives "${by}" for a ${describeType(x)}`, f, path, { op: by });
|
|
924
|
+
const r = this.#call(op, by, [], f, path);
|
|
925
|
+
if (!r.ok) return r;
|
|
926
|
+
out.push(r.value);
|
|
927
|
+
}
|
|
928
|
+
return ok(out);
|
|
929
|
+
}
|
|
930
|
+
|
|
902
931
|
/**
|
|
903
932
|
* This method folds the values with a domain op, from the left. An empty list gives the identity of the op, if one
|
|
904
933
|
* domain has an identity for the op (LIST.EMPTY). The option `domain` names that domain when more domains have one.
|
package/src/kernel.ts
CHANGED
|
@@ -686,9 +686,10 @@ function listOp(op: string, items: readonly ListItem[], rest: readonly unknown[]
|
|
|
686
686
|
case "first":
|
|
687
687
|
return vs.length > 0 ? vs[0] : empty();
|
|
688
688
|
case "sortBy": {
|
|
689
|
-
const
|
|
690
|
-
|
|
691
|
-
|
|
689
|
+
const ks = sortValues(o, vs, domains);
|
|
690
|
+
const kind = typeof ks[0];
|
|
691
|
+
if (ks.some((x) => typeof x !== kind || (kind !== "number" && kind !== "string") || Number.isNaN(x))) stop("kind-mismatch", '"sortBy" needs numbers without NaN or texts, all of one kind');
|
|
692
|
+
return vexList(good.map((it, i) => ({ it, i })).toSorted((a, b) => compare(ks[a.i], ks[b.i])).map(({ it }) => it));
|
|
692
693
|
}
|
|
693
694
|
case "take":
|
|
694
695
|
case "drop":
|
|
@@ -745,6 +746,20 @@ function listOp(op: string, items: readonly ListItem[], rest: readonly unknown[]
|
|
|
745
746
|
}
|
|
746
747
|
}
|
|
747
748
|
|
|
749
|
+
/**
|
|
750
|
+
* This function gives the sort values of `sortBy` (LIST.SORT.BY). Without the option `by`, they are the values. With
|
|
751
|
+
* it, the op of that name applies to each value, in order, and the first error stops the sort.
|
|
752
|
+
*/
|
|
753
|
+
function sortValues(o: unknown, vs: readonly unknown[], domains: readonly AnyDomain[]): readonly unknown[] {
|
|
754
|
+
const by = (o as { readonly by?: unknown } | null | undefined)?.by;
|
|
755
|
+
if (by === undefined) return vs;
|
|
756
|
+
if (typeof by !== "string") return stop("kind-mismatch", '"sortBy" needs an op name in the option "by"');
|
|
757
|
+
return vs.map((x) => {
|
|
758
|
+
const found = domains.filter((d) => accepts(d, x)).map((d) => opOf(d, x, by)).find((f) => f !== undefined);
|
|
759
|
+
return found === undefined ? stop("unknown-op", `no domain gives "${by}" for a ${describe(x)}`) : callFound(found, by, x, [], domains);
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
|
|
748
763
|
/** This function compares two numbers or two texts for `sortBy`. Equal values keep their order (LIST.SORT). */
|
|
749
764
|
const compare = (a: unknown, b: unknown): number => ((a as number) < (b as number) ? -1 : (a as number) > (b as number) ? 1 : 0);
|
|
750
765
|
|