quetch 0.17.0 → 0.18.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.
@@ -1,12 +1,48 @@
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: entries(context).map(([field, 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
+ }
33
+ /*
34
+
35
+ {
36
+ filter: {
37
+ field: "a"
38
+ }
39
+ }
40
+
41
+ {
42
+ operator: "equal",
43
+ field: ["filter", "field"],
44
+ value: "a"
45
+ }
46
+
47
+ */
12
48
  //# sourceMappingURL=filterFromContext.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"filterFromContext.js","sourceRoot":"","sources":["../../lib/tools/filterFromContext.ts"],"names":[],"mappings":"AAEA,MAAM,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;AAE3B,MAAM,UAAU,iBAAiB,CAC/B,OAAmB;IAEnB,OAAO;QACL,QAAQ,EAAE,KAAK;QACf,KAAK,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;YAC/C,KAAK;YACL,QAAQ,EAAE,OAAO;YACjB,KAAK;SACN,CAAC,CAAgB;KACnB,CAAC;AACJ,CAAC"}
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,CAAgB;KACxC,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;qBACN;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;AAED;;;;;;;;;;;;;;EAcE"}
@@ -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,50 @@ export function filterFromContext<T extends object>(
7
9
  ): Filter<T> {
8
10
  return {
9
11
  operator: "all",
10
- value: entries(context).map(([field, value]) => ({
11
- field,
12
- operator: "equal",
13
- value,
14
- })) as Filter<T>[],
12
+ value: criteria(context) as Filter<T>[],
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
+ },
34
+ ];
35
+ case "object":
36
+ return criteria(value, [...path, field]);
37
+ default:
38
+ return EMPTY_ARRAY;
39
+ // Ignore
40
+ }
41
+ });
42
+ }
43
+
44
+ /*
45
+
46
+ {
47
+ filter: {
48
+ field: "a"
49
+ }
50
+ }
51
+
52
+ {
53
+ operator: "equal",
54
+ field: ["filter", "field"],
55
+ value: "a"
56
+ }
57
+
58
+ */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quetch",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "type": "module",
5
5
  "main": "./dist/main.js",
6
6
  "exports": {