quetch 0.17.0 → 0.18.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.
|
@@ -1,12 +1,33 @@
|
|
|
1
|
+
import { EMPTY_ARRAY } from "unchangeable";
|
|
1
2
|
const { entries } = Object;
|
|
2
3
|
export function filterFromContext(context) {
|
|
3
4
|
return {
|
|
4
5
|
operator: "all",
|
|
5
|
-
value:
|
|
6
|
-
field,
|
|
7
|
-
operator: "equal",
|
|
8
|
-
value,
|
|
9
|
-
})),
|
|
6
|
+
value: criteria(context),
|
|
10
7
|
};
|
|
11
8
|
}
|
|
9
|
+
function criteria(context, path = EMPTY_ARRAY) {
|
|
10
|
+
return entries(context).flatMap(([field, value]) => {
|
|
11
|
+
switch (typeof value) {
|
|
12
|
+
case "bigint":
|
|
13
|
+
case "string":
|
|
14
|
+
case "number":
|
|
15
|
+
case "boolean":
|
|
16
|
+
case "symbol":
|
|
17
|
+
case "undefined":
|
|
18
|
+
return [
|
|
19
|
+
{
|
|
20
|
+
field: path.length === 0 ? field : [...path, field],
|
|
21
|
+
operator: "equal",
|
|
22
|
+
value,
|
|
23
|
+
},
|
|
24
|
+
];
|
|
25
|
+
case "object":
|
|
26
|
+
return criteria(value, [...path, field]);
|
|
27
|
+
default:
|
|
28
|
+
return EMPTY_ARRAY;
|
|
29
|
+
// Ignore
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
12
33
|
//# sourceMappingURL=filterFromContext.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"filterFromContext.js","sourceRoot":"","sources":["../../lib/tools/filterFromContext.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"filterFromContext.js","sourceRoot":"","sources":["../../lib/tools/filterFromContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAI3C,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;AAE3B,MAAM,UAAU,iBAAiB,CAC/B,OAAmB;IAEnB,OAAO;QACL,QAAQ,EAAE,KAAK;QACf,KAAK,EAAE,QAAQ,CAAC,OAAO,CAAC;KACzB,CAAC;AACJ,CAAC;AAED,SAAS,QAAQ,CACf,OAAe,EACf,OAA0B,WAAW;IAErC,OAAO,OAAO,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,EAAE,KAAK,CAAC,EAAE,EAAE;QACjD,QAAQ,OAAO,KAAK,EAAE,CAAC;YACrB,KAAK,QAAQ,CAAC;YACd,KAAK,QAAQ,CAAC;YACd,KAAK,QAAQ,CAAC;YACd,KAAK,SAAS,CAAC;YACf,KAAK,QAAQ,CAAC;YACd,KAAK,WAAW;gBACd,OAAO;oBACL;wBACE,KAAK,EAAE,IAAI,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,EAAE,KAAK,CAAC;wBACnD,QAAQ,EAAE,OAAO;wBACjB,KAAK;qBACO;iBACf,CAAC;YACJ,KAAK,QAAQ;gBACX,OAAO,QAAQ,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;YAC3C;gBACE,OAAO,WAAW,CAAC;YACrB,SAAS;QACX,CAAC;IACH,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { filterFromContext } from "./filterFromContext.js";
|
|
4
|
+
|
|
5
|
+
test("returns filter from context", () => {
|
|
6
|
+
expect(filterFromContext({ a: 1 })).toEqual({
|
|
7
|
+
operator: "all",
|
|
8
|
+
value: [
|
|
9
|
+
{
|
|
10
|
+
field: "a",
|
|
11
|
+
operator: "equal",
|
|
12
|
+
value: 1,
|
|
13
|
+
},
|
|
14
|
+
],
|
|
15
|
+
});
|
|
16
|
+
expect(filterFromContext({ a: { b: 1 } })).toEqual({
|
|
17
|
+
operator: "all",
|
|
18
|
+
value: [
|
|
19
|
+
{
|
|
20
|
+
field: ["a", "b"],
|
|
21
|
+
operator: "equal",
|
|
22
|
+
value: 1,
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
});
|
|
26
|
+
expect(filterFromContext({ a: { b: 1 }, c: 2 })).toEqual({
|
|
27
|
+
operator: "all",
|
|
28
|
+
value: [
|
|
29
|
+
{
|
|
30
|
+
field: ["a", "b"],
|
|
31
|
+
operator: "equal",
|
|
32
|
+
value: 1,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
field: "c",
|
|
36
|
+
operator: "equal",
|
|
37
|
+
value: 2,
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { EMPTY_ARRAY } from "unchangeable";
|
|
2
|
+
|
|
1
3
|
import type { Context, Filter } from "../types";
|
|
2
4
|
|
|
3
5
|
const { entries } = Object;
|
|
@@ -7,10 +9,34 @@ export function filterFromContext<T extends object>(
|
|
|
7
9
|
): Filter<T> {
|
|
8
10
|
return {
|
|
9
11
|
operator: "all",
|
|
10
|
-
value:
|
|
11
|
-
field,
|
|
12
|
-
operator: "equal",
|
|
13
|
-
value,
|
|
14
|
-
})) as Filter<T>[],
|
|
12
|
+
value: criteria(context),
|
|
15
13
|
};
|
|
16
14
|
}
|
|
15
|
+
|
|
16
|
+
function criteria<T extends object>(
|
|
17
|
+
context: object,
|
|
18
|
+
path: readonly string[] = EMPTY_ARRAY,
|
|
19
|
+
): readonly Filter<T>[] {
|
|
20
|
+
return entries(context).flatMap(([field, value]) => {
|
|
21
|
+
switch (typeof value) {
|
|
22
|
+
case "bigint":
|
|
23
|
+
case "string":
|
|
24
|
+
case "number":
|
|
25
|
+
case "boolean":
|
|
26
|
+
case "symbol":
|
|
27
|
+
case "undefined":
|
|
28
|
+
return [
|
|
29
|
+
{
|
|
30
|
+
field: path.length === 0 ? field : [...path, field],
|
|
31
|
+
operator: "equal",
|
|
32
|
+
value,
|
|
33
|
+
} as Filter<T>,
|
|
34
|
+
];
|
|
35
|
+
case "object":
|
|
36
|
+
return criteria(value, [...path, field]);
|
|
37
|
+
default:
|
|
38
|
+
return EMPTY_ARRAY;
|
|
39
|
+
// Ignore
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
@@ -3,6 +3,7 @@ import { expect, test } from "vitest";
|
|
|
3
3
|
import { SymbolCache } from "../constants.js";
|
|
4
4
|
import type { FilterChildren } from "../types.js";
|
|
5
5
|
|
|
6
|
+
import { filterFromContext } from "./filterFromContext.js";
|
|
6
7
|
import { testFilter } from "./testFilter.js";
|
|
7
8
|
|
|
8
9
|
test("tests filter lists", () => {
|
|
@@ -266,6 +267,15 @@ test("tests filter on array values", () => {
|
|
|
266
267
|
).toBe(false);
|
|
267
268
|
});
|
|
268
269
|
|
|
270
|
+
test("tests filter with paths", () => {
|
|
271
|
+
const context = { value: { filter: { field: "label" } } };
|
|
272
|
+
expect(
|
|
273
|
+
testFilter(filterFromContext(context), {
|
|
274
|
+
value: { filter: { field: "label" } },
|
|
275
|
+
}),
|
|
276
|
+
).toBe(true);
|
|
277
|
+
});
|
|
278
|
+
|
|
269
279
|
test("tests filter with children predicates", () => {
|
|
270
280
|
expect(testFilter({ operator: "children", value: "a" }, { id: "a/b" })).toBe(
|
|
271
281
|
true,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "quetch",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/main.js",
|
|
6
6
|
"exports": {
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"format:fix": "prettier --write './**/*.{css,scss,md,mdx,json,tsx,ts,jsx,js}'",
|
|
39
39
|
"lint": "npm run lint:ts",
|
|
40
40
|
"lint:fix": "npm run lint:ts:fix",
|
|
41
|
-
"lint:ts": "eslint .",
|
|
41
|
+
"lint:ts": "eslint . && tsc --project tsconfig.package.json --noEmit",
|
|
42
42
|
"lint:ts:fix": "eslint --fix .",
|
|
43
43
|
"release:init": "npm publish --access=public",
|
|
44
44
|
"release:patch": "npm version patch && git push origin --follow-tags && npm publish",
|
|
@@ -79,6 +79,6 @@
|
|
|
79
79
|
},
|
|
80
80
|
"dependencies": {
|
|
81
81
|
"futurise": "^1.7.1",
|
|
82
|
-
"unchangeable": "^1.7.
|
|
82
|
+
"unchangeable": "^1.7.3"
|
|
83
83
|
}
|
|
84
84
|
}
|