Repository navigation
Expand file tree
/
Copy patheventFilterMatches.ts
More file actions
214 lines (171 loc) · 5.4 KB
/
Copy patheventFilterMatches.ts
File metadata and controls
214 lines (171 loc) · 5.4 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
import type { EventFilter } from "./schemas/eventFilter.js";
// EventFilter is a recursive type, where the keys are strings and the values are an array of strings, numbers, booleans, or objects.
// If the values of the array are strings, numbers, or booleans, than we are matching against the value of the payload.
// If the values of the array are objects, then we are doing content filtering
// An example would be [{ $endsWith: ".png" }, { $startsWith: "images/" } ]
export function eventFilterMatches(payload: any, filter: EventFilter): boolean {
if (payload === undefined || payload === null) {
if (Object.entries(filter).length === 0) {
return true;
} else {
return false;
}
}
for (const [patternKey, patternValue] of Object.entries(filter)) {
const payloadValue = payload[patternKey];
if (Array.isArray(patternValue)) {
if (patternValue.length === 0) {
continue;
}
// Check to see if all the items in the array are a string
if ((patternValue as unknown[]).every((item) => typeof item === "string")) {
if ((patternValue as string[]).includes(payloadValue)) {
continue;
}
return false;
}
// Check to see if all the items in the array are a number
if ((patternValue as unknown[]).every((item) => typeof item === "number")) {
if ((patternValue as number[]).includes(payloadValue)) {
continue;
}
return false;
}
// Check to see if all the items in the array are a boolean
if ((patternValue as unknown[]).every((item) => typeof item === "boolean")) {
if ((patternValue as boolean[]).includes(payloadValue)) {
continue;
}
return false;
}
// Now we know that all the items in the array are objects
const objectArray = patternValue as Exclude<
typeof patternValue,
number[] | string[] | boolean[]
>;
if (!contentFiltersMatches(payloadValue, objectArray)) {
return false;
}
continue;
} else if (typeof patternValue === "object") {
if (Array.isArray(payloadValue)) {
if (!payloadValue.some((item) => eventFilterMatches(item, patternValue))) {
return false;
}
} else {
if (!eventFilterMatches(payloadValue, patternValue)) {
return false;
}
}
}
}
return true;
}
type ContentFilters = Exclude<EventFilter[string], EventFilter | string[] | number[] | boolean[]>;
function contentFiltersMatches(actualValue: any, contentFilters: ContentFilters): boolean {
for (const contentFilter of contentFilters) {
if (typeof contentFilter === "object") {
if (!contentFilterMatches(actualValue, contentFilter)) {
return false;
}
}
}
return true;
}
function contentFilterMatches(actualValue: any, contentFilter: ContentFilters[number]): boolean {
if ("$endsWith" in contentFilter) {
if (typeof actualValue !== "string") {
return false;
}
return actualValue.endsWith(contentFilter.$endsWith);
}
if ("$startsWith" in contentFilter) {
if (typeof actualValue !== "string") {
return false;
}
return actualValue.startsWith(contentFilter.$startsWith);
}
if ("$anythingBut" in contentFilter) {
if (Array.isArray(contentFilter.$anythingBut)) {
if ((contentFilter.$anythingBut as any[]).includes(actualValue)) {
return false;
}
}
if (contentFilter.$anythingBut === actualValue) {
return false;
}
return true;
}
if ("$exists" in contentFilter) {
if (contentFilter.$exists) {
return actualValue !== undefined;
}
return actualValue === undefined;
}
if ("$gt" in contentFilter) {
if (typeof actualValue !== "number") {
return false;
}
return actualValue > contentFilter.$gt;
}
if ("$lt" in contentFilter) {
if (typeof actualValue !== "number") {
return false;
}
return actualValue < contentFilter.$lt;
}
if ("$gte" in contentFilter) {
if (typeof actualValue !== "number") {
return false;
}
return actualValue >= contentFilter.$gte;
}
if ("$lte" in contentFilter) {
if (typeof actualValue !== "number") {
return false;
}
return actualValue <= contentFilter.$lte;
}
if ("$between" in contentFilter) {
if (typeof actualValue !== "number") {
return false;
}
return actualValue >= contentFilter.$between[0] && actualValue <= contentFilter.$between[1];
}
if ("$includes" in contentFilter) {
if (Array.isArray(actualValue)) {
return actualValue.includes(contentFilter.$includes);
}
return false;
}
// Use localCompare
if ("$ignoreCaseEquals" in contentFilter) {
if (typeof actualValue !== "string") {
return false;
}
return (
actualValue.localeCompare(contentFilter.$ignoreCaseEquals, undefined, {
sensitivity: "accent",
}) === 0
);
}
if ("$isNull" in contentFilter) {
if (contentFilter.$isNull) {
return actualValue === null;
}
return actualValue !== null;
}
if ("$not" in contentFilter) {
if (Array.isArray(actualValue)) {
return !actualValue.includes(contentFilter.$not);
} else if (
typeof actualValue === "number" ||
typeof actualValue === "boolean" ||
typeof actualValue === "string"
) {
return actualValue !== contentFilter.$not;
}
return false;
}
return true;
}