Skip to content

Commit 916e8a3

Browse files
fix stringfy (thomaspoignant#92)
* add two test cases * fix stringfy * optimize stringfy * add same test cases to parse
1 parent 5e20db9 commit 916e8a3

File tree

3 files changed

+92
-28
lines changed

3 files changed

+92
-28
lines changed

src/stringify.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Filter } from ".";
22

3-
export function stringify(f: Filter, trimParens = true): string {
3+
export function stringify(f: Filter, wrapOr = false): string {
44
let returnValue = '';
55
switch (f.op) {
66
case "eq":
@@ -18,21 +18,19 @@ export function stringify(f: Filter, trimParens = true): string {
1818
returnValue = `${f.attrPath} ${f.op}`;
1919
break;
2020
case "or":
21-
const filtersAsString = f.filters.map(filter => stringify(filter, false)).join(` ${f.op} `);
22-
returnValue = `(${filtersAsString})`;
21+
const filtersAsString = f.filters.map(filter => stringify(filter)).join(` ${f.op} `);
22+
returnValue = wrapOr ? `(${filtersAsString})` : filtersAsString;
2323
break;
2424
case "and":
25-
returnValue = f.filters.map(filter => stringify(filter, false)).join(` ${f.op} `);
25+
returnValue = f.filters.map(filter => stringify(filter, true)).join(` ${f.op} `);
2626
break;
2727
case "not":
28-
returnValue = `${f.op} (${stringify(f.filter, true)})`;
28+
returnValue = `${f.op} (${stringify(f.filter)})`;
2929
break;
3030
case "[]":
3131
returnValue = `${f.attrPath}[${stringify(f.valFilter)}]`;
3232
break;
3333
}
34-
if (trimParens) {
35-
returnValue = returnValue.replace(/(^\()(.*)(\)$)/, '$2');
36-
}
34+
3735
return returnValue;
3836
}

test/parse.test.ts

+53-20
Original file line numberDiff line numberDiff line change
@@ -158,25 +158,10 @@ describe('parse', () => {
158158
op("ne", "userType", "Employee")
159159
)
160160
);
161-
test(
162-
`userType eq "Employee" and not (emails co "example.com" or emails co "example.org") and userType ne "Employee"`,
163-
and(
164-
op("eq", "userType", "Employee"),
165-
{
166-
op: "not",
167-
filter: or(
168-
op("co", "emails", "example.com"),
169-
op("co", "emails", "example.org")
170-
)
171-
},
172-
op("ne", "userType", "Employee")
173-
)
174-
);
175-
test(
176-
`userType eq "Employee" or not (emails co "example.com" or emails co "example.org") and userType ne "Employee"`,
177-
or(
178-
op("eq", "userType", "Employee"),
161+
test(
162+
`userType eq "Employee" and not (emails co "example.com" or emails co "example.org") and userType ne "Employee"`,
179163
and(
164+
op("eq", "userType", "Employee"),
180165
{
181166
op: "not",
182167
filter: or(
@@ -186,8 +171,56 @@ describe('parse', () => {
186171
},
187172
op("ne", "userType", "Employee")
188173
)
189-
)
190-
);
174+
);
175+
test(
176+
`userType eq "Employee" or not (emails co "example.com" or emails co "example.org") and userType ne "Employee"`,
177+
or(
178+
op("eq", "userType", "Employee"),
179+
and(
180+
{
181+
op: "not",
182+
filter: or(
183+
op("co", "emails", "example.com"),
184+
op("co", "emails", "example.org")
185+
)
186+
},
187+
op("ne", "userType", "Employee")
188+
)
189+
)
190+
);
191+
test(
192+
`(userType eq "Employee" or userType eq "Employer") and emails sw "foo" and not (emails co "example.com" or emails co "example.org")`,
193+
and(
194+
or(
195+
op("eq", "userType", "Employee"),
196+
op("eq", "userType", "Employer"),
197+
),
198+
op("sw", "emails", "foo"),
199+
{
200+
op: "not",
201+
filter: or(
202+
op("co", "emails", "example.com"),
203+
op("co", "emails", "example.org")
204+
)
205+
},
206+
)
207+
);
208+
test(
209+
`userType eq "Employee" and (emails sw "foo" or not (emails co "example.com" or emails co "example.org"))`,
210+
and(
211+
op("eq", "userType", "Employee"),
212+
or(
213+
op("sw", "emails", "foo"),
214+
{
215+
op: "not",
216+
filter: or(
217+
op("co", "emails", "example.com"),
218+
op("co", "emails", "example.org")
219+
)
220+
},
221+
),
222+
)
223+
);
191224
test(
192225
`userType eq "Employee" and (emails.type eq "work")`,
193226
and(eq("userType", "Employee"), eq("emails.type", "work"))

test/stringify.test.ts

+33
Original file line numberDiff line numberDiff line change
@@ -219,5 +219,38 @@ describe('stringify', () => {
219219
)
220220
)
221221
);
222+
test(
223+
`(userType eq "Employee" or userType eq "Employer") and emails sw "foo" and not (emails co "example.com" or emails co "example.org")`,
224+
and(
225+
or(
226+
op("eq", "userType", "Employee"),
227+
op("eq", "userType", "Employer"),
228+
),
229+
op("sw", "emails", "foo"),
230+
{
231+
op: "not",
232+
filter: or(
233+
op("co", "emails", "example.com"),
234+
op("co", "emails", "example.org")
235+
)
236+
},
237+
)
238+
);
239+
test(
240+
`userType eq "Employee" and (emails sw "foo" or not (emails co "example.com" or emails co "example.org"))`,
241+
and(
242+
op("eq", "userType", "Employee"),
243+
or(
244+
op("sw", "emails", "foo"),
245+
{
246+
op: "not",
247+
filter: or(
248+
op("co", "emails", "example.com"),
249+
op("co", "emails", "example.org")
250+
)
251+
},
252+
),
253+
)
254+
);
222255
});
223256
});

0 commit comments

Comments
 (0)