/
api.ts
251 lines (233 loc) · 5.52 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import type {
Fn,
Fn6,
FnU,
Keys,
NumOrString,
Predicate,
Predicate2,
} from "@thi.ng/api";
export type FTerm = Predicate<any>;
export type OTerm = any | null;
export type SPTerm = Predicate<string> | NumOrString | null;
export type SPInputTerm = SPTerm | NumOrString[] | Set<NumOrString>;
export type QueryObj = Record<string, any>;
/**
* All 27 possible query types.
*
* @remarks
* - l => literal
* - n => null / wildcard
* - f => function / predicate
*
* @internal
*/
export type QueryType =
| "lll"
| "llf"
| "lln"
| "lfl"
| "lff"
| "lfn"
| "lnl"
| "lnf"
| "lnn"
| "fll"
| "flf"
| "fln"
| "ffl"
| "fff"
| "ffn"
| "fnl"
| "fnf"
| "fnn"
| "nll"
| "nlf"
| "nln"
| "nfl"
| "nff"
| "nfn"
| "nnl"
| "nnf"
| "nnn";
/**
* @internal
*/
export type QueryImpl = Fn6<
QueryObj,
QueryObj,
SPTerm,
SPTerm,
OTerm,
QueryOpts,
void
>;
/**
* @internal
*/
export type QueryImpls = Record<QueryType, QueryImpl>;
/**
* Takes an object of this structure `{ s1: { p1: o, p2: ... }, s2: { p1: o
* }...}`, matches all entries using provided `s`(ubject), `p`(redicate) and
* `o`(object) terms. Returns new object of matched results (format depends
* on query config given to {@link defQuery}).
*
* @remarks
* If `res` is provided, results will be injected in that object. Otherwise
* a new result object will be created.
*/
export type ObjQueryFn<T extends QueryObj = QueryObj> = (
obj: T,
s: SPInputTerm,
p: SPInputTerm,
o: OTerm,
res?: QueryObj
) => QueryObj;
/**
* Takes a source array of objects with this structure: [{p1: o, p2: ...},
* ...]`, and matches each item using provided `p`(redicate) and `o`bject terms.
* Returns new array of matched results (result object format depends on query
* config given to {@link defQuery}).
* @remarks
* If `res` is provided, results will be appended to that array. Otherwise a new
* result array will be created.
*/
export type ArrayQueryFn<T extends QueryObj[]> = (
src: T,
p: SPInputTerm,
o: OTerm,
res?: T
) => T;
/**
* Similar to {@link ObjQueryFn}, but only collects and returns a set of
* matching `s` keys.
*/
export type ObjKeyQueryFn<T extends QueryObj> = (
obj: T,
s: SPInputTerm,
p: SPInputTerm,
o: OTerm,
res?: Set<string>
) => Set<string>;
/**
* Similar to {@link ArrayQueryFn}, but only collects and returns a set of
* indices of matching objects.
*/
export type ArrayKeyQueryFn<T extends QueryObj[]> = (
src: T,
p: SPInputTerm,
o: OTerm,
res?: Set<number>
) => Set<number>;
/**
* Conditional return type for {@link defQuery}.
*/
export type QueryFn<T extends QueryObj | QueryObj[]> = T extends QueryObj[]
? ArrayQueryFn<T>
: ObjQueryFn<T>;
/**
* Conditional return type for {@link defKeyQuery}.
*/
export type KeyQueryFn<T extends QueryObj | QueryObj[]> = T extends QueryObj[]
? ArrayKeyQueryFn<T>
: ObjKeyQueryFn<T>;
/**
* Query behavior options.
*/
export interface QueryOpts {
/**
* If false (default), an entire object is included in the solution as soon
* as any of its P(redicate)-O(bject) terms matches. If true, only the
* successfully matched property values will be included for each result.
*
* @example
* ```ts
* import { defQuery } from "@thi.ng/oquery";
*
* const DB = { a: { id: 1, name: "alice" }, b: { name: "bob" } };
*
* defQuery({ partial: false })(DB, null, "id", 1)
* // { a: { id: 1, name: "alice" } }
*
* defQuery({ partial: true })(DB, null, "id", 1)
* // { a: { id: 1 } }
* ```
*
* @defaultValue false
*/
partial: boolean;
/**
* If true (default), any array or Set values in the target object's
* O(bject) position will be matched componentwise rather than matched as
* array value themselves.
*
* @remarks
* Array or Set terms in S(ubject) or P(redicate) position are of course
* ALWAYS matched in a componentwise manner.
*
* @example
* ```ts
* import { defQuery } from "@thi.ng/oquery";
*
* const DB = { a: { knows: ["b","c"] }, b: { knows: ["a","c"] }};
* defQuery({ cwise: true })(DB, null, "knows", "b")
* // { a: { knows: ["b","c"] } }
*
* defQuery({ cwise: false })(DB, null, "knows", (x) => x.includes("b"))
* // { a: { knows: ["b","c"] } }
* ```
*
* @defaultValue true
*/
cwise: boolean;
/**
* Only used if `cwise` is enabled. If true, ALL of the query elements must
* match (aka intersection query). If false (default), an array or Set
* query term in O(bject) position will succeed if at least ONE of its
* elements is matched (aka union query). .
*
* @defaultValue false
*/
intersect: boolean;
/**
* Equality predicate applied for matching literals in O(bject) position.
*
* @defaultValue thi.ng/equiv#equiv
*/
equiv: Predicate2<any>;
}
/**
* Subset of {@link QueryOpts} applicable to {@link defKeyQuery}.
*/
export interface KeyQueryOpts
extends Pick<QueryOpts, "cwise" | "intersect" | "equiv"> {}
export interface QueryTerm<T extends QueryObj = QueryObj> {
/**
* Actual query expressed as tuple of `[key, value]` tuple. Predicate
* functions can be used in either position.
*/
q: [Keys<T> | Predicate<string> | null, any];
/**
* Optional function to post-process query results.
*/
post?: FnU<T[]>;
/**
* Query options for this term.
*/
opts?: Partial<QueryOpts>;
}
export interface MultiQueryOpts<T extends QueryObj = QueryObj> {
/**
* Max number of query results. Default: unlimited
*/
limit: number;
/**
* If given, results are sorted by this key.
*/
sort: Keys<T> | Fn<T, any>;
/**
* Only used if {@link MultiQueryOpts.sort} is given. If true, reverses sort
* order.
*/
reverse: boolean;
}