forked from thomaspoignant/scim2-parse-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflatten.ts
35 lines (34 loc) · 906 Bytes
/
flatten.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
import { Filter } from "./index";
export const valfilter = (f: Filter, path?: string): Filter => {
if (path && "attrPath" in f) {
f = { ...f, attrPath: `${path}.${f.attrPath}` };
}
switch (f.op) {
case "and":
case "or":
return { ...f, filters: f.filters.map(c => valfilter(c, path)) };
case "not":
return { ...f, filter: valfilter(f, path) };
case "[]":
return valfilter(f.valFilter, f.attrPath);
}
return f;
};
// 1 and 2 or (1 or b) => 1 and 2 or 1 or b
export const log = (f: Filter): Filter => {
switch (f.op) {
case "and":
case "or":
const filters = f.filters.map(log);
const result: Filter[] = [];
filters.forEach(c => {
if (c.op == f.op) {
c.filters.forEach(cc => result.push(cc));
} else {
result.push(c);
}
});
return { ...f, filters: result };
}
return f;
};