-
Notifications
You must be signed in to change notification settings - Fork 0
/
jfilter.ts
166 lines (123 loc) · 3.4 KB
/
jfilter.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
import * as jsdiff from "https://cdn.skypack.dev/diff@^4.0.2";
import { red, green } from "https://deno.land/std/fmt/colors.ts";
import { Command } from "https://deno.land/x/[email protected]/command/mod.ts";
export class JFilter {
private value = "";
constructor(value?: string) {
if (value) {
this.value = value;
}
}
private reduce(text: (string | JFilter)[]) {
return text.reduce((acc, t) => acc + `(${t.toString()})`, "");
}
or(...text: (string | JFilter)[]) {
const v = this.reduce(text);
this.value += `|${v}`;
return this;
}
and(...text: (string | JFilter)[]) {
const v = this.reduce(text);
this.value += `&${v}`;
return this;
}
toString() {
return `${this.value}`;
}
}
export const diff = (actual: string, expected: string) => {
const diff = jsdiff.diffChars(actual, expected, undefined) as any[];
let match = true;
const output = diff.reduce((acc: string, part: any) => {
if (!part.value) {
return;
}
if (part.added) {
match = false;
acc += green(part.value);
} else if (part.removed) {
match = false;
acc += red(part.value);
} else {
acc += part.value;
}
return acc;
}, "");
return {
result: match,
output,
};
};
export type Action = "CREATE" | "GET" | "UPDATE" | "DELETE" | "CONNECT";
export type FilterDataValue = Record<string, string[]>;
export interface FilterData {
action?: Action | Action[];
values: FilterDataValue | FilterDataValue[];
}
const getValues = (key: string, values: string[]) => {
const res: any[] = values.map((v) => `${key} = ${v}`);
if (res.length === 1) {
return new JFilter(res[0]);
}
return new JFilter().or(...res);
};
const getActions = (actions: Action | Action[]) => {
if (!actions) {
return "";
}
let res: any[] = Array.isArray(actions) ? actions : [actions];
res = res.map((r) => `action = ${r}`);
if (res.length === 1) {
return new JFilter(res[0]);
}
return new JFilter().or(...res);
};
export const parse = (data: FilterData[]) => {
if (!Array.isArray(data)) {
console.log("Filter data is wrong format, expected array");
console.log("Use the '--nested' flag to find Jfilter values in object");
Deno.exit(1);
}
let output = data.map((value) => {
const v = value.values;
let values: JFilter[] = [];
if (Array.isArray(v)) {
const temp = v.map((w) =>
new JFilter().and(...Object.keys(w).map((k) => getValues(k, w[k])))
);
values = [new JFilter().or(...temp)];
} else {
values = Object.keys(v).map((k) => getValues(k, v[k]));
}
if (!value.action) {
if (values.length === 1) {
return values[0];
}
return new JFilter().and(...values);
}
const actions = getActions(value.action);
return new JFilter().and(actions, ...values);
});
if (output.length === 1) {
return output[0].toString();
}
return new JFilter().or(...output).toString();
};
const SUPPORTED_KEYS = ["ogit/Auth/vertexRule", "ogit/Auth/edgeRule"];
export const parseNested = (data: any): any => {
if (Array.isArray(data)) {
return parse(data);
}
for (const key in data) {
if (typeof data[key] !== "object") {
continue;
}
if (!Array.isArray(data[key])) {
data[key] = parseNested(data[key]);
}
if (SUPPORTED_KEYS.includes(key)) {
data[key] = parse(data[key]);
}
}
return data;
};