This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
/
createInMemoryFilterPredicate.ts
202 lines (168 loc) · 7.22 KB
/
createInMemoryFilterPredicate.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
import { convertType, isDateObject } from '../utils/convertType';
import { parseObjectID, isObjectID, getObjectIDTimestamp } from '../scalars/objectId';
import { QueryFilter } from './QueryFilter';
type PredicateFn = (input: any) => boolean;
type InputType = number | string | boolean | Date;
interface IPredicate {
eq(filterValue: InputType): PredicateFn
ne(filterValue: InputType): PredicateFn
gt(filterValue: InputType): PredicateFn
ge(filterValue: InputType): PredicateFn
le(filterValue: InputType): PredicateFn
lt(filterValue: InputType): PredicateFn
in(filterValue: InputType[]): PredicateFn
between(filterValue: InputType[]): PredicateFn
contains(filterValue: InputType): PredicateFn
startsWith(filterValue: string): PredicateFn
endsWith(filterValue: string): PredicateFn
}
const predicateMap: IPredicate = {
eq: (filterValue: InputType) => (fieldValue: InputType) => {
const parsedFieldValue = convertType(fieldValue, filterValue);
const parsedFilterValue = convertType(filterValue, parsedFieldValue)
return parsedFieldValue?.toString() === parsedFilterValue?.toString();
},
ne: (filterValue: InputType) => (fieldValue: InputType) => {
const parsedFieldValue = convertType(fieldValue, filterValue);
const parsedFilterValue = convertType(filterValue, parsedFieldValue)
return parsedFilterValue?.toString() !== parsedFieldValue?.toString();
},
gt: (filterValue: InputType) => (fieldValue: InputType) => {
const parsedFieldValue = convertType(fieldValue, filterValue);
const parsedFilterValue = convertType(filterValue, parsedFieldValue);
return parsedFieldValue > parsedFilterValue;
},
ge: (filterValue: InputType) => (fieldValue: InputType) => {
const parsedFieldValue = convertType(fieldValue, filterValue);
const parsedFilterValue = convertType(filterValue, parsedFieldValue);
// if values are of MongoDb ObjectID, convert to timestamp before comparison
if (isObjectID(parsedFieldValue) && isObjectID(parsedFilterValue)) {
return getObjectIDTimestamp(parsedFieldValue) >= getObjectIDTimestamp(parsedFilterValue)
}
return parsedFieldValue >= parsedFilterValue;
},
le: (filterValue: InputType) => (fieldValue: InputType) => {
const parsedFieldValue = convertType(fieldValue, filterValue);
const parsedFilterValue = convertType(filterValue, parsedFieldValue)
// if values are of MongoDb ObjectID, convert to timestamp before comparison
if (isObjectID(parsedFieldValue) && isObjectID(parsedFilterValue)) {
return getObjectIDTimestamp(parsedFieldValue) <= getObjectIDTimestamp(parsedFilterValue)
}
return parsedFieldValue <= parsedFilterValue;
},
lt: (filterValue: InputType) => (fieldValue: InputType) => {
const parsedFieldValue = convertType(fieldValue, filterValue);
const parsedFilterValue = convertType(filterValue, parsedFieldValue);
// if values are of MongoDb ObjectID, convert to timestamp before comparison
if (isObjectID(parsedFieldValue) && isObjectID(parsedFilterValue)) {
return getObjectIDTimestamp(parsedFieldValue) < getObjectIDTimestamp(parsedFilterValue);
}
return parsedFieldValue < parsedFilterValue;
},
in: (filterValue: InputType[]) => (fieldValue: InputType) => {
return filterValue.map((f: InputType) => f?.toString()).includes(fieldValue?.toString());
},
between: ([fromVal, toVal]: InputType[]) => (fieldValue: InputType) => {
if (isDateObject(fieldValue)) {
const fieldValDate = convertType(fieldValue, fieldValue)
const fromValDate = convertType(fromVal, fieldValue)
const toValDate = convertType(toVal, fieldValue)
return fieldValDate >= fromValDate && fieldValDate <= toValDate;
}
// if values are of MongoDb ObjectID, convert to timestamp before comparison
if (isObjectID(fromVal) || isObjectID(toVal) || isObjectID(fieldValue)) {
const toValTimestamp = getObjectIDTimestamp(parseObjectID(toVal.toString()));
const fromValTimestamp = getObjectIDTimestamp(parseObjectID(fromVal.toString()));
const fieldValTimestamp = getObjectIDTimestamp(parseObjectID(fieldValue.toString()));
return fieldValTimestamp >= fromValTimestamp && fieldValTimestamp <= toValTimestamp;
}
const parsedFieldValue = Number(fieldValue)
return parsedFieldValue >= Number(fromVal) && parsedFieldValue <= Number(toVal);
},
contains: (filterValue: InputType = '') => (fieldValue: InputType = '') => {
return fieldValue?.toString().includes(filterValue?.toString());
},
startsWith: (filterValue: string = '') => (fieldValue: string = '') => {
return fieldValue?.toString().startsWith(filterValue?.toString());
},
endsWith: (filterValue: string = '') => (fieldValue: string = '') => {
return fieldValue?.toString().endsWith(filterValue?.toString());
}
}
/**
* Dynamically creates a subscription filter predicate using the filter object values
*
* @param {QueryFilter} filter - subscription filter input object
*/
export function createInMemoryFilterPredicate<T = any>(filter: QueryFilter): (input: Partial<T>) => boolean {
filter = filter || {};
const andFilter = filter.and;
const orFilter = filter.or;
const notFilter = filter.not;
const filterFields = Object.keys(filter).filter((key: string) => !['and', 'or', 'not'].includes(key));
return (payload: Partial<T>): boolean => {
let predicateResult = true;
for (const fieldName of filterFields) {
// skip these filter expressions
if (['and', 'or', 'not'].includes(fieldName)) {
continue;
}
const fieldFilter = filter[fieldName];
for (const [expr, exprVal] of Object.entries(fieldFilter)) {
const predicateFn: PredicateFn = predicateMap[expr](exprVal)
if (!predicateFn(payload[fieldName])) {
predicateResult = false;
break;
}
}
}
if (orFilter) {
const orPredicateResult = getOrPredicateResult<T>(orFilter, payload);
predicateResult = predicateResult && orPredicateResult;
if (!predicateResult) {
return false;
}
}
if (andFilter) {
const andPredicateResult = getAndPredicateResult(andFilter, payload);
predicateResult = predicateResult && andPredicateResult;
}
if (notFilter) {
const notPredicateResult = createInMemoryFilterPredicate<T>(notFilter)(payload);
predicateResult = predicateResult && !notPredicateResult;
}
return predicateResult;
}
}
/**
* Get the predicate result of an `and` filter expression
*
* @param {QueryFilter[]} and - And filter
* @param {Partial<T>} payload - Subscription payload
*/
function getAndPredicateResult<T>(and: QueryFilter[], payload: Partial<T>): boolean {
let andResult = true;
for (const andItem of and) {
andResult = createInMemoryFilterPredicate<T>(andItem)(payload);
if (!andResult) {
break;
}
}
return andResult;
}
/**
* Get the boolean result of an `or` filter expression
*
* @param {QueryFilter[]} or - Or query filter
* @param {Partial<T>} payload - Subscription payload
*/
function getOrPredicateResult<T>(or: QueryFilter[], payload: Partial<T>): boolean {
let orResult = true;
for (const orItem of or) {
orResult = createInMemoryFilterPredicate<T>(orItem)(payload);
if (orResult) {
break;
}
}
return orResult;
}