quetch 0.18.3 → 0.19.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.
Files changed (167) hide show
  1. package/dist/constants/CACHE.d.ts +1 -0
  2. package/dist/constants/CACHE.js +2 -0
  3. package/dist/constants/CACHE.js.map +1 -0
  4. package/dist/constants/SELF.d.ts +1 -0
  5. package/dist/constants/SELF.js +2 -0
  6. package/dist/constants/SELF.js.map +1 -0
  7. package/dist/constants.d.ts +2 -1
  8. package/dist/constants.js +2 -1
  9. package/dist/constants.js.map +1 -1
  10. package/dist/middlewares/fetchLocal.d.ts +1 -1
  11. package/dist/tools/defineCheckQuery.d.ts +1 -1
  12. package/dist/tools/defineCustomFetch.d.ts +1 -1
  13. package/dist/tools/defineGenericFetch.d.ts +1 -1
  14. package/dist/tools/fieldListFromFilter.d.ts +1 -1
  15. package/dist/tools/filterChildren.d.ts +1 -1
  16. package/dist/tools/filterFromContext.d.ts +1 -1
  17. package/dist/tools/filterFromContext.js.map +1 -1
  18. package/dist/tools/get.d.ts +2 -2
  19. package/dist/tools/get.js +4 -1
  20. package/dist/tools/get.js.map +1 -1
  21. package/dist/tools/isFilterGroup.d.ts +1 -1
  22. package/dist/tools/normalizeOrder.d.ts +1 -1
  23. package/dist/tools/normalizeOrder.js.map +1 -1
  24. package/dist/tools/queryItemList.d.ts +1 -1
  25. package/dist/tools/reverseOrder.d.ts +1 -1
  26. package/dist/tools/reverseOrder.js.map +1 -1
  27. package/dist/tools/sortItemList.d.ts +1 -1
  28. package/dist/tools/sortItemList.js.map +1 -1
  29. package/dist/tools/testFilter.d.ts +1 -1
  30. package/dist/tools/testFilter.js +8 -8
  31. package/dist/tools/testFilter.js.map +1 -1
  32. package/dist/types/AggregateFunction.d.ts +3 -2
  33. package/dist/types/CombineUnion.d.ts +9 -0
  34. package/dist/types/CombineUnion.js +1 -0
  35. package/dist/types/CombineUnion.js.map +1 -0
  36. package/dist/types/Context.d.ts +1 -1
  37. package/dist/types/CustomFetch.d.ts +1 -1
  38. package/dist/types/CustomFieldAggregateMap.d.ts +1 -1
  39. package/dist/types/CustomFieldMap.d.ts +1 -1
  40. package/dist/types/Field.d.ts +4 -2
  41. package/dist/types/FieldFiltered.d.ts +4 -2
  42. package/dist/types/FieldFunction.d.ts +1 -1
  43. package/dist/types/FieldFunctionCustom.d.ts +1 -1
  44. package/dist/types/FieldFunctionFormatDate.d.ts +1 -1
  45. package/dist/types/Filter.d.ts +1 -1
  46. package/dist/types/FilterArray.d.ts +1 -2
  47. package/dist/types/FilterBoolean.d.ts +1 -1
  48. package/dist/types/FilterChildren.d.ts +3 -3
  49. package/dist/types/FilterCustom.d.ts +1 -1
  50. package/dist/types/FilterField.d.ts +1 -1
  51. package/dist/types/FilterGroup.d.ts +1 -1
  52. package/dist/types/FilterNumber.d.ts +1 -1
  53. package/dist/types/FilterString.d.ts +1 -1
  54. package/dist/types/FilterStringIntersect.d.ts +1 -1
  55. package/dist/types/FilterStringMatch.d.ts +3 -3
  56. package/dist/types/Get.d.ts +1 -1
  57. package/dist/types/Group.d.ts +1 -1
  58. package/dist/types/Immutable.d.ts +1 -1
  59. package/dist/types/InjectCustomFields.d.ts +1 -1
  60. package/dist/types/KeyFiltered.d.ts +10 -4
  61. package/dist/types/KeyFromUnion.d.ts +1 -0
  62. package/dist/types/KeyFromUnion.js +1 -0
  63. package/dist/types/KeyFromUnion.js.map +1 -0
  64. package/dist/types/Order.d.ts +1 -1
  65. package/dist/types/Path.d.ts +4 -2
  66. package/dist/types/PathFiltered.d.ts +3 -2
  67. package/dist/types/Primitive.d.ts +1 -0
  68. package/dist/types/Primitive.js +1 -0
  69. package/dist/types/Primitive.js.map +1 -0
  70. package/dist/types/Query.d.ts +2 -2
  71. package/dist/types/QueryAggregate.d.ts +1 -1
  72. package/dist/types/QueryCreate.d.ts +1 -1
  73. package/dist/types/QueryCreateMultiple.d.ts +1 -1
  74. package/dist/types/QueryDelete.d.ts +1 -1
  75. package/dist/types/QueryDeleteMultiple.d.ts +1 -1
  76. package/dist/types/QueryRead.d.ts +1 -1
  77. package/dist/types/QueryReadMultiple.d.ts +1 -1
  78. package/dist/types/QuerySettings.d.ts +1 -1
  79. package/dist/types/QueryUpdate.d.ts +1 -1
  80. package/dist/types/QueryUpdateMultiple.d.ts +1 -1
  81. package/dist/types/Result.d.ts +7 -7
  82. package/dist/types/Value.d.ts +1 -1
  83. package/dist/types.d.ts +3 -3
  84. package/lib/constants/CACHE.ts +1 -0
  85. package/lib/constants/SELF.ts +1 -0
  86. package/lib/constants.ts +2 -1
  87. package/lib/middlewares/fetchLocal.ts +1 -1
  88. package/lib/tools/defineCheckQuery.ts +1 -1
  89. package/lib/tools/defineCustomFetch.ts +1 -1
  90. package/lib/tools/defineGenericFetch.ts +1 -1
  91. package/lib/tools/fieldListFromFilter.ts +1 -1
  92. package/lib/tools/filterChildren.ts +1 -1
  93. package/lib/tools/filterFromContext.test.ts +10 -0
  94. package/lib/tools/filterFromContext.ts +2 -4
  95. package/lib/tools/get.test.ts +1 -0
  96. package/lib/tools/get.ts +16 -10
  97. package/lib/tools/isFilterGroup.ts +1 -1
  98. package/lib/tools/normalizeOrder.ts +1 -3
  99. package/lib/tools/queryItemList.test.ts +2 -2
  100. package/lib/tools/queryItemList.ts +3 -3
  101. package/lib/tools/reverseOrder.ts +1 -3
  102. package/lib/tools/sortItemList.ts +3 -3
  103. package/lib/tools/testFilter.test.ts +3 -3
  104. package/lib/tools/testFilter.ts +14 -17
  105. package/lib/types/AggregateFunction.ts +3 -2
  106. package/lib/types/CombineUnion.ts +18 -0
  107. package/lib/types/Context.ts +1 -1
  108. package/lib/types/CustomFetch.ts +1 -1
  109. package/lib/types/CustomFieldAggregateMap.ts +1 -4
  110. package/lib/types/CustomFieldMap.ts +1 -1
  111. package/lib/types/Field.ts +4 -4
  112. package/lib/types/FieldFiltered.ts +4 -4
  113. package/lib/types/FieldFunction.ts +1 -1
  114. package/lib/types/FieldFunctionCustom.ts +1 -1
  115. package/lib/types/FieldFunctionFormatDate.ts +1 -1
  116. package/lib/types/Filter.ts +1 -1
  117. package/lib/types/FilterArray.ts +1 -2
  118. package/lib/types/FilterBoolean.ts +1 -1
  119. package/lib/types/FilterChildren.ts +3 -3
  120. package/lib/types/FilterCustom.ts +1 -1
  121. package/lib/types/FilterField.ts +1 -1
  122. package/lib/types/FilterGroup.ts +1 -1
  123. package/lib/types/FilterNumber.ts +1 -1
  124. package/lib/types/FilterString.ts +1 -1
  125. package/lib/types/FilterStringIntersect.ts +1 -1
  126. package/lib/types/FilterStringMatch.ts +3 -3
  127. package/lib/types/Get.ts +1 -1
  128. package/lib/types/Group.ts +1 -1
  129. package/lib/types/Immutable.ts +1 -1
  130. package/lib/types/InjectCustomFields.ts +1 -4
  131. package/lib/types/KeyFiltered.ts +29 -5
  132. package/lib/types/KeyFromUnion.ts +1 -0
  133. package/lib/types/Order.ts +1 -1
  134. package/lib/types/Path.ts +15 -9
  135. package/lib/types/PathFiltered.ts +16 -13
  136. package/lib/types/Primitive.ts +1 -0
  137. package/lib/types/Query.ts +2 -2
  138. package/lib/types/QueryAggregate.ts +1 -1
  139. package/lib/types/QueryCreate.ts +1 -1
  140. package/lib/types/QueryCreateMultiple.ts +1 -1
  141. package/lib/types/QueryDelete.ts +1 -1
  142. package/lib/types/QueryDeleteMultiple.ts +1 -1
  143. package/lib/types/QueryRead.ts +1 -1
  144. package/lib/types/QueryReadMultiple.ts +1 -1
  145. package/lib/types/QuerySettings.ts +1 -1
  146. package/lib/types/QueryUpdate.ts +1 -1
  147. package/lib/types/QueryUpdateMultiple.ts +1 -1
  148. package/lib/types/Result.ts +5 -8
  149. package/lib/types/Value.ts +1 -1
  150. package/lib/types.ts +3 -3
  151. package/package.json +6 -5
  152. package/dist/constants/SymbolCache.d.ts +0 -1
  153. package/dist/constants/SymbolCache.js +0 -2
  154. package/dist/constants/SymbolCache.js.map +0 -1
  155. package/dist/types/Any.d.ts +0 -1
  156. package/dist/types/Any.js +0 -1
  157. package/dist/types/Any.js.map +0 -1
  158. package/dist/types/Mutable.d.ts +0 -3
  159. package/dist/types/Mutable.js +0 -1
  160. package/dist/types/Mutable.js.map +0 -1
  161. package/dist/types/Normalized.d.ts +0 -2
  162. package/dist/types/Normalized.js +0 -1
  163. package/dist/types/Normalized.js.map +0 -1
  164. package/lib/constants/SymbolCache.ts +0 -1
  165. package/lib/types/Any.ts +0 -10
  166. package/lib/types/Mutable.ts +0 -3
  167. package/lib/types/Normalized.ts +0 -3
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=KeyFromUnion.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeyFromUnion.js","sourceRoot":"","sources":["../../lib/types/KeyFromUnion.ts"],"names":[],"mappings":""}
@@ -2,7 +2,7 @@ import type { Field } from "./Field";
2
2
  /**
3
3
  * Order item.
4
4
  */
