quetch 0.18.2 → 0.19.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.
Files changed (159) hide show
  1. package/dist/constants/SymbolSelf.d.ts +1 -0
  2. package/dist/constants/SymbolSelf.js +2 -0
  3. package/dist/constants/SymbolSelf.js.map +1 -0
  4. package/dist/constants.d.ts +1 -0
  5. package/dist/constants.js +1 -0
  6. package/dist/constants.js.map +1 -1
  7. package/dist/middlewares/fetchLocal.d.ts +1 -1
  8. package/dist/tools/defineCheckQuery.d.ts +1 -1
  9. package/dist/tools/defineCustomFetch.d.ts +1 -1
  10. package/dist/tools/defineGenericFetch.d.ts +1 -1
  11. package/dist/tools/fieldListFromFilter.d.ts +1 -1
  12. package/dist/tools/filterChildren.d.ts +1 -1
  13. package/dist/tools/filterFromContext.d.ts +1 -1
  14. package/dist/tools/filterFromContext.js +11 -1
  15. package/dist/tools/filterFromContext.js.map +1 -1
  16. package/dist/tools/get.d.ts +2 -2
  17. package/dist/tools/get.js +2 -0
  18. package/dist/tools/get.js.map +1 -1
  19. package/dist/tools/isFilterGroup.d.ts +1 -1
  20. package/dist/tools/normalizeOrder.d.ts +1 -1
  21. package/dist/tools/normalizeOrder.js.map +1 -1
  22. package/dist/tools/queryItemList.d.ts +1 -1
  23. package/dist/tools/reverseOrder.d.ts +1 -1
  24. package/dist/tools/reverseOrder.js.map +1 -1
  25. package/dist/tools/sortItemList.d.ts +1 -1
  26. package/dist/tools/sortItemList.js.map +1 -1
  27. package/dist/tools/testFilter.d.ts +1 -1
  28. package/dist/tools/testFilter.js.map +1 -1
  29. package/dist/types/AggregateFunction.d.ts +3 -2
  30. package/dist/types/CombineUnion.d.ts +9 -0
  31. package/dist/types/CombineUnion.js +1 -0
  32. package/dist/types/CombineUnion.js.map +1 -0
  33. package/dist/types/Context.d.ts +1 -1
  34. package/dist/types/CustomFetch.d.ts +1 -1
  35. package/dist/types/CustomFieldAggregateMap.d.ts +1 -1
  36. package/dist/types/CustomFieldMap.d.ts +1 -1
  37. package/dist/types/Field.d.ts +4 -2
  38. package/dist/types/FieldFiltered.d.ts +4 -2
  39. package/dist/types/FieldFunction.d.ts +1 -1
  40. package/dist/types/FieldFunctionCustom.d.ts +1 -1
  41. package/dist/types/FieldFunctionFormatDate.d.ts +1 -1
  42. package/dist/types/Filter.d.ts +1 -1
  43. package/dist/types/FilterArray.d.ts +1 -2
  44. package/dist/types/FilterBoolean.d.ts +1 -1
  45. package/dist/types/FilterChildren.d.ts +1 -1
  46. package/dist/types/FilterCustom.d.ts +1 -1
  47. package/dist/types/FilterField.d.ts +1 -1
  48. package/dist/types/FilterGroup.d.ts +1 -1
  49. package/dist/types/FilterNumber.d.ts +1 -1
  50. package/dist/types/FilterString.d.ts +1 -1
  51. package/dist/types/FilterStringIntersect.d.ts +1 -1
  52. package/dist/types/FilterStringMatch.d.ts +1 -1
  53. package/dist/types/Get.d.ts +1 -1
  54. package/dist/types/Group.d.ts +1 -1
  55. package/dist/types/Immutable.d.ts +1 -1
  56. package/dist/types/InjectCustomFields.d.ts +1 -1
  57. package/dist/types/KeyFiltered.d.ts +10 -4
  58. package/dist/types/KeyFromUnion.d.ts +1 -0
  59. package/dist/types/KeyFromUnion.js +1 -0
  60. package/dist/types/KeyFromUnion.js.map +1 -0
  61. package/dist/types/Order.d.ts +1 -1
  62. package/dist/types/Path.d.ts +4 -2
  63. package/dist/types/PathFiltered.d.ts +3 -2
  64. package/dist/types/Primitive.d.ts +1 -0
  65. package/dist/types/Primitive.js +1 -0
  66. package/dist/types/Primitive.js.map +1 -0
  67. package/dist/types/Query.d.ts +2 -2
  68. package/dist/types/QueryAggregate.d.ts +1 -1
  69. package/dist/types/QueryCreate.d.ts +1 -1
  70. package/dist/types/QueryCreateMultiple.d.ts +1 -1
  71. package/dist/types/QueryDelete.d.ts +1 -1
  72. package/dist/types/QueryDeleteMultiple.d.ts +1 -1
  73. package/dist/types/QueryRead.d.ts +1 -1
  74. package/dist/types/QueryReadMultiple.d.ts +1 -1
  75. package/dist/types/QuerySettings.d.ts +1 -1
  76. package/dist/types/QueryUpdate.d.ts +1 -1
  77. package/dist/types/QueryUpdateMultiple.d.ts +1 -1
  78. package/dist/types/Result.d.ts +7 -7
  79. package/dist/types/Value.d.ts +1 -1
  80. package/dist/types.d.ts +3 -3
  81. package/lib/constants/SymbolSelf.ts +1 -0
  82. package/lib/constants.ts +1 -0
  83. package/lib/middlewares/fetchLocal.ts +1 -1
  84. package/lib/tools/defineCheckQuery.ts +1 -1
  85. package/lib/tools/defineCustomFetch.ts +1 -1
  86. package/lib/tools/defineGenericFetch.ts +1 -1
  87. package/lib/tools/fieldListFromFilter.ts +1 -1
  88. package/lib/tools/filterChildren.ts +1 -1
  89. package/lib/tools/filterFromContext.test.ts +30 -0
  90. package/lib/tools/filterFromContext.ts +13 -5
  91. package/lib/tools/get.test.ts +1 -0
  92. package/lib/tools/get.ts +14 -9
  93. package/lib/tools/isFilterGroup.ts +1 -1
  94. package/lib/tools/normalizeOrder.ts +1 -3
  95. package/lib/tools/queryItemList.test.ts +2 -2
  96. package/lib/tools/queryItemList.ts +3 -3
  97. package/lib/tools/reverseOrder.ts +1 -3
  98. package/lib/tools/sortItemList.ts +3 -3
  99. package/lib/tools/testFilter.test.ts +1 -1
  100. package/lib/tools/testFilter.ts +6 -9
  101. package/lib/types/AggregateFunction.ts +3 -2
  102. package/lib/types/CombineUnion.ts +18 -0
  103. package/lib/types/Context.ts +1 -1
  104. package/lib/types/CustomFetch.ts +1 -1
  105. package/lib/types/CustomFieldAggregateMap.ts +1 -4
  106. package/lib/types/CustomFieldMap.ts +1 -1
  107. package/lib/types/Field.ts +4 -4
  108. package/lib/types/FieldFiltered.ts +4 -4
  109. package/lib/types/FieldFunction.ts +1 -1
  110. package/lib/types/FieldFunctionCustom.ts +1 -1
  111. package/lib/types/FieldFunctionFormatDate.ts +1 -1
  112. package/lib/types/Filter.ts +1 -1
  113. package/lib/types/FilterArray.ts +1 -2
  114. package/lib/types/FilterBoolean.ts +1 -1
  115. package/lib/types/FilterChildren.ts +1 -1
  116. package/lib/types/FilterCustom.ts +1 -1
  117. package/lib/types/FilterField.ts +1 -1
  118. package/lib/types/FilterGroup.ts +1 -1
  119. package/lib/types/FilterNumber.ts +1 -1
  120. package/lib/types/FilterString.ts +1 -1
  121. package/lib/types/FilterStringIntersect.ts +1 -1
  122. package/lib/types/FilterStringMatch.ts +1 -1
  123. package/lib/types/Get.ts +1 -1
  124. package/lib/types/Group.ts +1 -1
  125. package/lib/types/Immutable.ts +1 -1
  126. package/lib/types/InjectCustomFields.ts +1 -4
  127. package/lib/types/KeyFiltered.ts +29 -5
  128. package/lib/types/KeyFromUnion.ts +1 -0
  129. package/lib/types/Order.ts +1 -1
  130. package/lib/types/Path.ts +15 -9
  131. package/lib/types/PathFiltered.ts +16 -13
  132. package/lib/types/Primitive.ts +1 -0
  133. package/lib/types/Query.ts +2 -2
  134. package/lib/types/QueryAggregate.ts +1 -1
  135. package/lib/types/QueryCreate.ts +1 -1
  136. package/lib/types/QueryCreateMultiple.ts +1 -1
  137. package/lib/types/QueryDelete.ts +1 -1
  138. package/lib/types/QueryDeleteMultiple.ts +1 -1
  139. package/lib/types/QueryRead.ts +1 -1
  140. package/lib/types/QueryReadMultiple.ts +1 -1
  141. package/lib/types/QuerySettings.ts +1 -1
  142. package/lib/types/QueryUpdate.ts +1 -1
  143. package/lib/types/QueryUpdateMultiple.ts +1 -1
  144. package/lib/types/Result.ts +5 -8
  145. package/lib/types/Value.ts +1 -1
  146. package/lib/types.ts +3 -3
  147. package/package.json +6 -5
  148. package/dist/types/Any.d.ts +0 -1
  149. package/dist/types/Any.js +0 -1
  150. package/dist/types/Any.js.map +0 -1
  151. package/dist/types/Mutable.d.ts +0 -3
  152. package/dist/types/Mutable.js +0 -1
  153. package/dist/types/Mutable.js.map +0 -1
  154. package/dist/types/Normalized.d.ts +0 -2
  155. package/dist/types/Normalized.js +0 -1
  156. package/dist/types/Normalized.js.map +0 -1
  157. package/lib/types/Any.ts +0 -10
  158. package/lib/types/Mutable.ts +0 -3
  159. package/lib/types/Normalized.ts +0 -3
