-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathinternalFilter.ts
264 lines (221 loc) · 5.71 KB
/
internalFilter.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
252
253
254
255
256
257
258
259
260
261
262
263
264
import { createStringTest } from './createStringTest';
import { testComparisonRange } from './testComparisonRange';
import { testRange } from './testRange';
import {
type InternalHighlight,
type InternalTest,
type LiqeQuery,
} from './types';
const createValueTest = (ast: LiqeQuery): InternalTest => {
if (ast.type !== 'Tag') {
throw new Error('Expected a tag expression.');
}
const { expression } = ast;
if (expression.type === 'RangeExpression') {
return (value) => {
return testRange(value, expression.range);
};
}
if (expression.type === 'EmptyExpression') {
return () => {
return false;
};
}
const expressionValue = expression.value;
if (ast.operator && ast.operator.operator !== ':') {
const operator = ast.operator;
if (typeof expressionValue !== 'number') {
throw new TypeError('Expected a number.');
}
return (value) => {
if (typeof value !== 'number') {
return false;
}
return testComparisonRange(expressionValue, value, operator.operator);
};
} else if (typeof expressionValue === 'boolean') {
return (value) => {
return value === expressionValue;
};
} else if (expressionValue === null) {
return (value) => {
return value === null;
};
} else {
const testString = createStringTest({}, ast);
return (value) => {
return testString(String(value));
};
}
};
const testValue = (
ast: LiqeQuery,
value: unknown,
resultFast: boolean,
path: readonly string[],
highlights: InternalHighlight[],
) => {
if (Array.isArray(value)) {
let foundMatch = false;
let index = 0;
for (const item of value) {
if (
testValue(ast, item, resultFast, [...path, String(index++)], highlights)
) {
if (resultFast) {
return true;
}
foundMatch = true;
}
}
return foundMatch;
} else if (typeof value === 'object' && value !== null) {
let foundMatch = false;
for (const key in value) {
if (testValue(ast, value[key], resultFast, [...path, key], highlights)) {
if (resultFast) {
return true;
}
foundMatch = true;
}
}
return foundMatch;
}
if (ast.type !== 'Tag') {
throw new Error('Expected a tag expression.');
}
if (!ast.test) {
throw new Error('Expected test to be defined.');
}
const result = ast.test(value);
if (result) {
highlights.push({
...(typeof result === 'string' && { keyword: result }),
path: path.join('.'),
});
return true;
}
return Boolean(result);
};
const testField = <T extends Object>(
row: T,
ast: LiqeQuery,
resultFast: boolean,
path: readonly string[],
highlights: InternalHighlight[],
): boolean => {
if (ast.type !== 'Tag') {
throw new Error('Expected a tag expression.');
}
if (!ast.test) {
ast.test = createValueTest(ast);
}
if (ast.field.type === 'ImplicitField') {
let foundMatch = false;
for (const fieldName in row) {
if (
testValue(
{
...ast,
field: {
location: {
end: -1,
start: -1,
},
name: fieldName,
quoted: true,
quotes: 'double',
type: 'Field',
},
},
row[fieldName],
resultFast,
[...path, fieldName],
highlights,
)
) {
if (resultFast) {
return true;
}
foundMatch = true;
}
}
return foundMatch;
}
if (ast.field.name in row) {
return testValue(ast, row[ast.field.name], resultFast, path, highlights);
} else if (ast.getValue && ast.field.path) {
return testValue(
ast,
ast.getValue(row),
resultFast,
ast.field.path,
highlights,
);
} else if (ast.field.path) {
let value = row;
for (const key of ast.field.path) {
if (typeof value !== 'object' || value === null) {
return false;
} else if (key in value) {
value = value[key];
} else {
return false;
}
}
return testValue(ast, value, resultFast, ast.field.path, highlights);
} else {
return false;
}
};
export const internalFilter = <T extends Object>(
ast: LiqeQuery,
rows: readonly T[],
resultFast: boolean = true,
path: readonly string[] = [],
highlights: InternalHighlight[] = [],
): readonly T[] => {
if (ast.type === 'Tag') {
return rows.filter((row) => {
return testField(
row,
ast,
resultFast,
ast.field.type === 'ImplicitField' ? path : [...path, ast.field.name],
highlights,
);
});
}
if (ast.type === 'UnaryOperator') {
const removeRows = internalFilter(ast.operand, rows, resultFast, path, []);
return rows.filter((row) => {
return !removeRows.includes(row);
});
}
if (ast.type === 'ParenthesizedExpression') {
return internalFilter(ast.expression, rows, resultFast, path, highlights);
}
if (!ast.left) {
throw new Error('Expected left to be defined.');
}
const leftRows = internalFilter(ast.left, rows, resultFast, path, highlights);
if (!ast.right) {
throw new Error('Expected right to be defined.');
}
if (ast.type !== 'LogicalExpression') {
throw new Error('Expected a tag expression.');
}
if (ast.operator.operator === 'OR') {
const rightRows = internalFilter(
ast.right,
rows,
resultFast,
path,
highlights,
);
return Array.from(new Set([...leftRows, ...rightRows]));
} else if (ast.operator.operator === 'AND') {
return internalFilter(ast.right, leftRows, resultFast, path, highlights);
}
throw new Error('Unexpected state.');
};