5
- export type Order<T extends object> = Field<T> | {
5
+ export type Order<T> = Field<T> | {
6
6
  field: Field<T>;
7
7
  descending?: boolean;
8
8
  };
@@ -1,4 +1,6 @@
1
1
  import type { Increment } from "./Increment";
2
- export type Path<T, D = 0> = D extends 2 ? never : T extends Array<infer P> ? readonly [number] | readonly [number, ...Path<P, Increment<D>>] : T extends object ? {
3
- [K in keyof T]-?: [K] | [K, ...Path<T[K], Increment<D>>];
2
+ import type { Key } from "./Key";
3
+ import type { Primitive } from "./Primitive";
4
+ export type Path<T, D = 7> = [0] extends [1 & T] ? readonly (Key | never)[] : D extends -1 ? never : T extends Array<infer P> ? readonly [number] | readonly [number, ...Path<P, Increment<D>>] : T extends object ? T extends Primitive ? readonly never[] : {
5
+ [K in keyof T]: [K] | [K, ...Path<T[K], Increment<D>>];
4
6
  }[keyof T] : never;
@@ -1,4 +1,5 @@
1
1
  import type { Increment } from "./Increment";
2
- export type PathFiltered<T, P, D = 0> = D extends 4 ? never : T extends ReadonlyArray<infer I> ? I extends P ? readonly [number] : readonly [number, ...PathFiltered<I, P, Increment<D>>] : T extends object ? {
3
- [K in keyof T]-?: T[K] extends P ? readonly [K] : readonly [K, ...PathFiltered<T[K], P, Increment<D>>];
2
+ import type { Key } from "./Key";
3
+ export type PathFiltered<T, P, D = 7> = [0] extends [1 & T] ? readonly (Key | never)[] : D extends -1 ? never : T extends ReadonlyArray<infer I> ? Extract<I, P> extends never ? readonly [number, ...PathFiltered<I, P, Increment<D>>] : readonly [number] : T extends object ? {
4
+ [K in keyof T]: Extract<T[K], P> extends never ? readonly [K, ...PathFiltered<T[K], P, Increment<D>>] : readonly [K];
4
5
  }[keyof T] : never;
@@ -0,0 +1 @@
1
+ export type Primitive = String | Number | Boolean | BigInt;
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=Primitive.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Primitive.js","sourceRoot":"","sources":["../../lib/types/Primitive.ts"],"names":[],"mappings":""}
@@ -12,6 +12,6 @@ import type { QueryUpdateMultiple } from "./QueryUpdateMultiple";
12
12
  */
13
13
  export type Query<
14
14
  /**
15
- * Type of the object item to be queried.
15
+ * Type of the item to be queried.
16
16
  */
17
- T extends object> = QueryRead<T> | QueryReadMultiple<T> | QueryCreate<T> | QueryCreateMultiple<T> | QueryUpdate<T> | QueryUpdateMultiple<T> | QueryDelete<T> | QueryDeleteMultiple<T> | QueryAggregate<T>;
17
+ T> = QueryRead<T> | QueryReadMultiple<T> | QueryCreate<T> | QueryCreateMultiple<T> | QueryUpdate<T> | QueryUpdateMultiple<T> | QueryDelete<T> | QueryDeleteMultiple<T> | QueryAggregate<T>;
@@ -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 CACHE = Symbol("cache");
@@ -0,0 +1 @@
1
+ export const SELF = Symbol("self");
package/lib/constants.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  // File automatically generated by `vite-plugin-module-list`
2
- export { SymbolCache } from "./constants/SymbolCache.js";
2
+ export { CACHE } from "./constants/CACHE.js";
3
+ export { SELF } from "./constants/SELF.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,
@@ -43,6 +43,16 @@ test("returns filter from context", () => {
43
43
  },
44
44
  ],
45
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
+ });
46
56
  expect(filterFromContext({ a: { b: 1 }, c: 2 })).toEqual({
47
57
  operator: "all",
48
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>[] {
@@ -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,5 @@
1
- import type { Field, Get } from "../types";
1
+ import { SELF } from "../constants/SELF.js";
2
+ import type { Get, Path } from "../types";
2
3
 
3
4
  /**
4
5
  * Gets the property value of the given `value` at the specified `path` (an array of object property names or array indexes).
@@ -10,8 +11,11 @@ import type { Field, Get } from "../types";
10
11
  * @param path The path leading to the property value or a property name or `undefined`.
11
12
  * @returns The property value found at the given path, or `undefined` if it cannot be found.
12
13
  */
13
- export function get<T, P extends Field<T>>(value: T, path?: P): Get<T, P> {
14
- if (path === undefined) {
14
+ export function get<const T, const P extends Path<T> | keyof T>(
15
+ value: T,
16
+ path?: P,
17
+ ): Get<T, P> {
18
+ if (path === undefined || path === SELF) {
15
19
  return value as any;
16
20
  }
17
21
  switch (typeof path) {
@@ -20,19 +24,21 @@ export function get<T, P extends Field<T>>(value: T, path?: P): Get<T, P> {
20
24
  case "symbol":
21
25
  return (value as any)?.[path];
22
26
  default: {
23
- switch (path.length) {
27
+ switch (path.length as number) {
28
+ case 0:
29
+ return value as any;
24
30
  case 1:
25
31
  return (value as any)?.[path[0] as any];
26
32
  case 2:
27
- return (value as any)?.[path[0] as any]?.[path[1] as any];
33
+ return (value as any)?.[path[0] as any]?.[(path as any)[1] as any];
28
34
  case 3:
29
- return (value as any)?.[path[0] as any]?.[path[1] as any]?.[
30
- path[2] as any
35
+ return (value as any)?.[path[0] as any]?.[(path as any)[1] as any]?.[
36
+ (path as any)[2] as any
31
37
  ];
32
38
  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];
39
+ return (value as any)?.[path[0] as any]?.[(path as any)[1] as any]?.[
40
+ (path as any)[2] as any
41
+ ]?.[(path as any)[3] as any];
36
42
  default:
37
43
  return path.reduce(
38
44
  (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
  }
@@ -1,6 +1,6 @@
1
1
  import { expect, test } from "vitest";
2
2
 
3
- import { SymbolCache } from "../constants.js";
3
+ import { CACHE } from "../constants/CACHE.js";
4
4
  import type { FilterChildren } from "../types.js";
5
5
 
6
6
  import { filterFromContext } from "./filterFromContext.js";
@@ -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
  };
@@ -309,7 +309,7 @@ test("tests filter with children predicates", () => {
309
309
  },
310
310
  ),
311
311
  ).toBe(true);
312
- expect(filterChildren[SymbolCache]).toBeDefined();
312
+ expect(filterChildren[CACHE]).toBeDefined();
313
313
  expect(testFilter({ operator: "children", value: "b" }, { id: "a/b" })).toBe(
314
314
  false,
315
315
  );
@@ -1,16 +1,13 @@
1
- import { SymbolCache } from "../constants/SymbolCache.js";
1
+ import { CACHE } from "../constants/CACHE.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
  }
@@ -107,17 +104,17 @@ export function testFilter<T extends object>(
107
104
  return true;
108
105
  }
109
106
  case "children": {
110
- if (filter[SymbolCache] === undefined) {
107
+ if (filter[CACHE] === undefined) {
111
108
  switch (true) {
112
109
  case settings?.transformFilterChildren !== undefined:
113
- filter[SymbolCache] = settings.transformFilterChildren(filter);
110
+ filter[CACHE] = settings.transformFilterChildren(filter);
114
111
  break;
115
112
  default: {
116
113
  const {
117
114
  pathFieldKey = "id" as FieldFiltered<T, string>,
118
115
  pathFieldSeparator = "/",
119
116
  } = settings || {};
120
- filter[SymbolCache] = filterChildren(
117
+ filter[CACHE] = filterChildren(
121
118
  filter.value as string,
122
119
  pathFieldKey,
123
120
  filter.deep,
@@ -126,7 +123,7 @@ export function testFilter<T extends object>(
126
123
  }
127
124
  }
128
125
  }
129
- return testFilter(filter[SymbolCache], value);
126
+ return testFilter(filter[CACHE], value);
130
127
  }
131
128
  case "custom": {
132
129
  return filter.value(value);
@@ -276,15 +273,15 @@ export function testFilter<T extends object>(
276
273
  if (leftValue === undefined) {
277
274
  return false;
278
275
  }
279
- if (filter[SymbolCache] === undefined) {
280
- filter[SymbolCache] = new RegExp(
276
+ if (filter[CACHE] === undefined) {
277
+ filter[CACHE] = new RegExp(
281
278
  rightValue,
282
279
  `${filter.options?.ignoreCase ? "i" : ""}${
283
280
  filter.options?.dotAll ? "s" : ""
284
281
  }`,
285
282
  );
286
283
  }
287
- return filter[SymbolCache].test(leftValue);
284
+ return filter[CACHE].test(leftValue);
288
285
  }
289
286
  case "intersect": {
290
287
  const rightValue = valueFromFilter(value, filter);
@@ -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
  };