@@ -7,7 +7,7 @@ import type { QuerySettings } from "./QuerySettings";
7
7
  /**
8
8
  * Query for computing an aggregated value.
9
9
  */
10
- export type QueryAggregate<T extends object> = {
10
+ export type QueryAggregate<T> = {
11
11
  method: "aggregate";
12
12
  /**
13
13
  * Common item properties to use for identifying the item.
@@ -4,7 +4,7 @@ import type { QuerySettings } from "./QuerySettings";
4
4
  /**
5
5
  * Query for creating an item.
6
6
  */
7
- export type QueryCreate<T extends object> = {
7
+ export type QueryCreate<T> = {
8
8
  method: "create";
9
9
  multiple?: false;
10
10
  /**
@@ -4,7 +4,7 @@ import type { QuerySettings } from "./QuerySettings";
4
4
  /**
5
5
  * Query for creating multiple items.
6
6
  */
7
- export type QueryCreateMultiple<T extends object> = {
7
+ export type QueryCreateMultiple<T> = {
8
8
  method: "create";
9
9
  multiple: true;
10
10
  /**
@@ -5,7 +5,7 @@ import type { QuerySettings } from "./QuerySettings";
5
5
  /**
6
6
  * Query for deleting an item.
7
7
  */
8
- export type QueryDelete<T extends object> = {
8
+ export type QueryDelete<T> = {
9
9
  method: "delete";
10
10
  multiple?: false;
11
11
  /**
@@ -5,7 +5,7 @@ import type { QuerySettings } from "./QuerySettings";
5
5
  /**
6
6
  * Query for deleting multiple items.
7
7
  */
8
- export type QueryDeleteMultiple<T extends object> = {
8
+ export type QueryDeleteMultiple<T> = {
9
9
  method: "delete";
10
10
  multiple: true;
11
11
  /**
@@ -6,7 +6,7 @@ import type { QuerySettings } from "./QuerySettings";
6
6
  /**
7
7
  * Query for reading a single item.
8
8
  */
9
- export type QueryRead<T extends object> = {
9
+ export type QueryRead<T> = {
10
10
  method?: "read";
11
11
  multiple?: false;
12
12
  /**
@@ -7,7 +7,7 @@ import type { QuerySettings } from "./QuerySettings";
7
7
  /**
8
8
  * Query for getting a list of items.
9
9
  */
10
- export type QueryReadMultiple<T extends object> = {
10
+ export type QueryReadMultiple<T> = {
11
11
  method?: "read";
12
12
  multiple: true;
13
13
  /**
@@ -4,7 +4,7 @@ import type { FilterChildren } from "./FilterChildren";
4
4
  /**
5
5
  * Settings to use when doing a query.
6
6
  */
7
- export type QuerySettings<T extends object> = {
7
+ export type QuerySettings<T> = {
8
8
  /**
9
9
  * String used to separate the path nodes of an item.
10
10
  */
@@ -5,7 +5,7 @@ import type { QuerySettings } from "./QuerySettings";
5
5
  /**
6
6
  * Query for updating an item.
7
7
  */
8
- export type QueryUpdate<T extends object> = {
8
+ export type QueryUpdate<T> = {
9
9
  method: "update";
10
10
  multiple?: false;
11
11
  /**
@@ -6,7 +6,7 @@ import type { QuerySettings } from "./QuerySettings";
6
6
  /**
7
7
  * Query for updating multiple items.
8
8
  */
9
- export type QueryUpdateMultiple<T extends object> = {
9
+ export type QueryUpdateMultiple<T> = {
10
10
  method: "update";
11
11
  multiple: true;
12
12
  /**
@@ -3,21 +3,21 @@ import type { Query } from "./Query";
3
3
  /**
4
4
  * Picks fields `F` from object `T`.
5
5
  */
6
- export type PickFields<T extends object | undefined, F extends string | number | symbol> = [F] extends [keyof T] ? {
6
+ export type PickFields<T, F extends string | number | symbol> = [F] extends [
7
+ keyof T
8
+ ] ? {
7
9
  readonly [K in F]: T[K];
8
10
  } : {
9
11
  readonly [K in keyof T]: T[K];
10
12
  };
11
- export type ResultRead<T extends object, Q extends Query<T>> = [Q] extends [
13
+ export type ResultRead<T, Q extends Query<T>> = [Q] extends [
12
14
  {
13
15
  fields: (keyof T)[];
14
16
  }
15
17
  ] ? PickFields<T, Item<Q["fields"]>> : T;
16
- export type Result<T extends object, Q extends Query<T>> = [Q] extends [
17
- {
18
- method: "read";
19
- }
20
- ] ? [Q] extends [{
18
+ export type Result<T, Q extends Query<T>> = [Q] extends [{
19
+ method: "read";
20
+ }] ? [Q] extends [{
21
21
  multiple: true;
22
22
  }] ? readonly ResultRead<T, Q>[] : ResultRead<T, Q> : [Q] extends [{
23
23
  method: "aggregate";
@@ -2,7 +2,7 @@ import type { FieldFiltered } from "./FieldFiltered";
2
2
  /**
3
3
  * Actual `value` or `valueField` leading to the value.
4
4
  */
5
- export type Value<T extends object, V> = {
5
+ export type Value<T, V> = {
6
6
  /**
7
7
  * Value to use.
8
8
  */
package/dist/types.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export type { AggregateFunction } from "./types/AggregateFunction";
2
2
  export type { AggregateFunctionOperator } from "./types/AggregateFunctionOperator";
3
- export type { Any } from "./types/Any";
3
+ export type { CombineUnion } from "./types/CombineUnion";
4
4
  export type { Context } from "./types/Context";
5
5
  export type { CustomFetch } from "./types/CustomFetch";
6
6
  export type { CustomFieldAggregateMap } from "./types/CustomFieldAggregateMap";
@@ -34,14 +34,14 @@ export type { Item } from "./types/Item";
34
34
  export type { Join } from "./types/Join";
35
35
  export type { Key } from "./types/Key";
36
36
  export type { KeyFiltered } from "./types/KeyFiltered";
37
+ export type { KeyFromUnion } from "./types/KeyFromUnion";
37
38
  export type { Locale } from "./types/Locale";
38
- export type { Mutable } from "./types/Mutable";
39
39
  export type { NextHandler } from "./types/NextHandler";
40
- export type { Normalized } from "./types/Normalized";
41
40
  export type { Order } from "./types/Order";
42
41
  export type { Parameters } from "./types/Parameters";
43
42
  export type { Path } from "./types/Path";
44
43
  export type { PathFiltered } from "./types/PathFiltered";
44
+ export type { Primitive } from "./types/Primitive";
45
45
  export type { Query } from "./types/Query";
46
46
  export type { QueryAggregate } from "./types/QueryAggregate";
47
47
  export type { QueryCreate } from "./types/QueryCreate";
@@ -0,0 +1 @@
1
+ export const SymbolSelf = Symbol("self");
package/lib/constants.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  // File automatically generated by `vite-plugin-module-list`
2
2
  export { SymbolCache } from "./constants/SymbolCache.js";
3
+ export { SymbolSelf } from "./constants/SymbolSelf.js";
@@ -6,7 +6,7 @@ import type { Handler, Query } from "../types";
6
6
  *
7
7
  * @returns Promise that resolves to the response.
8
8
  */
9
- export function fetchLocal<T extends object>(): Handler<
9
+ export function fetchLocal<T>(): Handler<
10
10
  Query<T> & { type: T[] },
11
11
  any,
12
12
  never,
@@ -6,7 +6,7 @@ import type { Query } from "../types";
6
6
  * @returns Function that checks queries.
7
7
  */
8
8
  export function defineCheckQuery<M extends Record<string, object>>() {
9
- function checkQuery<T extends object, const Q extends Query<T>>(
9
+ function checkQuery<T, const Q extends Query<T>>(
10
10
  query: Q & { type: T[] },
11
11
  ): typeof query;
12
12
  function checkQuery<
@@ -16,7 +16,7 @@ export function defineCustomFetch<M extends Record<string, object>>(
16
16
  T extends M[K],
17
17
  const Q extends Query<T>,
18
18
  >(query: Q & { type: K }): Promise<Result<T, Q>>;
19
- async function customFetch<T extends object, const Q extends Query<T>>(
19
+ async function customFetch<T, const Q extends Query<T>>(
20
20
  type: T[],
21
21
  query: Q,
22
22
  ): Promise<Result<T, Q>>;
@@ -15,7 +15,7 @@ export function defineGenericFetch<K extends string>(
15
15
  handler: Handler<Query<any> & { type: Key | any[] }, any, never, never>,
16
16
  ) {
17
17
  // FIXME: Until https://github.com/microsoft/TypeScript/issues/26242 gets resolved, the fetcher needs to be curried
18
- function genericFetch<T extends object>() {
18
+ function genericFetch<T>() {
19
19
  async function customFetch<const Q extends Query<T>>(
20
20
  query: Q & { type: K | T[] },
21
21
  ): Promise<Result<T, Q>> {
@@ -8,7 +8,7 @@ import type { Field, Filter } from "../types";
8
8
  * @param filter The filter to extract the fields from.
9
9
  * @returns A list of fields.
10
10
  */
11
- export function fieldListFromFilter<T extends object>(
11
+ export function fieldListFromFilter<T>(
12
12
  filter?: Filter<T>,
13
13
  ): readonly Field<T>[] {
14
14
  if (filter === undefined) {
@@ -2,7 +2,7 @@ import type { FieldFiltered, Filter } from "../types.ts";
2
2
 
3
3
  import { escapeRegex } from "./escapeRegex.js";
4
4
 
5
- export function filterChildren<T extends object>(
5
+ export function filterChildren<T>(
6
6
  parentPath: string,
7
7
  pathFieldKey: FieldFiltered<T, string>,
8
8
  deep = false,
@@ -23,6 +23,36 @@ test("returns filter from context", () => {
23
23
  },
24
24
  ],
25
25
  });
26
+ expect(filterFromContext({ a: { b: undefined } })).toEqual({
27
+ operator: "all",
28
+ value: [
29
+ {
30
+ field: ["a", "b"],
31
+ operator: "equal",
32
+ value: undefined,
33
+ },
34
+ ],
35
+ });
36
+ expect(filterFromContext({ a: { b: null } })).toEqual({
37
+ operator: "all",
38
+ value: [
39
+ {
40
+ field: ["a", "b"],
41
+ operator: "equal",
42
+ value: null,
43
+ },
44
+ ],
45
+ });
46
+ expect(filterFromContext({ a: null })).toEqual({
47
+ operator: "all",
48
+ value: [
49
+ {
50
+ field: "a",
51
+ operator: "equal",
52
+ value: null,
53
+ },
54
+ ],
55
+ });
26
56
  expect(filterFromContext({ a: { b: 1 }, c: 2 })).toEqual({
27
57
  operator: "all",
28
58
  value: [
@@ -4,16 +4,14 @@ import type { Context, Filter } from "../types";
4
4
 
5
5
  const { entries } = Object;
6
6
 
7
- export function filterFromContext<T extends object>(
8
- context: Context<T>,
9
- ): Filter<T> {
7
+ export function filterFromContext<T>(context: Context<T>): Filter<T> {
10
8
  return {
11
9
  operator: "all",
12
10
  value: criteria(context),
13
11
  };
14
12
  }
15
13
 
16
- function criteria<T extends object>(
14
+ function criteria<T>(
17
15
  context: object,
18
16
  path: readonly string[] = EMPTY_ARRAY,
19
17
  ): readonly Filter<T>[] {
@@ -32,8 +30,18 @@ function criteria<T extends object>(
32
30
  value,
33
31
  } as Filter<T>,
34
32
  ];
35
- case "object":
33
+ case "object": {
34
+ if (value == null) {
35
+ return [
36
+ {
37
+ field: path.length === 0 ? field : [...path, field],
38
+ operator: "equal",
39
+ value,
40
+ } as Filter<T>,
41
+ ];
42
+ }
36
43
  return criteria(value, [...path, field]);
44
+ }
37
45
  default:
38
46
  return EMPTY_ARRAY;
39
47
  // Ignore
@@ -14,6 +14,7 @@ test("gets properties", () => {
14
14
  expect(result2).toBe(value.a.d.e[2].f);
15
15
 
16
16
  const result3 = get(value, ["a"]);
17
+ expect(result3.b).toBe(value.a.b);
17
18
  expect(result3).toBe(value.a);
18
19
 
19
20
  const result4 = get(value, "a");
package/lib/tools/get.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Field, Get } from "../types";
1
+ import type { Get, Path } from "../types";
2
2
 
3
3
  /**
4
4
  * Gets the property value of the given `value` at the specified `path` (an array of object property names or array indexes).
@@ -10,7 +10,10 @@ import type { Field, Get } from "../types";
10
10
  * @param path The path leading to the property value or a property name or `undefined`.
11
11
  * @returns The property value found at the given path, or `undefined` if it cannot be found.
12
12
  */
13
- export function get<T, P extends Field<T>>(value: T, path?: P): Get<T, P> {
13
+ export function get<const T, const P extends Path<T> | keyof T>(
14
+ value: T,
15
+ path?: P,
16
+ ): Get<T, P> {
14
17
  if (path === undefined) {
15
18
  return value as any;
16
19
  }
@@ -20,19 +23,21 @@ export function get<T, P extends Field<T>>(value: T, path?: P): Get<T, P> {
20
23
  case "symbol":
21
24
  return (value as any)?.[path];
22
25
  default: {
23
- switch (path.length) {
26
+ switch (path.length as number) {
27
+ case 0:
28
+ return value as any;
24
29
  case 1:
25
30
  return (value as any)?.[path[0] as any];
26
31
  case 2:
27
- return (value as any)?.[path[0] as any]?.[path[1] as any];
32
+ return (value as any)?.[path[0] as any]?.[(path as any)[1] as any];
28
33
  case 3:
29
- return (value as any)?.[path[0] as any]?.[path[1] as any]?.[
30
- path[2] as any
34
+ return (value as any)?.[path[0] as any]?.[(path as any)[1] as any]?.[
35
+ (path as any)[2] as any
31
36
  ];
32
37
  case 4:
33
- return (value as any)?.[path[0] as any]?.[path[1] as any]?.[
34
- path[2] as any
35
- ]?.[path[3] as any];
38
+ return (value as any)?.[path[0] as any]?.[(path as any)[1] as any]?.[
39
+ (path as any)[2] as any
40
+ ]?.[(path as any)[3] as any];
36
41
  default:
37
42
  return path.reduce(
38
43
  (value, key) => (value as any)?.[key],
@@ -6,7 +6,7 @@ import type { Filter, FilterGroup } from "../types";
6
6
  * @param value The filter to test.
7
7
  * @returns Whether the provided filter is a group filter or not.
8
8
  */
9
- export function isFilterGroup<T extends object>(
9
+ export function isFilterGroup<T>(
10
10
  value: Filter<T> | undefined,
11
11
  ): value is FilterGroup<T> {
12
12
  switch (value?.operator) {
@@ -8,9 +8,7 @@ const isArray = Array.isArray as (value: any) => value is readonly any[];
8
8
  * @param order The string or order object.
9
9
  * @returns The normalized order object.
10
10
  */
11
- export function normalizeOrder<T extends object>(
12
- order: Order<T>,
13
- ): {
11
+ export function normalizeOrder<T>(order: Order<T>): {
14
12
  field: Field<T>;
15
13
  descending?: boolean | undefined;
16
14
  } {
@@ -5,7 +5,7 @@ import { queryItemList } from "./queryItemList.js";
5
5
  test("queries a single item", () => {
6
6
  expect(
7
7
  queryItemList({
8
- type: [{ a: 1 }, { a: 2 }, { a: 3 }] as { a: number }[],
8
+ type: [{ a: 1 }, { a: 2 }, { a: 3 }],
9
9
  }),
10
10
  ).toEqual({ a: 1 });
11
11
  expect(() =>
@@ -13,7 +13,7 @@ test("queries a single item", () => {
13
13
  context: {
14
14
  a: 4,
15
15
  },
16
- type: [{ a: 1 }, { a: 2 }, { a: 3 }] as { a: number }[],
16
+ type: [{ a: 1 }, { a: 2 }, { a: 3 }] as readonly { a: number }[],
17
17
  }),
18
18
  ).toThrow("Not found");
19
19
  expect(
@@ -11,7 +11,7 @@ import { filterFromContext } from "./filterFromContext.js";
11
11
  import { sortItemList } from "./sortItemList.js";
12
12
  import { testFilter } from "./testFilter.js";
13
13
 
14
- function mergeContextAndFilter<T extends object>(
14
+ function mergeContextAndFilter<T>(
15
15
  context?: Context<T>,
16
16
  filter?: Filter<T>,
17
17
  ): Filter<T> | undefined {
@@ -30,7 +30,7 @@ function mergeContextAndFilter<T extends object>(
30
30
  };
31
31
  }
32
32
 
33
- function normalizeAggregator<T extends object>(
33
+ function normalizeAggregator<T>(
34
34
  aggregator: AggregateFunction<T>,
35
35
  ): Exclude<AggregateFunction<T>, string> {
36
36
  if (typeof aggregator === "string") {
@@ -45,7 +45,7 @@ function normalizeAggregator<T extends object>(
45
45
  * @param query The query to perform.
46
46
  * @returns The result of the query.
47
47
  */
48
- export function queryItemList<T extends object, const Q extends Query<T>>(
48
+ export function queryItemList<T, const Q extends Query<T>>(
49
49
  query: Q & { type: readonly T[] },
50
50
  ): Result<T, Q> {
51
51
  const data = query.type;
@@ -8,9 +8,7 @@ import { normalizeOrder } from "./normalizeOrder.js";
8
8
  * @param order The order to reverse.
9
9
  * @returns The reversed order.
10
10
  */
11
- export function reverseOrder<T extends object>(
12
- order: Order<T>,
13
- ): {
11
+ export function reverseOrder<T>(order: Order<T>): {
14
12
  field: Field<T>;
15
13
  descending?: boolean | undefined;
16
14
  } {
@@ -10,7 +10,7 @@ import { normalizeOrder } from "./normalizeOrder.js";
10
10
  * @param value The array to sort.
11
11
  * @returns A new sorted array.
12
12
  */
13
- export function sortItemList<T extends object>(
13
+ export function sortItemList<T>(
14
14
  orderList: readonly Order<T>[] | undefined,
15
15
  value: readonly T[],
16
16
  ) {
@@ -21,8 +21,8 @@ export function sortItemList<T extends object>(
21
21
  return [...value].sort((a, b) => {
22
22
  for (let index = 0; index < normalizedOrder.length; index++) {
23
23
  const { field, descending } = normalizedOrder[index]!;
24
- const valueA = get(a, field);
25
- const valueB = get(b, field);
24
+ const valueA = get(a, field as any);
25
+ const valueB = get(b, field as any);
26
26
  if (valueA === valueB) {
27
27
  continue;
28
28
  }
@@ -290,7 +290,7 @@ test("tests filter with children predicates", () => {
290
290
  },
291
291
  ),
292
292
  ).toBe(true);
293
- const filterChildren: FilterChildren<any> = {
293
+ const filterChildren: FilterChildren<{ path: string }> = {
294
294
  operator: "children",
295
295
  value: "a",
296
296
  };
@@ -1,16 +1,13 @@
1
1
  import { SymbolCache } from "../constants/SymbolCache.js";
2
2
  import type { QuerySettings } from "../types/QuerySettings.js";
3
- import type { Any, FieldFiltered, Filter, FilterString } from "../types.js";
3
+ import type { FieldFiltered, Filter, FilterString } from "../types.js";
4
4
 
5
5
  import { filterChildren } from "./filterChildren.js";
6
6
  import { get } from "./get.js";
7
7
 
8
8
  const { isArray } = Array;
9
9
 
10
- function valueFromFilter<T extends object, F extends Filter<T>>(
11
- value: T,
12
- filter: F,
13
- ): F extends { value: infer V } ? V : never {
10
+ function valueFromFilter<T>(value: T, filter: Filter<T>): any {
14
11
  if ("valueField" in filter) {
15
12
  filter.valueField;
16
13
  return get<T, any>(value, filter.valueField) as any;
@@ -26,7 +23,7 @@ function valueFromFilter<T extends object, F extends Filter<T>>(
26
23
  * @param settings Optional query settings.
27
24
  * @returns `true` if the `value` matches the `filter` and `false` otherwise.
28
25
  */
29
- export function testFilter<T extends object>(
26
+ export function testFilter<T>(
30
27
  filter: Filter<T> | undefined,
31
28
  value: T | undefined,
32
29
  settings?: QuerySettings<T>,
@@ -53,12 +50,12 @@ export function testFilter<T extends object>(
53
50
  return filter.value.every((filter) => !testFilter(filter, value));
54
51
  }
55
52
  case "exist":
56
- return get(value, filter.field) !== undefined;
53
+ return get(value, filter.field as any) !== undefined;
57
54
  case "equal": {
58
- const rightValue = valueFromFilter(value, filter);
55
+ const rightValue = valueFromFilter(value, filter as Filter<any>);
59
56
  if (isArray(rightValue)) {
60
57
  filter.field;
61
- const leftValue = get<T, any>(value, filter.field) as Any[] | undefined;
58
+ const leftValue = get<T, any>(value, filter.field);
62
59
  if (!isArray(leftValue)) {
63
60
  return false;
64
61
  }
@@ -1,9 +1,10 @@
1
+ import type { Field } from "./Field";
1
2
  import type { Filter } from "./Filter";
2
3
 
3
4
  /**
4
5
  * Aggregation function.
5
6
  */
6
- export type AggregateFunction<T extends object> =
7
+ export type AggregateFunction<T> =
7
8
  | "length"
8
9
  | { operator: "length" }
9
10
  | { operator: "index"; filter?: Filter<T>; last?: boolean }
@@ -16,5 +17,5 @@ export type AggregateFunction<T extends object> =
16
17
  | "maximum"
17
18
  | "variance"
18
19
  | "mode";
19
- field: keyof T;
20
+ field: Field<T>;
20
21
  };
@@ -0,0 +1,18 @@
1
+ import type { KeyFromUnion } from "./KeyFromUnion";
2
+
3
+ export type CombineUnion<U> = [0] extends [1 & U]
4
+ ? any
5
+ : [U] extends [Function | readonly Function[]]
6
+ ? undefined
7
+ : [U] extends readonly [Array<any>]
8
+ ? U
9
+ : {
10
+ [K in KeyFromUnion<U>]: Continue<Combine<U, K>>;
11
+ };
12
+
13
+ type Combine<U, K extends string | number | symbol> = Exclude<
14
+ Extract<U, { [k in K]?: any }>[K],
15
+ undefined
16
+ >;
17
+
18
+ type Continue<U> = U extends object ? CombineUnion<U> : U;
@@ -1,3 +1,3 @@
1
- export type Context<T extends object> = {
1
+ export type Context<T> = {
2
2
  [K in keyof T]?: T[K];
3
3
  };
@@ -1,6 +1,6 @@
1
1
  import type { Query } from "./Query";
2
2
  import type { Result } from "./Result";
3
3
 
4
- export interface CustomFetch<T extends object> {
4
+ export interface CustomFetch<T> {
5
5
  <Q extends Query<T>>(query: Q): Promise<Result<T, Q>>;
6
6
  }
@@ -1,6 +1,3 @@
1
1
  import type { AggregateFunction } from "./AggregateFunction";
2
2
 
3
- export type CustomFieldAggregateMap<T extends object> = Record<
4
- string,
5
- AggregateFunction<T>
6
- >;
3
+ export type CustomFieldAggregateMap<T> = Record<string, AggregateFunction<T>>;
@@ -1,3 +1,3 @@
1
1
  import type { FieldFunction } from "./FieldFunction";
2
2
 
3
- export type CustomFieldMap<T extends object> = Record<string, FieldFunction<T>>;
3
+ export type CustomFieldMap<T> = Record<string, FieldFunction<T>>;
@@ -1,6 +1,6 @@
1
- import type { Normalized } from "./Normalized";
1
+ import type { CombineUnion } from "./CombineUnion";
2
2
  import type { Path } from "./Path";
3
3
 
4
- export type Field<T> = T extends object
5
- ? keyof Normalized<T> | Path<Normalized<T>>
6
- : never;
4
+ export type Field<T> = KeyOrPath<CombineUnion<T>>;
5
+
6
+ type KeyOrPath<T> = keyof T | Path<T>;
@@ -1,7 +1,7 @@
1
+ import type { CombineUnion } from "./CombineUnion";
1
2
  import type { KeyFiltered } from "./KeyFiltered";
2
- import type { Normalized } from "./Normalized";
3
3
  import type { PathFiltered } from "./PathFiltered";
4
4
 
5
- export type FieldFiltered<T extends object, P> =
6
- | KeyFiltered<Normalized<T>, P>
7
- | PathFiltered<Normalized<T>, P>;
5
+ export type FieldFiltered<T, P> = KeyOrPathFiltered<CombineUnion<T>, P>;
6
+
7
+ type KeyOrPathFiltered<T, P> = KeyFiltered<T, P> | PathFiltered<T, P>;
@@ -1,6 +1,6 @@
1
1
  import type { FieldFunctionCustom } from "./FieldFunctionCustom";
2
2
  import type { FieldFunctionFormatDate } from "./FieldFunctionFormatDate";
3
3
 
4
- export type FieldFunction<T extends object> =
4
+ export type FieldFunction<T> =
5
5
  | FieldFunctionCustom<T>
6
6
  | FieldFunctionFormatDate<T>;
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Applies a custom field transform function.
3
3
  */
4
- export type FieldFunctionCustom<T extends object> = {
4
+ export type FieldFunctionCustom<T> = {
5
5
  operator: "custom";
6
6
  value: (item: T) => any;
7
7
  };