oak-domain 2.0.1 → 2.0.2
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/lib/store/CascadeStore.js +2 -2
- package/lib/store/filter.d.ts +25 -0
- package/lib/store/filter.js +314 -137
- package/package.json +3 -2
|
@@ -1379,10 +1379,10 @@ var CascadeStore = /** @class */ (function (_super) {
|
|
|
1379
1379
|
var data = {};
|
|
1380
1380
|
var rel = this_3.judgeRelation(entity, attr);
|
|
1381
1381
|
if (rel === 2) {
|
|
1382
|
-
this_3.addToResultSelections(attr, rows.map(function (ele) { return ele[attr]; }), context);
|
|
1382
|
+
this_3.addToResultSelections(attr, rows.map(function (ele) { return ele[attr]; }).filter(function (ele) { return !!ele; }), context);
|
|
1383
1383
|
}
|
|
1384
1384
|
else if (typeof rel === 'string') {
|
|
1385
|
-
this_3.addToResultSelections(rel, rows.map(function (ele) { return ele[attr]; }), context);
|
|
1385
|
+
this_3.addToResultSelections(rel, rows.map(function (ele) { return ele[attr]; }).filter(function (ele) { return !!ele; }), context);
|
|
1386
1386
|
}
|
|
1387
1387
|
else if (rel instanceof Array) {
|
|
1388
1388
|
this_3.addToResultSelections(rel[0], rows.map(function (ele) { return ele[attr]; }).reduce(function (prev, current) { return prev.concat(current); }, []), context);
|
package/lib/store/filter.d.ts
CHANGED
|
@@ -5,6 +5,31 @@ import { SyncContext } from './SyncRowStore';
|
|
|
5
5
|
export declare function addFilterSegment<ED extends EntityDict, T extends keyof ED>(...filters: ED[T]['Selection']['filter'][]): ED[T]["Selection"]["filter"];
|
|
6
6
|
export declare function unionFilterSegment<ED extends EntityDict, T extends keyof ED>(...filters: ED[T]['Selection']['filter'][]): ED[T]["Selection"]["filter"];
|
|
7
7
|
export declare function combineFilters<ED extends EntityDict, T extends keyof ED>(filters: Array<ED[T]['Selection']['filter']>, union?: true): ED[T]["Selection"]["filter"];
|
|
8
|
+
/**
|
|
9
|
+
* 判断value1表达的单个属性查询与同属性上value2表达的查询是包容还是相斥
|
|
10
|
+
* 相容即value1所表达的查询结果一定被value2表达的查询结果所包含,例如:
|
|
11
|
+
* value1: {
|
|
12
|
+
* $eq: 12
|
|
13
|
+
* }
|
|
14
|
+
* value2: {
|
|
15
|
+
* $gt: 8,
|
|
16
|
+
* }
|
|
17
|
+
* 此时value1相容value2
|
|
18
|
+
*
|
|
19
|
+
* 相斥即value1所表达的查询结果与value2一定毫无联系,例如:
|
|
20
|
+
* value1: {
|
|
21
|
+
* $gt: 8,
|
|
22
|
+
* }
|
|
23
|
+
* value2: {
|
|
24
|
+
* $lt: 2,
|
|
25
|
+
* }
|
|
26
|
+
*
|
|
27
|
+
*
|
|
28
|
+
* @param value1
|
|
29
|
+
* @param value2
|
|
30
|
+
* @attention: 1)这里的测试不够充分,有些算子之间的相容或相斥可能有遗漏, 2)有新的算子加入需要修改代码
|
|
31
|
+
*/
|
|
32
|
+
export declare function judgeValueRelation(value1: any, value2: any, contained: boolean): boolean;
|
|
8
33
|
/**
|
|
9
34
|
*
|
|
10
35
|
* 判断filter是否包含conditionalFilter中的查询条件,即filter查询的结果一定满足conditionalFilter的约束
|
package/lib/store/filter.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.checkFilterContains = exports.makeTreeDescendantFilter = exports.makeTreeAncestorFilter = exports.same = exports.getRelevantIds = exports.repel = exports.contains = exports.combineFilters = exports.unionFilterSegment = exports.addFilterSegment = void 0;
|
|
3
|
+
exports.checkFilterContains = exports.makeTreeDescendantFilter = exports.makeTreeAncestorFilter = exports.same = exports.getRelevantIds = exports.repel = exports.contains = exports.judgeValueRelation = exports.combineFilters = exports.unionFilterSegment = exports.addFilterSegment = void 0;
|
|
4
4
|
var tslib_1 = require("tslib");
|
|
5
5
|
var assert_1 = tslib_1.__importDefault(require("assert"));
|
|
6
6
|
var types_1 = require("../types");
|
|
@@ -98,13 +98,13 @@ function combineFilters(filters, union) {
|
|
|
98
98
|
}
|
|
99
99
|
exports.combineFilters = combineFilters;
|
|
100
100
|
/**
|
|
101
|
-
* 判断value1表达的单个属性查询与同属性上value2
|
|
102
|
-
* 相容即value2
|
|
101
|
+
* 判断value1表达的单个属性查询与同属性上value2表达的查询是包容还是相斥
|
|
102
|
+
* 相容即value1所表达的查询结果一定被value2表达的查询结果所包含,例如:
|
|
103
103
|
* value1: {
|
|
104
|
-
* $
|
|
104
|
+
* $eq: 12
|
|
105
105
|
* }
|
|
106
106
|
* value2: {
|
|
107
|
-
* $
|
|
107
|
+
* $gt: 8,
|
|
108
108
|
* }
|
|
109
109
|
* 此时value1相容value2
|
|
110
110
|
*
|
|
@@ -115,119 +115,194 @@ exports.combineFilters = combineFilters;
|
|
|
115
115
|
* value2: {
|
|
116
116
|
* $lt: 2,
|
|
117
117
|
* }
|
|
118
|
+
*
|
|
119
|
+
*
|
|
118
120
|
* @param value1
|
|
119
121
|
* @param value2
|
|
122
|
+
* @attention: 1)这里的测试不够充分,有些算子之间的相容或相斥可能有遗漏, 2)有新的算子加入需要修改代码
|
|
120
123
|
*/
|
|
121
|
-
function
|
|
124
|
+
function judgeValueRelation(value1, value2, contained) {
|
|
122
125
|
if (typeof value1 === 'object') {
|
|
123
126
|
var attr = Object.keys(value1)[0];
|
|
124
|
-
if (['$gt', '$lt', '$gte', '$lte', '$eq', '$ne',
|
|
125
|
-
'$startsWith', '$endsWith', '$includes'].includes(attr)) {
|
|
127
|
+
if (['$gt', '$lt', '$gte', '$lte', '$eq', '$ne', '$startsWith', '$endsWith', '$includes'].includes(attr)) {
|
|
126
128
|
switch (attr) {
|
|
127
129
|
case '$gt': {
|
|
130
|
+
if (contained) {
|
|
131
|
+
// 包容
|
|
132
|
+
if (typeof value2 === 'object') {
|
|
133
|
+
var attr2 = Object.keys(value2)[0];
|
|
134
|
+
return ['$gt', '$gte'].includes(attr2) && value2[attr2] <= value1.$gt;
|
|
135
|
+
}
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
// 互斥
|
|
128
139
|
if (typeof value2 === 'object') {
|
|
129
140
|
var attr2 = Object.keys(value2)[0];
|
|
130
|
-
return
|
|
131
|
-
||
|
|
132
|
-
|| attr2 === '$in' && value2[attr2] instanceof Array && !((value2[attr2]).find(function (ele) { return typeof ele !== typeof value1.$gt || ele <= value1.$gt; }));
|
|
133
|
-
}
|
|
134
|
-
else {
|
|
135
|
-
return value2 > value1.$gt;
|
|
141
|
+
return ['$lt', '$lte', '$eq'].includes(attr2) && value2[attr2] <= value1.$gt
|
|
142
|
+
|| attr2 === '$in' && value2[attr2] instanceof Array && !((value2[attr2]).find(function (ele) { return typeof ele !== typeof value1.$gt || ele > value1.$gt; }));
|
|
136
143
|
}
|
|
144
|
+
return value2 <= value1.$gt;
|
|
137
145
|
}
|
|
138
146
|
case '$gte': {
|
|
147
|
+
if (contained) {
|
|
148
|
+
// 包容
|
|
149
|
+
if (typeof value2 === 'object') {
|
|
150
|
+
var attr2 = Object.keys(value2)[0];
|
|
151
|
+
return ['$gte'].includes(attr2) && value2[attr2] <= value1.$gte
|
|
152
|
+
|| ['$gt'].includes(attr2) && value2[attr2] < value1.$gte;
|
|
153
|
+
}
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
// 互斥
|
|
139
157
|
if (typeof value2 === 'object') {
|
|
140
158
|
var attr2 = Object.keys(value2)[0];
|
|
141
|
-
return
|
|
142
|
-
|| ['$
|
|
143
|
-
|| attr2 === '$in' && value2[attr2] instanceof Array && !((value2[attr2]).find(function (ele) { return typeof ele !== typeof value1.$gte || ele
|
|
144
|
-
}
|
|
145
|
-
else {
|
|
146
|
-
return value2 >= value1.$gt;
|
|
159
|
+
return ['$lt'].includes(attr2) && value2[attr2] <= value1.$gte
|
|
160
|
+
|| ['$eq', '$lte'].includes(attr2) && value2[attr2] < value1.gte
|
|
161
|
+
|| attr2 === '$in' && value2[attr2] instanceof Array && !((value2[attr2]).find(function (ele) { return typeof ele !== typeof value1.$gte || ele >= value1.$gte; }));
|
|
147
162
|
}
|
|
163
|
+
return value2 < value1.$gte;
|
|
148
164
|
}
|
|
149
165
|
case '$lt': {
|
|
166
|
+
if (contained) {
|
|
167
|
+
// 相容
|
|
168
|
+
if (typeof value2 === 'object') {
|
|
169
|
+
var attr2 = Object.keys(value2)[0];
|
|
170
|
+
return ['$lt', '$lte'].includes(attr2) && value2[attr2] >= value1.$lt;
|
|
171
|
+
}
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
// 互斥
|
|
150
175
|
if (typeof value2 === 'object') {
|
|
151
176
|
var attr2 = Object.keys(value2)[0];
|
|
152
|
-
return
|
|
153
|
-
||
|
|
154
|
-
|| attr2 === '$in' && value2[attr2] instanceof Array && !(value2[attr2]).find(function (ele) { return typeof ele !== typeof value1.$lt || ele >= value1.$lt; });
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
return value2 < value1.$lt;
|
|
177
|
+
return ['$gt', '$gte', '$eq'].includes(attr2) && value2[attr2] >= value1.$lt
|
|
178
|
+
|| attr2 === '$in' && value2[attr2] instanceof Array && !((value2[attr2]).find(function (ele) { return typeof ele !== typeof value1.$gt || ele < value1.$lt; }));
|
|
158
179
|
}
|
|
180
|
+
return value2 >= value1.$gt;
|
|
159
181
|
}
|
|
160
182
|
case '$lte': {
|
|
183
|
+
if (contained) {
|
|
184
|
+
// 包容
|
|
185
|
+
if (typeof value2 === 'object') {
|
|
186
|
+
var attr2 = Object.keys(value2)[0];
|
|
187
|
+
return ['$lte'].includes(attr2) && value2[attr2] >= value1.$lte
|
|
188
|
+
|| ['$lt'].includes(attr2) && value2[attr2] > value1.$lte;
|
|
189
|
+
}
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
// 互斥
|
|
161
193
|
if (typeof value2 === 'object') {
|
|
162
194
|
var attr2 = Object.keys(value2)[0];
|
|
163
|
-
return
|
|
164
|
-
|| ['$
|
|
165
|
-
|| attr2 === '$in' && value2[attr2] instanceof Array && !(value2[attr2].find(function (ele) { return typeof ele !== typeof value1.$lte || ele
|
|
166
|
-
}
|
|
167
|
-
else {
|
|
168
|
-
return value2 <= value1.$lte;
|
|
195
|
+
return ['$gt'].includes(attr2) && value2[attr2] >= value1.$lte
|
|
196
|
+
|| ['$eq', '$gte'].includes(attr2) && value2[attr2] > value1.lte
|
|
197
|
+
|| attr2 === '$in' && value2[attr2] instanceof Array && !((value2[attr2]).find(function (ele) { return typeof ele !== typeof value1.$lte || ele <= value1.$lte; }));
|
|
169
198
|
}
|
|
199
|
+
return value2 > value1.$gte;
|
|
170
200
|
}
|
|
171
201
|
case '$eq': {
|
|
172
|
-
if (
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
202
|
+
if (contained) {
|
|
203
|
+
// 相容
|
|
204
|
+
if (typeof value2 === 'object') {
|
|
205
|
+
var attr2 = Object.keys(value2)[0];
|
|
206
|
+
return attr2 === '$eq' && value2[attr2] === value1.$eq || attr2 === '$ne' && value2[attr2] !== value1.$eq
|
|
207
|
+
|| attr2 === '$gt' && value2[attr2] < value1.$eq || attr2 === '$lt' && value2[attr2] > value1.$eq
|
|
208
|
+
|| attr2 === '$gte' && value2[attr2] <= value1.$eq || attr2 === '$lte' && value2[attr2] >= value1.$eq
|
|
209
|
+
|| attr2 === '$startsWith' && value1.$eq.startsWith(value2[attr2])
|
|
210
|
+
|| attr2 === '$endsWith' && value1.$eq.endsWith(value2[attr2])
|
|
211
|
+
|| attr2 === '$includes' && value1.$eq.includes(value2[attr2])
|
|
212
|
+
|| attr2 === '$in' && value2[attr2] instanceof Array && value2[attr2].includes(value1.$eq)
|
|
213
|
+
|| attr2 === '$nin' && value2[attr2] instanceof Array && !value2[attr2].includes(value1.$eq)
|
|
214
|
+
|| attr2 === '$between' && value2[attr2][0] <= value1.$eq && value2[attr2][1] >= value1.$eq
|
|
215
|
+
|| attr2 === '$exists' && value2[attr2] === true;
|
|
216
|
+
}
|
|
177
217
|
return value2 === value1.$eq;
|
|
178
218
|
}
|
|
219
|
+
// 互斥
|
|
220
|
+
if (typeof value2 === 'object') {
|
|
221
|
+
var attr2 = Object.keys(value2)[0];
|
|
222
|
+
return attr2 === '$eq' && value2[attr2] !== value1.$eq || attr2 === '$gt' && value2[attr2] >= value1.$eq
|
|
223
|
+
|| attr2 === '$lt' && value2[attr2] <= value1.$eq
|
|
224
|
+
|| attr2 === '$gte' && value2[attr2] > value1.$eq || attr2 === '$lte' && value2[attr2] < value1.$eq
|
|
225
|
+
|| attr2 === '$startsWith' && !value1.$eq.startsWith(value2[attr2])
|
|
226
|
+
|| attr2 === '$endsWith' && !value1.$eq.endsWith(value2[attr2])
|
|
227
|
+
|| attr2 === '$includes' && !value1.$eq.includes(value2[attr2])
|
|
228
|
+
|| attr2 === '$in' && value2[attr2] instanceof Array && !value2[attr2].includes(value1.$eq)
|
|
229
|
+
|| attr2 === '$between' && (value2[attr2][0] > value1.$eq || value2[attr2][1] < value1.$eq)
|
|
230
|
+
|| attr2 === '$exists' && value2[attr2] === false;
|
|
231
|
+
}
|
|
232
|
+
return value2 !== value1.$eq;
|
|
179
233
|
}
|
|
180
234
|
case '$ne': {
|
|
235
|
+
if (contained) {
|
|
236
|
+
// 相容
|
|
237
|
+
if (typeof value2 === 'object') {
|
|
238
|
+
var attr2 = Object.keys(value2)[0];
|
|
239
|
+
return attr2 === '$ne' && value2[attr2] === value1.$ne;
|
|
240
|
+
}
|
|
241
|
+
return false;
|
|
242
|
+
}
|
|
243
|
+
// 互斥
|
|
181
244
|
if (typeof value2 === 'object') {
|
|
182
245
|
var attr2 = Object.keys(value2)[0];
|
|
183
|
-
return attr2 === '$eq' && value2[attr2]
|
|
184
|
-
|| attr2 === '$ne' && value2[attr2] === value1.$ne
|
|
185
|
-
|| attr2 === '$gt' && value2[attr2] >= value1.$ne
|
|
186
|
-
|| attr2 === '$gte' && value2[attr2] > value1.$ne
|
|
187
|
-
|| attr2 === '$lt' && value2[attr2] <= value1.$ne
|
|
188
|
-
|| attr2 === '$in' && value2[attr2] instanceof Array && !((value2[attr2]).find(function (ele) { return ele === value1.$ne; }))
|
|
189
|
-
|| attr2 === '$nin' && value2[attr2] instanceof Array && !!(value2[attr2]).find(function (ele) { return ele === value1.$ne; });
|
|
190
|
-
}
|
|
191
|
-
else {
|
|
192
|
-
return value2 !== value1.$ne;
|
|
246
|
+
return attr2 === '$eq' && value2[attr2] === value1.$ne;
|
|
193
247
|
}
|
|
248
|
+
return value2 === value1.$ne;
|
|
194
249
|
}
|
|
195
250
|
case '$startsWith': {
|
|
251
|
+
if (contained) {
|
|
252
|
+
// 相容
|
|
253
|
+
if (typeof value2 === 'object') {
|
|
254
|
+
var attr2 = Object.keys(value2)[0];
|
|
255
|
+
return attr2 === '$startsWith' && typeof (value2[attr2]) === 'string'
|
|
256
|
+
&& value1.$startsWith.startsWith(value2[attr2]);
|
|
257
|
+
}
|
|
258
|
+
return typeof value2 === 'string' && value1.$startsWith.startsWith(value2);
|
|
259
|
+
}
|
|
260
|
+
// 互斥
|
|
196
261
|
if (typeof value2 === 'object') {
|
|
197
262
|
var attr2 = Object.keys(value2)[0];
|
|
198
263
|
return attr2 === '$startsWith' && typeof (value2[attr2]) === 'string'
|
|
199
|
-
&& value2[attr2].startsWith(value1.$startsWith)
|
|
200
|
-
|| attr2 === '$
|
|
201
|
-
&& !value2[attr2].find(function (ele) { return typeof ele !== 'string' || !ele.startsWith(value1.$startsWith); });
|
|
202
|
-
}
|
|
203
|
-
else {
|
|
204
|
-
return typeof value2 === 'string' && value2.startsWith(value1.$startsWith);
|
|
264
|
+
&& !value1.$startsWith.startsWith(value2[attr2]) && !value2[attr2].startsWith(value1.$startsWith)
|
|
265
|
+
|| attr2 === '$eq' && !value2[attr2].startsWith(value1.$startsWith);
|
|
205
266
|
}
|
|
267
|
+
return !value2.startsWith(value1.$startsWith);
|
|
206
268
|
}
|
|
207
269
|
case '$endsWith': {
|
|
270
|
+
if (contained) {
|
|
271
|
+
// 相容
|
|
272
|
+
if (typeof value2 === 'object') {
|
|
273
|
+
var attr2 = Object.keys(value2)[0];
|
|
274
|
+
return attr2 === '$endsWith' && typeof (value2[attr2]) === 'string'
|
|
275
|
+
&& value1.$startsWith.endsWith(value2[attr2]);
|
|
276
|
+
}
|
|
277
|
+
return typeof value2 === 'string' && value1.$startsWith.endsWith(value2);
|
|
278
|
+
}
|
|
279
|
+
// 互斥
|
|
208
280
|
if (typeof value2 === 'object') {
|
|
209
281
|
var attr2 = Object.keys(value2)[0];
|
|
210
|
-
return attr2 === '$
|
|
211
|
-
&& value2[attr2].endsWith(value1.$
|
|
212
|
-
|| attr2 === '$
|
|
213
|
-
&& !value2[attr2].find(function (ele) { return typeof ele !== 'string' || !ele.endsWith(value1.$endsWith); });
|
|
214
|
-
}
|
|
215
|
-
else {
|
|
216
|
-
return typeof value2 === 'string' && value2.endsWith(value1.$endsWith);
|
|
282
|
+
return attr2 === '$startsWith' && typeof (value2[attr2]) === 'string'
|
|
283
|
+
&& !value1.$startsWith.endsWith(value2[attr2]) && !value2[attr2].endsWith(value1.$startsWith)
|
|
284
|
+
|| attr2 === '$eq' && !value2[attr2].endsWith(value1.$startsWith);
|
|
217
285
|
}
|
|
286
|
+
return !value2.endsWith(value1.$startsWith);
|
|
218
287
|
}
|
|
219
288
|
case '$includes': {
|
|
289
|
+
if (contained) {
|
|
290
|
+
// 相容
|
|
291
|
+
if (typeof value2 === 'object') {
|
|
292
|
+
var attr2 = Object.keys(value2)[0];
|
|
293
|
+
return ['$includes', '$startsWith', '$endsWith'].includes(attr2)
|
|
294
|
+
&& typeof (value2[attr2]) === 'string'
|
|
295
|
+
&& (value2[attr2]).includes(value1.$includes);
|
|
296
|
+
}
|
|
297
|
+
return typeof value2 === 'string' && value2.includes(value1.$includes);
|
|
298
|
+
}
|
|
299
|
+
// 互斥
|
|
220
300
|
if (typeof value2 === 'object') {
|
|
221
301
|
var attr2 = Object.keys(value2)[0];
|
|
222
|
-
return
|
|
223
|
-
&&
|
|
224
|
-
&& value2[attr2].includes(value1.$includes)
|
|
225
|
-
|| attr2 === '$in' && value2[attr2] instanceof Array
|
|
226
|
-
&& !value2[attr2].find(function (ele) { return typeof ele !== 'string' || !ele.includes(value1.$includes); });
|
|
227
|
-
}
|
|
228
|
-
else {
|
|
229
|
-
return typeof value2 === 'string' && value2.includes(value1.$includes);
|
|
302
|
+
return attr2 === '$eq' && !value2[attr2].includes(value1.$includes)
|
|
303
|
+
|| attr2 === '$in' && value2[attr2] instanceof Array && !value2[attr2].find(function (ele) { return ele.includes(value1.$includes); });
|
|
230
304
|
}
|
|
305
|
+
return typeof value2 === 'string' && !value2.includes(value1.$includes);
|
|
231
306
|
}
|
|
232
307
|
default: {
|
|
233
308
|
(0, assert_1.default)(false, "\u4E0D\u80FD\u5904\u7406\u7684\u7B97\u5B50\u300C".concat(attr, "\u300D"));
|
|
@@ -236,68 +311,139 @@ function judgeFilterValueRelation(value1, value2, contained) {
|
|
|
236
311
|
}
|
|
237
312
|
else if (['$exists'].includes(attr)) {
|
|
238
313
|
(0, assert_1.default)(attr === '$exists');
|
|
239
|
-
if (
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
return attr2 === '$exists' && value2[attr2] ===
|
|
243
|
-
}
|
|
244
|
-
else {
|
|
245
|
-
// 可能不完整,有没有更多情况?
|
|
246
|
-
return !(attr2 === '$exists' && value2[attr2] === false
|
|
247
|
-
|| attr2 === '$nin');
|
|
314
|
+
if (contained) {
|
|
315
|
+
if (typeof value2 === 'object') {
|
|
316
|
+
var attr2 = Object.keys(value2)[0];
|
|
317
|
+
return attr2 === '$exists' && value2[attr2] === value1.$exists;
|
|
248
318
|
}
|
|
319
|
+
return false;
|
|
249
320
|
}
|
|
250
|
-
|
|
251
|
-
return value1.$exists === true;
|
|
252
|
-
}
|
|
321
|
+
return typeof value2 === 'object' && value2.$exists === !(value1.$exists);
|
|
253
322
|
}
|
|
254
323
|
else if (['$in', '$nin', '$between'].includes(attr)) {
|
|
255
324
|
switch (attr) {
|
|
256
325
|
case '$in': {
|
|
326
|
+
if (contained) {
|
|
327
|
+
// 相容
|
|
328
|
+
if (value1.$in instanceof Array) {
|
|
329
|
+
if (typeof value2 === 'object') {
|
|
330
|
+
var attr2 = Object.keys(value2)[0];
|
|
331
|
+
if (attr2 === '$in') {
|
|
332
|
+
return value2[attr2] instanceof Array && (0, lodash_1.difference)(value1.$in, value2[attr2]).length === 0;
|
|
333
|
+
}
|
|
334
|
+
else if (attr2 === '$nin') {
|
|
335
|
+
return value2[attr2] instanceof Array && (0, lodash_1.intersection)(value1.$in, value2[attr2]).length === 0;
|
|
336
|
+
}
|
|
337
|
+
else if (attr2 === '$exists') {
|
|
338
|
+
return value2[attr2] === true;
|
|
339
|
+
}
|
|
340
|
+
else if (['$gt', '$gte', '$lt', '$lte', '$between'].includes(attr2)) {
|
|
341
|
+
var min33_1, max33_1;
|
|
342
|
+
value1.$in.forEach(function (ele) {
|
|
343
|
+
if (!min33_1 || min33_1 > ele) {
|
|
344
|
+
min33_1 = ele;
|
|
345
|
+
}
|
|
346
|
+
if (!max33_1 || max33_1 < ele) {
|
|
347
|
+
max33_1 = ele;
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
return attr2 === '$gt' && value2[attr2] < min33_1 || attr2 === '$gte' && value2[attr2] <= min33_1
|
|
351
|
+
|| attr2 === '$lt' && value2[attr2] > max33_1 || attr2 === '$lte' && value2[attr2] >= max33_1
|
|
352
|
+
|| attr2 === '$between' && value2[attr2][0] < min33_1 && value2[attr2][1] > max33_1;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
return false;
|
|
357
|
+
}
|
|
358
|
+
// 相斥
|
|
257
359
|
if (value1.$in instanceof Array) {
|
|
258
360
|
if (typeof value2 === 'object') {
|
|
259
361
|
var attr2 = Object.keys(value2)[0];
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
362
|
+
if (attr2 === '$in') {
|
|
363
|
+
return (0, lodash_1.intersection)(value2[attr2], value1.$in).length === 0;
|
|
364
|
+
}
|
|
365
|
+
else if (attr2 === '$eq') {
|
|
366
|
+
return !value1.$in.includes(value2[attr2]);
|
|
367
|
+
}
|
|
368
|
+
else if (attr2 === '$exists') {
|
|
369
|
+
return value2[attr2] === false;
|
|
370
|
+
}
|
|
371
|
+
else if (['$gt', '$gte', '$lt', '$lte', '$between'].includes(attr2)) {
|
|
372
|
+
var min44_1, max44_1;
|
|
373
|
+
value1.$in.forEach(function (ele) {
|
|
374
|
+
if (!min44_1 || min44_1 > ele) {
|
|
375
|
+
min44_1 = ele;
|
|
376
|
+
}
|
|
377
|
+
if (!max44_1 || max44_1 < ele) {
|
|
378
|
+
max44_1 = ele;
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
return attr2 === '$gt' && value2[attr2] >= max44_1 || attr2 === '$gte' && value2[attr2] > max44_1
|
|
382
|
+
|| attr2 === '$lt' && value2[attr2] <= min44_1 || attr2 === '$lte' && value2[attr2] < min44_1
|
|
383
|
+
|| attr2 === '$between' && (value2[attr2][0] > max44_1 || value2[attr2][1] < min44_1);
|
|
384
|
+
}
|
|
265
385
|
}
|
|
386
|
+
return !value1.$in.includes(value2);
|
|
266
387
|
}
|
|
267
|
-
|
|
268
|
-
// 子查询,暂不支持
|
|
269
|
-
return false;
|
|
270
|
-
}
|
|
388
|
+
return false;
|
|
271
389
|
}
|
|
272
390
|
case '$nin': {
|
|
391
|
+
if (contained) {
|
|
392
|
+
// 相容
|
|
393
|
+
if (value1.$nin instanceof Array) {
|
|
394
|
+
if (typeof value2 === 'object') {
|
|
395
|
+
var attr2 = Object.keys(value2)[0];
|
|
396
|
+
if (attr2 === '$nin') {
|
|
397
|
+
return value2[attr2] instanceof Array && (0, lodash_1.intersection)(value2[attr2], value1.$nin).length === 0;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
// 相斥
|
|
273
404
|
if (value1.$nin instanceof Array) {
|
|
274
405
|
if (typeof value2 === 'object') {
|
|
275
406
|
var attr2 = Object.keys(value2)[0];
|
|
276
|
-
|
|
277
|
-
&& (0, lodash_1.
|
|
278
|
-
|
|
279
|
-
&& (0, lodash_1.difference)(value1.$nin, value2[attr2]).length === 0;
|
|
280
|
-
}
|
|
281
|
-
else {
|
|
282
|
-
return !value1.$nin.includes(value2);
|
|
407
|
+
if (attr2 === '$in') {
|
|
408
|
+
return value2[attr2] instanceof Array && (0, lodash_1.difference)(value2[attr2], value1.$nin).length === 0;
|
|
409
|
+
}
|
|
283
410
|
}
|
|
284
411
|
}
|
|
285
|
-
|
|
286
|
-
// 子查询,暂不支持
|
|
287
|
-
return false;
|
|
288
|
-
}
|
|
412
|
+
return false;
|
|
289
413
|
}
|
|
290
414
|
case '$between': {
|
|
291
415
|
(0, assert_1.default)(value1.$between instanceof Array);
|
|
416
|
+
if (contained) {
|
|
417
|
+
// 相容
|
|
418
|
+
if (typeof value2 === 'object') {
|
|
419
|
+
var attr2 = Object.keys(value2)[0];
|
|
420
|
+
if (['$gt', '$gte', '$lt', '$lte', '$between', '$eq'].includes(attr2)) {
|
|
421
|
+
return attr2 === '$between' && value2[attr2][0] <= value1.$between[0] && value2[attr2][1] >= value1.$between[1]
|
|
422
|
+
|| attr2 === '$gt' && value2[attr2] < value1.$between[0] || attr2 === '$gte' && value2[attr2] <= value1.$between[0]
|
|
423
|
+
|| attr2 === '$lt' && value2[attr2] > value1.$between[1] || attr2 === '$lte' && value2[attr2] >= value1.$between[1];
|
|
424
|
+
}
|
|
425
|
+
else if (attr2 === '$exists') {
|
|
426
|
+
return value2[attr2] === true;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
return false;
|
|
430
|
+
}
|
|
431
|
+
// 相斥
|
|
292
432
|
if (typeof value2 === 'object') {
|
|
293
433
|
var attr2 = Object.keys(value2)[0];
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
&& value2[attr2]
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
434
|
+
if (['$gt', '$gte', '$lt', '$lte', '$between', '$eq'].includes(attr2)) {
|
|
435
|
+
return attr2 === '$between' && (value2[attr2][0] > value1.$between[1] || value2[attr2][1] < value1.$between[0])
|
|
436
|
+
|| attr2 === '$gt' && value2[attr2] > value1.$between[1] || attr2 === '$gte' && value2[attr2] >= value1.$between[1]
|
|
437
|
+
|| attr2 === '$lt' && value2[attr2] < value1.$between[0] || attr2 === '$lte' && value2[attr2] <= value1.$between[0]
|
|
438
|
+
|| attr2 === '$eq' && (value2[attr2] > value1.$between[1] || value2[attr2] < value1.$between[0]);
|
|
439
|
+
}
|
|
440
|
+
else if (attr2 === '$exists') {
|
|
441
|
+
return value2[attr2] === false;
|
|
442
|
+
}
|
|
443
|
+
else if (attr2 === '$in' && value2[attr2] instanceof Array) {
|
|
444
|
+
return !value2[attr2].find(function (ele) { return ele >= value1.$between[0] && ele <= value1.$between[1]; });
|
|
445
|
+
}
|
|
446
|
+
return false;
|
|
301
447
|
}
|
|
302
448
|
}
|
|
303
449
|
default: {
|
|
@@ -311,34 +457,58 @@ function judgeFilterValueRelation(value1, value2, contained) {
|
|
|
311
457
|
}
|
|
312
458
|
else {
|
|
313
459
|
// value1是一个等值查询
|
|
314
|
-
if (
|
|
315
|
-
|
|
460
|
+
if (contained) {
|
|
461
|
+
// 相容
|
|
462
|
+
if (typeof value2 === 'object') {
|
|
463
|
+
var attr2 = Object.keys(value2)[0];
|
|
464
|
+
return attr2 === '$eq' && value2[attr2] === value1 || attr2 === '$ne' && value2[attr2] !== value1
|
|
465
|
+
|| attr2 === '$gt' && value2[attr2] < value1 || attr2 === '$lt' && value2[attr2] > value1
|
|
466
|
+
|| attr2 === '$gte' && value2[attr2] <= value1 || attr2 === '$lte' && value2[attr2] >= value1
|
|
467
|
+
|| attr2 === '$startsWith' && value1.startsWith(value2[attr2])
|
|
468
|
+
|| attr2 === '$endsWith' && value1.endsWith(value2[attr2])
|
|
469
|
+
|| attr2 === '$includes' && value1.includes(value2[attr2])
|
|
470
|
+
|| attr2 === '$in' && value2[attr2] instanceof Array && value2[attr2].includes(value1)
|
|
471
|
+
|| attr2 === '$nin' && value2[attr2] instanceof Array && !value2[attr2].includes(value1)
|
|
472
|
+
|| attr2 === '$between' && value2[attr2][0] <= value1 && value2[attr2][1] >= value1
|
|
473
|
+
|| attr2 === '$exists' && value2[attr2] === true;
|
|
474
|
+
}
|
|
475
|
+
return value2 === value1;
|
|
316
476
|
}
|
|
317
|
-
|
|
318
|
-
|
|
477
|
+
// 互斥
|
|
478
|
+
if (typeof value2 === 'object') {
|
|
479
|
+
var attr2 = Object.keys(value2)[0];
|
|
480
|
+
return attr2 === '$eq' && value2[attr2] !== value1 || attr2 === '$gt' && value2[attr2] >= value1
|
|
481
|
+
|| attr2 === '$lt' && value2[attr2] <= value1
|
|
482
|
+
|| attr2 === '$gte' && value2[attr2] > value1 || attr2 === '$lte' && value2[attr2] < value1
|
|
483
|
+
|| attr2 === '$startsWith' && !value1.startsWith(value2[attr2])
|
|
484
|
+
|| attr2 === '$endsWith' && !value1.endsWith(value2[attr2])
|
|
485
|
+
|| attr2 === '$includes' && !value1.includes(value2[attr2])
|
|
486
|
+
|| attr2 === '$in' && value2[attr2] instanceof Array && !value2[attr2].includes(value1)
|
|
487
|
+
|| attr2 === '$between' && (value2[attr2][0] > value1 || value2[attr2][1] < value1)
|
|
488
|
+
|| attr2 === '$exists' && value2[attr2] === false;
|
|
319
489
|
}
|
|
490
|
+
return value2 !== value1;
|
|
320
491
|
}
|
|
321
492
|
}
|
|
322
|
-
|
|
493
|
+
exports.judgeValueRelation = judgeValueRelation;
|
|
494
|
+
function judgeFilter2ValueRelation(entity, schema, attr, filter, conditionalFilterAttrValue, contained) {
|
|
323
495
|
var _a;
|
|
324
496
|
for (var attr2 in filter) {
|
|
325
|
-
if (['$and', '$or'].includes(attr2)) {
|
|
497
|
+
if (['$and', '$or', '$not'].includes(attr2)) {
|
|
326
498
|
switch (attr2) {
|
|
327
499
|
case '$and':
|
|
328
500
|
case '$or':
|
|
329
501
|
case '$xor': {
|
|
330
502
|
var logicQueries = filter[attr2];
|
|
331
|
-
var results = logicQueries.map(function (logicQuery) { return
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
503
|
+
var results = logicQueries.map(function (logicQuery) { return judgeFilter2ValueRelation(entity, schema, attr, logicQuery, conditionalFilterAttrValue, contained); });
|
|
504
|
+
// 如果filter的多个算子是and关系,则只要有一个包含此条件就是包含,只要有一个与此条件相斥就是相斥
|
|
505
|
+
// 如果filter的多个算子是or关系,则必须所有的条件都包含此条件才是包含,必须所有的条件都与此条件相斥才是相斥
|
|
506
|
+
if (attr2 === '$and') {
|
|
335
507
|
if (results.includes(true)) {
|
|
336
508
|
return true;
|
|
337
509
|
}
|
|
338
510
|
}
|
|
339
|
-
else if (attr2 === '$or'
|
|
340
|
-
// 如果filter的多个算子是or关系,则必须每个都能包含此条件
|
|
341
|
-
// 如果filter的多个算子是and关系,则必须每个都与此条件相斥
|
|
511
|
+
else if (attr2 === '$or') {
|
|
342
512
|
if (!results.includes(false)) {
|
|
343
513
|
return true;
|
|
344
514
|
}
|
|
@@ -349,19 +519,19 @@ function compareFilter2AttributeValue(entity, schema, attr, filter, conditionalF
|
|
|
349
519
|
break;
|
|
350
520
|
}
|
|
351
521
|
case '$not': {
|
|
352
|
-
/*
|
|
353
|
-
*
|
|
354
|
-
*
|
|
355
|
-
*
|
|
356
|
-
* 如: conditionalFilter为 { a: 2 }
|
|
357
|
-
* filter 为 { $not: { a: { $gt: 1 }}}
|
|
358
|
-
*
|
|
359
|
-
* todo 再想一想对吗?
|
|
522
|
+
/*
|
|
523
|
+
* 若filter的not条件被conditionalFilterAttrValue包容,则说明两者互斥
|
|
524
|
+
* 若filter的not条件被conditionalFilterAttrValue的not条件所包容,则说明此条件包容conditionalFilterAttrValue
|
|
525
|
+
* 但两条规则都没法应用,会无限递归
|
|
360
526
|
*/
|
|
361
527
|
var logicQuery = filter[attr2];
|
|
362
|
-
if (contained &&
|
|
363
|
-
|
|
364
|
-
|
|
528
|
+
/* if (contained && judgeFilterRelation(entity, schema, {
|
|
529
|
+
[attr2]: {
|
|
530
|
+
$not: conditionalFilterAttrValue
|
|
531
|
+
}
|
|
532
|
+
}, logicQuery, contained)) {
|
|
533
|
+
return true;
|
|
534
|
+
} */
|
|
365
535
|
if (!contained && judgeFilterRelation(entity, schema, (_a = {}, _a[attr2] = conditionalFilterAttrValue, _a), logicQuery, contained)) {
|
|
366
536
|
return true;
|
|
367
537
|
}
|
|
@@ -382,7 +552,7 @@ function compareFilter2AttributeValue(entity, schema, attr, filter, conditionalF
|
|
|
382
552
|
if (attr === attr2) {
|
|
383
553
|
var rel = (0, relation_1.judgeRelation)(schema, entity, attr2);
|
|
384
554
|
if (rel === 1) {
|
|
385
|
-
return
|
|
555
|
+
return judgeValueRelation(filter[attr2], conditionalFilterAttrValue, contained);
|
|
386
556
|
}
|
|
387
557
|
else if (rel === 2) {
|
|
388
558
|
return judgeFilterRelation(attr2, schema, filter[attr2], conditionalFilterAttrValue, contained);
|
|
@@ -396,7 +566,7 @@ function compareFilter2AttributeValue(entity, schema, attr, filter, conditionalF
|
|
|
396
566
|
}
|
|
397
567
|
}
|
|
398
568
|
}
|
|
399
|
-
return
|
|
569
|
+
return false;
|
|
400
570
|
}
|
|
401
571
|
/**
|
|
402
572
|
* @param entity
|
|
@@ -431,9 +601,13 @@ function judgeFilterRelation(entity, schema, filter, conditionalFilter, containe
|
|
|
431
601
|
break;
|
|
432
602
|
}
|
|
433
603
|
case '$not': {
|
|
604
|
+
/**
|
|
605
|
+
* 若filter与not所定义的filter相斥,则filter与conditionalFilter相容
|
|
606
|
+
* 若filter与not所定义的filter相容,则filter与conditionalFilter相斥
|
|
607
|
+
*/
|
|
434
608
|
var logicQuery = conditionalFilter[attr];
|
|
435
|
-
if (
|
|
436
|
-
return
|
|
609
|
+
if (judgeFilterRelation(entity, schema, filter, logicQuery, !contained)) {
|
|
610
|
+
return true;
|
|
437
611
|
}
|
|
438
612
|
break;
|
|
439
613
|
}
|
|
@@ -449,16 +623,17 @@ function judgeFilterRelation(entity, schema, filter, conditionalFilter, containe
|
|
|
449
623
|
return false;
|
|
450
624
|
}
|
|
451
625
|
else {
|
|
452
|
-
if (contained && !
|
|
626
|
+
if (contained && !judgeFilter2ValueRelation(entity, schema, attr, filter, conditionalFilter[attr], contained)) {
|
|
453
627
|
// 相容关系只要有一个不相容就不相容
|
|
454
628
|
return false;
|
|
455
629
|
}
|
|
456
|
-
if (!contained &&
|
|
630
|
+
if (!contained && judgeFilter2ValueRelation(entity, schema, attr, filter, conditionalFilter[attr], contained)) {
|
|
457
631
|
// 相斥关系只要有一个相斥就相斥
|
|
458
632
|
return true;
|
|
459
633
|
}
|
|
460
634
|
}
|
|
461
635
|
}
|
|
636
|
+
// 到这里说明不能否定其相容(所以要返回相容),也不能肯定其相斥(所以要返回不相斥)
|
|
462
637
|
return contained;
|
|
463
638
|
}
|
|
464
639
|
/**
|
|
@@ -480,6 +655,7 @@ function judgeFilterRelation(entity, schema, filter, conditionalFilter, containe
|
|
|
480
655
|
* @returns
|
|
481
656
|
*/
|
|
482
657
|
function contains(entity, schema, filter, conditionalFilter) {
|
|
658
|
+
// return judgeFilterRelation(entity, schema, filter, conditionalFilter, true);
|
|
483
659
|
return false;
|
|
484
660
|
}
|
|
485
661
|
exports.contains = contains;
|
|
@@ -499,6 +675,7 @@ exports.contains = contains;
|
|
|
499
675
|
*/
|
|
500
676
|
function repel(entity, schema, filter1, filter2) {
|
|
501
677
|
// todo
|
|
678
|
+
// return judgeFilterRelation(entity, schema, filter1, filter2, false);
|
|
502
679
|
return false;
|
|
503
680
|
}
|
|
504
681
|
exports.repel = repel;
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oak-domain",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "XuChang"
|
|
6
6
|
},
|
|
7
7
|
"scripts": {
|
|
8
8
|
"make:domain": "ts-node scripts/make.ts",
|
|
9
9
|
"build": "tsc",
|
|
10
|
-
"prebuild": "npm run make:domain"
|
|
10
|
+
"prebuild": "npm run make:domain",
|
|
11
|
+
"test": "ts-node test/testFilter.ts"
|
|
11
12
|
},
|
|
12
13
|
"files": [
|
|
13
14
|
"lib/**/*",
|