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.
- package/dist/constants/CACHE.d.ts +1 -0
- package/dist/constants/CACHE.js +2 -0
- package/dist/constants/CACHE.js.map +1 -0
- package/dist/constants/SELF.d.ts +1 -0
- package/dist/constants/SELF.js +2 -0
- package/dist/constants/SELF.js.map +1 -0
- package/dist/constants.d.ts +2 -1
- package/dist/constants.js +2 -1
- package/dist/constants.js.map +1 -1
- package/dist/middlewares/fetchLocal.d.ts +1 -1
- package/dist/tools/defineCheckQuery.d.ts +1 -1
- package/dist/tools/defineCustomFetch.d.ts +1 -1
- package/dist/tools/defineGenericFetch.d.ts +1 -1
- package/dist/tools/fieldListFromFilter.d.ts +1 -1
- package/dist/tools/filterChildren.d.ts +1 -1
- package/dist/tools/filterFromContext.d.ts +1 -1
- package/dist/tools/filterFromContext.js.map +1 -1
- package/dist/tools/get.d.ts +2 -2
- package/dist/tools/get.js +4 -1
- package/dist/tools/get.js.map +1 -1
- package/dist/tools/isFilterGroup.d.ts +1 -1
- package/dist/tools/normalizeOrder.d.ts +1 -1
- package/dist/tools/normalizeOrder.js.map +1 -1
- package/dist/tools/queryItemList.d.ts +1 -1
- package/dist/tools/reverseOrder.d.ts +1 -1
- package/dist/tools/reverseOrder.js.map +1 -1
- package/dist/tools/sortItemList.d.ts +1 -1
- package/dist/tools/sortItemList.js.map +1 -1
- package/dist/tools/testFilter.d.ts +1 -1
- package/dist/tools/testFilter.js +8 -8
- package/dist/tools/testFilter.js.map +1 -1
- package/dist/types/AggregateFunction.d.ts +3 -2
- package/dist/types/CombineUnion.d.ts +9 -0
- package/dist/types/CombineUnion.js +1 -0
- package/dist/types/CombineUnion.js.map +1 -0
- package/dist/types/Context.d.ts +1 -1
- package/dist/types/CustomFetch.d.ts +1 -1
- package/dist/types/CustomFieldAggregateMap.d.ts +1 -1
- package/dist/types/CustomFieldMap.d.ts +1 -1
- package/dist/types/Field.d.ts +4 -2
- package/dist/types/FieldFiltered.d.ts +4 -2
- package/dist/types/FieldFunction.d.ts +1 -1
- package/dist/types/FieldFunctionCustom.d.ts +1 -1
- package/dist/types/FieldFunctionFormatDate.d.ts +1 -1
- package/dist/types/Filter.d.ts +1 -1
- package/dist/types/FilterArray.d.ts +1 -2
- package/dist/types/FilterBoolean.d.ts +1 -1
- package/dist/types/FilterChildren.d.ts +3 -3
- package/dist/types/FilterCustom.d.ts +1 -1
- package/dist/types/FilterField.d.ts +1 -1
- package/dist/types/FilterGroup.d.ts +1 -1
- package/dist/types/FilterNumber.d.ts +1 -1
- package/dist/types/FilterString.d.ts +1 -1
- package/dist/types/FilterStringIntersect.d.ts +1 -1
- package/dist/types/FilterStringMatch.d.ts +3 -3
- package/dist/types/Get.d.ts +1 -1
- package/dist/types/Group.d.ts +1 -1
- package/dist/types/Immutable.d.ts +1 -1
- package/dist/types/InjectCustomFields.d.ts +1 -1
- package/dist/types/KeyFiltered.d.ts +10 -4
- package/dist/types/KeyFromUnion.d.ts +1 -0
- package/dist/types/KeyFromUnion.js +1 -0
- package/dist/types/KeyFromUnion.js.map +1 -0
- package/dist/types/Order.d.ts +1 -1
- package/dist/types/Path.d.ts +4 -2
- package/dist/types/PathFiltered.d.ts +3 -2
- package/dist/types/Primitive.d.ts +1 -0
- package/dist/types/Primitive.js +1 -0
- package/dist/types/Primitive.js.map +1 -0
- package/dist/types/Query.d.ts +2 -2
- package/dist/types/QueryAggregate.d.ts +1 -1
- package/dist/types/QueryCreate.d.ts +1 -1
- package/dist/types/QueryCreateMultiple.d.ts +1 -1
- package/dist/types/QueryDelete.d.ts +1 -1
- package/dist/types/QueryDeleteMultiple.d.ts +1 -1
- package/dist/types/QueryRead.d.ts +1 -1
- package/dist/types/QueryReadMultiple.d.ts +1 -1
- package/dist/types/QuerySettings.d.ts +1 -1
- package/dist/types/QueryUpdate.d.ts +1 -1
- package/dist/types/QueryUpdateMultiple.d.ts +1 -1
- package/dist/types/Result.d.ts +7 -7
- package/dist/types/Value.d.ts +1 -1
- package/dist/types.d.ts +3 -3
- package/lib/constants/CACHE.ts +1 -0
- package/lib/constants/SELF.ts +1 -0
- package/lib/constants.ts +2 -1
- package/lib/middlewares/fetchLocal.ts +1 -1
- package/lib/tools/defineCheckQuery.ts +1 -1
- package/lib/tools/defineCustomFetch.ts +1 -1
- package/lib/tools/defineGenericFetch.ts +1 -1
- package/lib/tools/fieldListFromFilter.ts +1 -1
- package/lib/tools/filterChildren.ts +1 -1
- package/lib/tools/filterFromContext.test.ts +10 -0
- package/lib/tools/filterFromContext.ts +2 -4
- package/lib/tools/get.test.ts +1 -0
- package/lib/tools/get.ts +16 -10
- package/lib/tools/isFilterGroup.ts +1 -1
- package/lib/tools/normalizeOrder.ts +1 -3
- package/lib/tools/queryItemList.test.ts +2 -2
- package/lib/tools/queryItemList.ts +3 -3
- package/lib/tools/reverseOrder.ts +1 -3
- package/lib/tools/sortItemList.ts +3 -3
- package/lib/tools/testFilter.test.ts +3 -3
- package/lib/tools/testFilter.ts +14 -17
- package/lib/types/AggregateFunction.ts +3 -2
- package/lib/types/CombineUnion.ts +18 -0
- package/lib/types/Context.ts +1 -1
- package/lib/types/CustomFetch.ts +1 -1
- package/lib/types/CustomFieldAggregateMap.ts +1 -4
- package/lib/types/CustomFieldMap.ts +1 -1
- package/lib/types/Field.ts +4 -4
- package/lib/types/FieldFiltered.ts +4 -4
- package/lib/types/FieldFunction.ts +1 -1
- package/lib/types/FieldFunctionCustom.ts +1 -1
- package/lib/types/FieldFunctionFormatDate.ts +1 -1
- package/lib/types/Filter.ts +1 -1
- package/lib/types/FilterArray.ts +1 -2
- package/lib/types/FilterBoolean.ts +1 -1
- package/lib/types/FilterChildren.ts +3 -3
- package/lib/types/FilterCustom.ts +1 -1
- package/lib/types/FilterField.ts +1 -1
- package/lib/types/FilterGroup.ts +1 -1
- package/lib/types/FilterNumber.ts +1 -1
- package/lib/types/FilterString.ts +1 -1
- package/lib/types/FilterStringIntersect.ts +1 -1
- package/lib/types/FilterStringMatch.ts +3 -3
- package/lib/types/Get.ts +1 -1
- package/lib/types/Group.ts +1 -1
- package/lib/types/Immutable.ts +1 -1
- package/lib/types/InjectCustomFields.ts +1 -4
- package/lib/types/KeyFiltered.ts +29 -5
- package/lib/types/KeyFromUnion.ts +1 -0
- package/lib/types/Order.ts +1 -1
- package/lib/types/Path.ts +15 -9
- package/lib/types/PathFiltered.ts +16 -13
- package/lib/types/Primitive.ts +1 -0
- package/lib/types/Query.ts +2 -2
- package/lib/types/QueryAggregate.ts +1 -1
- package/lib/types/QueryCreate.ts +1 -1
- package/lib/types/QueryCreateMultiple.ts +1 -1
- package/lib/types/QueryDelete.ts +1 -1
- package/lib/types/QueryDeleteMultiple.ts +1 -1
- package/lib/types/QueryRead.ts +1 -1
- package/lib/types/QueryReadMultiple.ts +1 -1
- package/lib/types/QuerySettings.ts +1 -1
- package/lib/types/QueryUpdate.ts +1 -1
- package/lib/types/QueryUpdateMultiple.ts +1 -1
- package/lib/types/Result.ts +5 -8
- package/lib/types/Value.ts +1 -1
- package/lib/types.ts +3 -3
- package/package.json +6 -5
- package/dist/constants/SymbolCache.d.ts +0 -1
- package/dist/constants/SymbolCache.js +0 -2
- package/dist/constants/SymbolCache.js.map +0 -1
- package/dist/types/Any.d.ts +0 -1
- package/dist/types/Any.js +0 -1
- package/dist/types/Any.js.map +0 -1
- package/dist/types/Mutable.d.ts +0 -3
- package/dist/types/Mutable.js +0 -1
- package/dist/types/Mutable.js.map +0 -1
- package/dist/types/Normalized.d.ts +0 -2
- package/dist/types/Normalized.js +0 -1
- package/dist/types/Normalized.js.map +0 -1
- package/lib/constants/SymbolCache.ts +0 -1
- package/lib/types/Any.ts +0 -10
- package/lib/types/Mutable.ts +0 -3
- 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":""}
|
package/dist/types/Order.d.ts
CHANGED
package/dist/types/Path.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { Increment } from "./Increment";
|
|
2
|
-
|
|
3
|
-
|
|
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
|
-
|
|
3
|
-
|
|
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":""}
|
package/dist/types/Query.d.ts
CHANGED
|
@@ -12,6 +12,6 @@ import type { QueryUpdateMultiple } from "./QueryUpdateMultiple";
|
|
|
12
12
|
*/
|
|
13
13
|
export type Query<
|
|
14
14
|
/**
|
|
15
|
-
* Type of the
|
|
15
|
+
* Type of the item to be queried.
|
|
16
16
|
*/
|
|
17
|
-
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
|
|
10
|
+
export type QueryAggregate<T> = {
|
|
11
11
|
method: "aggregate";
|
|
12
12
|
/**
|
|
13
13
|
* Common item properties to use for identifying the item.
|
package/dist/types/Result.d.ts
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
17
|
-
|
|
18
|
-
|
|
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";
|
package/dist/types/Value.d.ts
CHANGED
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 {
|
|
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
|
@@ -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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
14
|
+
function criteria<T>(
|
|
17
15
|
context: object,
|
|
18
16
|
path: readonly string[] = EMPTY_ARRAY,
|
|
19
17
|
): readonly Filter<T>[] {
|
package/lib/tools/get.test.ts
CHANGED
package/lib/tools/get.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
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
|
|
14
|
-
|
|
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
|
|
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
|
|
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 }]
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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 {
|
|
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<
|
|
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[
|
|
312
|
+
expect(filterChildren[CACHE]).toBeDefined();
|
|
313
313
|
expect(testFilter({ operator: "children", value: "b" }, { id: "a/b" })).toBe(
|
|
314
314
|
false,
|
|
315
315
|
);
|
package/lib/tools/testFilter.ts
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CACHE } from "../constants/CACHE.js";
|
|
2
2
|
import type { QuerySettings } from "../types/QuerySettings.js";
|
|
3
|
-
import type {
|
|
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
|
|
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
|
|
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)
|
|
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[
|
|
107
|
+
if (filter[CACHE] === undefined) {
|
|
111
108
|
switch (true) {
|
|
112
109
|
case settings?.transformFilterChildren !== undefined:
|
|
113
|
-
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[
|
|
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[
|
|
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[
|
|
280
|
-
filter[
|
|
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[
|
|
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
|
|
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:
|
|
20
|
+
field: Field<T>;
|
|
20
21
|
};
|