core.js
9.57 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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createQueryTester = exports.createOperationTester = exports.createQueryOperation = exports.containsOperation = exports.numericalOperation = exports.numericalOperationCreator = exports.NopeOperation = exports.createEqualsOperation = exports.EqualsOperation = exports.createTester = exports.NestedOperation = exports.QueryOperation = exports.NamedGroupOperation = exports.NamedBaseOperation = void 0;
const utils_1 = require("./utils");
/**
* Walks through each value given the context - used for nested operations. E.g:
* { "person.address": { $eq: "blarg" }}
*/
const walkKeyPathValues = (item, keyPath, next, depth, key, owner) => {
const currentKey = keyPath[depth];
// if array, then try matching. Might fall through for cases like:
// { $eq: [1, 2, 3] }, [ 1, 2, 3 ].
if (utils_1.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);
}
return walkKeyPathValues(item[currentKey], keyPath, next, depth + 1, currentKey, item);
};
class BaseOperation {
constructor(params, owneryQuery, options) {
this.params = params;
this.owneryQuery = owneryQuery;
this.options = options;
this.init();
}
init() { }
reset() {
this.done = false;
this.keep = false;
}
}
class NamedBaseOperation extends BaseOperation {
constructor(params, owneryQuery, options, name) {
super(params, owneryQuery, options);
this.name = name;
}
}
exports.NamedBaseOperation = NamedBaseOperation;
class GroupOperation extends BaseOperation {
constructor(params, owneryQuery, options, children) {
super(params, owneryQuery, options);
this.children = children;
}
/**
*/
reset() {
this.keep = false;
this.done = false;
for (let i = 0, { length } = this.children; i < length; i++) {
this.children[i].reset();
}
}
/**
*/
childrenNext(item, key, owner) {
let done = true;
let keep = true;
for (let i = 0, { length } = this.children; i < length; i++) {
const childOperation = this.children[i];
childOperation.next(item, key, owner);
if (!childOperation.keep) {
keep = false;
}
if (childOperation.done) {
if (!childOperation.keep) {
break;
}
}
else {
done = false;
}
}
this.done = done;
this.keep = keep;
}
}
class NamedGroupOperation extends GroupOperation {
constructor(params, owneryQuery, options, children, name) {
super(params, owneryQuery, options, children);
this.name = name;
}
}
exports.NamedGroupOperation = NamedGroupOperation;
class QueryOperation extends GroupOperation {
constructor() {
super(...arguments);
this.propop = true;
}
/**
*/
next(item, key, parent) {
this.childrenNext(item, key, parent);
}
}
exports.QueryOperation = QueryOperation;
class NestedOperation extends GroupOperation {
constructor(keyPath, params, owneryQuery, options, children) {
super(params, owneryQuery, options, children);
this.keyPath = keyPath;
this.propop = true;
/**
*/
this._nextNestedValue = (value, key, owner) => {
this.childrenNext(value, key, owner);
return !this.done;
};
}
/**
*/
next(item, key, parent) {
walkKeyPathValues(item, this.keyPath, this._nextNestedValue, 0, key, parent);
}
}
exports.NestedOperation = NestedOperation;
const createTester = (a, compare) => {
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 = utils_1.comparable(a);
return b => compare(comparableA, utils_1.comparable(b));
};
exports.createTester = createTester;
class EqualsOperation extends BaseOperation {
constructor() {
super(...arguments);
this.propop = true;
}
init() {
this._test = exports.createTester(this.params, this.options.compare);
}
next(item, key, parent) {
if (!Array.isArray(parent) || parent.hasOwnProperty(key)) {
if (this._test(item, key, parent)) {
this.done = true;
this.keep = true;
}
}
}
}
exports.EqualsOperation = EqualsOperation;
const createEqualsOperation = (params, owneryQuery, options) => new EqualsOperation(params, owneryQuery, options);
exports.createEqualsOperation = createEqualsOperation;
class NopeOperation extends BaseOperation {
constructor() {
super(...arguments);
this.propop = true;
}
next() {
this.done = true;
this.keep = false;
}
}
exports.NopeOperation = NopeOperation;
const numericalOperationCreator = (createNumericalOperation) => (params, owneryQuery, options, name) => {
if (params == null) {
return new NopeOperation(params, owneryQuery, options);
}
return createNumericalOperation(params, owneryQuery, options, name);
};
exports.numericalOperationCreator = numericalOperationCreator;
const numericalOperation = (createTester) => exports.numericalOperationCreator((params, owneryQuery, options) => {
const typeofParams = typeof utils_1.comparable(params);
const test = createTester(params);
return new EqualsOperation(b => {
return typeof utils_1.comparable(b) === typeofParams && test(b);
}, owneryQuery, options);
});
exports.numericalOperation = numericalOperation;
const createNamedOperation = (name, params, parentQuery, options) => {
const operationCreator = options.operations[name];
if (!operationCreator) {
throwUnsupportedOperation(name);
}
return operationCreator(params, parentQuery, options, name);
};
const throwUnsupportedOperation = (name) => {
throw new Error(`Unsupported operation: ${name}`);
};
const containsOperation = (query, options) => {
for (const key in query) {
if (options.operations.hasOwnProperty(key) || key.charAt(0) === "$")
return true;
}
return false;
};
exports.containsOperation = containsOperation;
const createNestedOperation = (keyPath, nestedQuery, parentKey, owneryQuery, options) => {
if (exports.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)
]);
};
const createQueryOperation = (query, owneryQuery = null, { compare, operations } = {}) => {
const options = {
compare: compare || utils_1.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);
};
exports.createQueryOperation = createQueryOperation;
const createQueryOperations = (query, parentKey, options) => {
const selfOperations = [];
const nestedOperations = [];
if (!utils_1.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];
};
const createOperationTester = (operation) => (item, key, owner) => {
operation.reset();
operation.next(item, key, owner);
return operation.keep;
};
exports.createOperationTester = createOperationTester;
const createQueryTester = (query, options = {}) => {
return exports.createOperationTester(exports.createQueryOperation(query, null, options));
};
exports.createQueryTester = createQueryTester;
//# sourceMappingURL=core.js.map