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.
- package/dist/constants/SymbolSelf.d.ts +1 -0
- package/dist/constants/SymbolSelf.js +2 -0
- package/dist/constants/SymbolSelf.js.map +1 -0
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +1 -0
- 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 +11 -1
- package/dist/tools/filterFromContext.js.map +1 -1
- package/dist/tools/get.d.ts +2 -2
- package/dist/tools/get.js +2 -0
- 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.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 +1 -1
- 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 +1 -1
- 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/SymbolSelf.ts +1 -0
- package/lib/constants.ts +1 -0
- 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 +30 -0
- package/lib/tools/filterFromContext.ts +13 -5
- package/lib/tools/get.test.ts +1 -0
- package/lib/tools/get.ts +14 -9
- 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 +1 -1
- package/lib/tools/testFilter.ts +6 -9
- 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 +1 -1
- 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 +1 -1
- 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/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/types/Any.ts +0 -10
- package/lib/types/Mutable.ts +0 -3
- 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
|
|
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 SymbolSelf = 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,
|
|
@@ -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
|
|
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>[] {
|
|
@@ -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
|
package/lib/tools/get.test.ts
CHANGED
package/lib/tools/get.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
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
|
|
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
|
|
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
|
}
|
package/lib/tools/testFilter.ts
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
import { SymbolCache } from "../constants/SymbolCache.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
|
}
|
|
@@ -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
|
};
|
|
@@ -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;
|
package/lib/types/Context.ts
CHANGED
package/lib/types/CustomFetch.ts
CHANGED
package/lib/types/Field.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { CombineUnion } from "./CombineUnion";
|
|
2
2
|
import type { Path } from "./Path";
|
|
3
3
|
|
|
4
|
-
export type Field<T> = T
|
|
5
|
-
|
|
6
|
-
|
|
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
|
|
6
|
-
|
|
7
|
-
|
|
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
|
|
4
|
+
export type FieldFunction<T> =
|
|
5
5
|
| FieldFunctionCustom<T>
|
|
6
6
|
| FieldFunctionFormatDate<T>;
|