core.ts
10.9 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import {
isArray,
Key,
Comparator,
isVanillaObject,
comparable,
equals
} from "./utils";
export interface Operation<TItem> {
readonly keep: boolean;
readonly done: boolean;
propop: boolean;
reset();
next(item: TItem, key?: Key, owner?: any, root?: boolean);
}
export type Tester = (
item: any,
key?: Key,
owner?: any,
root?: boolean
) => boolean;
export interface NamedOperation {
name: string;
}
export type OperationCreator<TItem> = (
params: any,
parentQuery: any,
options: Options,
name: string
) => Operation<TItem>;
type BasicValueQuery<TValue> = {
$eq?: TValue;
$ne?: TValue;
$lt?: TValue;
$gt?: TValue;
$lte?: TValue;
$gte?: TValue;
$in?: TValue[];
$nin?: TValue[];
$all?: TValue[];
$mod?: [number, number];
$exists?: boolean;
$regex?: string | RegExp;
$size?: number;
$where?: ((this: TValue, obj: TValue) => boolean) | string;
$options?: "i" | "g" | "m" | "u";
$type?: Function;
$not?: NestedQuery<TValue>;
$or?: NestedQuery<TValue>[];
$nor?: NestedQuery<TValue>[];
$and?: NestedQuery<TValue>[];
};
type ArrayValueQuery<TValue> = {
$elemMatch?: Query<TValue>;
} & BasicValueQuery<TValue>;
type Unpacked<T> = T extends (infer U)[] ? U : T;
type ValueQuery<TValue> = TValue extends Array<any>
? ArrayValueQuery<Unpacked<TValue>>
: BasicValueQuery<TValue>;
type NotObject = string | number | Date | boolean | Array<any>;
type ShapeQuery<TItemSchema> = TItemSchema extends NotObject
? {}
: { [k in keyof TItemSchema]?: TItemSchema[k] | ValueQuery<TItemSchema[k]> };
type NestedQuery<TItemSchema> = ValueQuery<TItemSchema> &
ShapeQuery<TItemSchema>;
export type Query<TItemSchema> =
| TItemSchema
| RegExp
| NestedQuery<TItemSchema>;
/**
* Walks through each value given the context - used for nested operations. E.g:
* { "person.address": { $eq: "blarg" }}
*/
const walkKeyPathValues = (
item: any,
keyPath: Key[],
next: Tester,
depth: number,
key: Key,
owner: any
) => {
const currentKey = keyPath[depth];
// if array, then try matching. Might fall through for cases like:
// { $eq: [1, 2, 3] }, [ 1, 2, 3 ].
if (isArray(item) && isNaN(Number(currentKey))) {
for (let i = 0, { length } = item; i < length; i++) {
// if FALSE is returned, then terminate walker. For operations, this simply
// means that the search critera was met.
if (!walkKeyPathValues(item[i], keyPath, next, depth, i, item)) {
return false;
}
}
}
if (depth === keyPath.length || item == null) {
return next(item, key, owner, depth === 0);
}
return walkKeyPathValues(
item[currentKey],
keyPath,
next,
depth + 1,
currentKey,
item
);
};
export abstract class BaseOperation<TParams, TItem = any>
implements Operation<TItem> {
keep: boolean;
done: boolean;
abstract propop: boolean;
constructor(
readonly params: TParams,
readonly owneryQuery: any,
readonly options: Options,
readonly name?: string
) {
this.init();
}
protected init() {}
reset() {
this.done = false;
this.keep = false;
}
abstract next(item: any, key: Key, parent: any, root: boolean);
}
abstract class GroupOperation extends BaseOperation<any> {
keep: boolean;
done: boolean;
constructor(
params: any,
owneryQuery: any,
options: Options,
public readonly children: Operation<any>[]
) {
super(params, owneryQuery, options);
}
/**
*/
reset() {
this.keep = false;
this.done = false;
for (let i = 0, { length } = this.children; i < length; i++) {
this.children[i].reset();
}
}
abstract next(item: any, key: Key, owner: any, root: boolean);
/**
*/
protected childrenNext(item: any, key: Key, owner: any, root: boolean) {
let done = true;
let keep = true;
for (let i = 0, { length } = this.children; i < length; i++) {
const childOperation = this.children[i];
if (!childOperation.done) {
childOperation.next(item, key, owner, root);
}
if (!childOperation.keep) {
keep = false;
}
if (childOperation.done) {
if (!childOperation.keep) {
break;
}
} else {
done = false;
}
}
this.done = done;
this.keep = keep;
}
}
export abstract class NamedGroupOperation extends GroupOperation
implements NamedOperation {
abstract propop: boolean;
constructor(
params: any,
owneryQuery: any,
options: Options,
children: Operation<any>[],
readonly name: string
) {
super(params, owneryQuery, options, children);
}
}
export class QueryOperation<TItem> extends GroupOperation {
readonly propop = true;
/**
*/
next(item: TItem, key: Key, parent: any, root: boolean) {
this.childrenNext(item, key, parent, root);
}
}
export class NestedOperation extends GroupOperation {
readonly propop = true;
constructor(
readonly keyPath: Key[],
params: any,
owneryQuery: any,
options: Options,
children: Operation<any>[]
) {
super(params, owneryQuery, options, children);
}
/**
*/
next(item: any, key: Key, parent: any) {
walkKeyPathValues(
item,
this.keyPath,
this._nextNestedValue,
0,
key,
parent
);
}
/**
*/
private _nextNestedValue = (
value: any,
key: Key,
owner: any,
root: boolean
) => {
this.childrenNext(value, key, owner, root);
return !this.done;
};
}
export const createTester = (a, compare: Comparator) => {
if (a instanceof Function) {
return a;
}
if (a instanceof RegExp) {
return b => {
const result = typeof b === "string" && a.test(b);
a.lastIndex = 0;
return result;
};
}
const comparableA = comparable(a);
return b => compare(comparableA, comparable(b));
};
export class EqualsOperation<TParam> extends BaseOperation<TParam> {
readonly propop = true;
private _test: Tester;
init() {
this._test = createTester(this.params, this.options.compare);
}
next(item, key: Key, parent: any) {
if (!Array.isArray(parent) || parent.hasOwnProperty(key)) {
if (this._test(item, key, parent)) {
this.done = true;
this.keep = true;
}
}
}
}
export const createEqualsOperation = (
params: any,
owneryQuery: any,
options: Options
) => new EqualsOperation(params, owneryQuery, options);
export class NopeOperation<TParam> extends BaseOperation<TParam> {
readonly propop = true;
next() {
this.done = true;
this.keep = false;
}
}
export const numericalOperationCreator = (
createNumericalOperation: OperationCreator<any>
) => (params: any, owneryQuery: any, options: Options, name: string) => {
if (params == null) {
return new NopeOperation(params, owneryQuery, options, name);
}
return createNumericalOperation(params, owneryQuery, options, name);
};
export const numericalOperation = (createTester: (any) => Tester) =>
numericalOperationCreator(
(params: any, owneryQuery: Query<any>, options: Options, name: string) => {
const typeofParams = typeof comparable(params);
const test = createTester(params);
return new EqualsOperation(
b => {
return typeof comparable(b) === typeofParams && test(b);
},
owneryQuery,
options,
name
);
}
);
export type Options = {
operations: {
[identifier: string]: OperationCreator<any>;
};
compare: (a, b) => boolean;
};
const createNamedOperation = (
name: string,
params: any,
parentQuery: any,
options: Options
) => {
const operationCreator = options.operations[name];
if (!operationCreator) {
throwUnsupportedOperation(name);
}
return operationCreator(params, parentQuery, options, name);
};
const throwUnsupportedOperation = (name: string) => {
throw new Error(`Unsupported operation: ${name}`);
};
export const containsOperation = (query: any, options: Options) => {
for (const key in query) {
if (options.operations.hasOwnProperty(key) || key.charAt(0) === "$")
return true;
}
return false;
};
const createNestedOperation = (
keyPath: Key[],
nestedQuery: any,
parentKey: string,
owneryQuery: any,
options: Options
) => {
if (containsOperation(nestedQuery, options)) {
const [selfOperations, nestedOperations] = createQueryOperations(
nestedQuery,
parentKey,
options
);
if (nestedOperations.length) {
throw new Error(
`Property queries must contain only operations, or exact objects.`
);
}
return new NestedOperation(
keyPath,
nestedQuery,
owneryQuery,
options,
selfOperations
);
}
return new NestedOperation(keyPath, nestedQuery, owneryQuery, options, [
new EqualsOperation(nestedQuery, owneryQuery, options)
]);
};
export const createQueryOperation = <TItem, TSchema = TItem>(
query: Query<TSchema>,
owneryQuery: any = null,
{ compare, operations }: Partial<Options> = {}
): QueryOperation<TItem> => {
const options = {
compare: compare || equals,
operations: Object.assign({}, operations || {})
};
const [selfOperations, nestedOperations] = createQueryOperations(
query,
null,
options
);
const ops = [];
if (selfOperations.length) {
ops.push(
new NestedOperation([], query, owneryQuery, options, selfOperations)
);
}
ops.push(...nestedOperations);
if (ops.length === 1) {
return ops[0];
}
return new QueryOperation(query, owneryQuery, options, ops);
};
const createQueryOperations = (
query: any,
parentKey: string,
options: Options
) => {
const selfOperations = [];
const nestedOperations = [];
if (!isVanillaObject(query)) {
selfOperations.push(new EqualsOperation(query, query, options));
return [selfOperations, nestedOperations];
}
for (const key in query) {
if (options.operations.hasOwnProperty(key)) {
const op = createNamedOperation(key, query[key], query, options);
if (op) {
if (!op.propop && parentKey && !options.operations[parentKey]) {
throw new Error(
`Malformed query. ${key} cannot be matched against property.`
);
}
}
// probably just a flag for another operation (like $options)
if (op != null) {
selfOperations.push(op);
}
} else if (key.charAt(0) === "$") {
throwUnsupportedOperation(key);
} else {
nestedOperations.push(
createNestedOperation(key.split("."), query[key], key, query, options)
);
}
}
return [selfOperations, nestedOperations];
};
export const createOperationTester = <TItem>(operation: Operation<TItem>) => (
item: TItem,
key?: Key,
owner?: any
) => {
operation.reset();
operation.next(item, key, owner);
return operation.keep;
};
export const createQueryTester = <TItem, TSchema = TItem>(
query: Query<TSchema>,
options: Partial<Options> = {}
) => {
return createOperationTester(
createQueryOperation<TItem, TSchema>(query, null, options)
);
};