forked from thomaspoignant/scim2-parse-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringify.ts
36 lines (34 loc) · 951 Bytes
/
stringify.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
import { Filter } from "./index";
export function stringify(f: Filter, wrapOr = false): string {
let returnValue = '';
switch (f.op) {
case "eq":
case "ne":
case "co":
case "sw":
case "ew":
case "gt":
case "lt":
case "ge":
case "le":
returnValue = `${f.attrPath} ${f.op} ${JSON.stringify(f.compValue)}`;
break;
case "pr":
returnValue = `${f.attrPath} ${f.op}`;
break;
case "or":
const filtersAsString = f.filters.map(filter => stringify(filter)).join(` ${f.op} `);
returnValue = wrapOr ? `(${filtersAsString})` : filtersAsString;
break;
case "and":
returnValue = f.filters.map(filter => stringify(filter, true)).join(` ${f.op} `);
break;
case "not":
returnValue = `${f.op} (${stringify(f.filter)})`;
break;
case "[]":
returnValue = `${f.attrPath}[${stringify(f.valFilter)}]`;
break;
}
return returnValue;
}