-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBoolFilter.ts
More file actions
96 lines (89 loc) · 3.17 KB
/
BoolFilter.ts
File metadata and controls
96 lines (89 loc) · 3.17 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
export default class BoolFilter {
private boolString = '';
private boolTemplate = '';
private boolOperands: string[] = [];
constructor(filter: string) {
this.boolString = filter;
this.buildBoolTemplate();
}
public isInvalidFilterSyntax(): boolean {
if (this.boolTemplate.length > 0) {
let boolString = this.boolTemplate;
for (let i = 0; i < this.boolOperands.length; ++i) {
boolString = boolString.replace('###'+i, 'true');
}
//console.log(boolString);
try {
// eslint-disable-next-line no-eval
eval(boolString);
return false;
} catch (e) {
return true;
}
}
return false;
}
private buildBoolTemplate() {
this.boolTemplate = '';
this.boolOperands.splice(0, this.boolOperands.length);
let argNum = 0;
if (this.boolString.includes('!')
|| this.boolString.includes('&&')
|| this.boolString.includes('||')) {
let operand = '';
for (let i = 0; i < this.boolString.length; ++i) {
let c1 = this.boolString.substr(i, 1);
let c2 = i < this.boolString.length - 1 ? this.boolString.substr(i + 1, 1) : '';
let nonOperand = '';
if (c1 === '!' || c1 === '(' || c1 === ')') nonOperand = c1;
if (c1 === '&' && c2 === '&') {
++i;
nonOperand = '&&';
}
if (c1 === '|' && c2 === '|') {
++i;
nonOperand = '||';
}
if (nonOperand.length > 0) {
operand = operand.trim();
if (operand.length > 0) {
this.boolTemplate += '###' + argNum++;
this.boolOperands.push(operand);
operand = '';
}
this.boolTemplate += nonOperand;
}
else {
operand += c1;
}
}
if (operand.length > 0) {
this.boolTemplate += '###' + argNum++;
this.boolOperands.push(operand.trim());
}
}
}
public getFilter() {
return this.boolString;
}
public isFiltered(data: string) {
if (this.boolString.length === 0) return false;
if (this.boolTemplate.length > 0) {
let boolString = this.boolTemplate;
for (let i = 0; i < this.boolOperands.length; ++i) {
const filtered = data.indexOf(this.boolOperands[i]) === -1;
boolString = boolString.replace('###'+i, (filtered ? 'false' : 'true'));
}
//console.log(boolString);
try {
// eslint-disable-next-line no-eval
return !eval(boolString);
} catch (e) {
return true;
}
}
else {
return data.indexOf(this.boolString) === -1;
}
}
}