Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix stringfy #92

Merged
merged 4 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/stringify.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Filter } from ".";

export function stringify(f: Filter, trimParens = true): string {
export function stringify(f: Filter, wrapOr = false): string {
let returnValue = '';
switch (f.op) {
case "eq":
Expand All @@ -18,21 +18,19 @@ export function stringify(f: Filter, trimParens = true): string {
returnValue = `${f.attrPath} ${f.op}`;
break;
case "or":
const filtersAsString = f.filters.map(filter => stringify(filter, false)).join(` ${f.op} `);
returnValue = `(${filtersAsString})`;
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, false)).join(` ${f.op} `);
returnValue = f.filters.map(filter => stringify(filter, true)).join(` ${f.op} `);
break;
case "not":
returnValue = `${f.op} (${stringify(f.filter, true)})`;
returnValue = `${f.op} (${stringify(f.filter)})`;
break;
case "[]":
returnValue = `${f.attrPath}[${stringify(f.valFilter)}]`;
break;
}
if (trimParens) {
returnValue = returnValue.replace(/(^\()(.*)(\)$)/, '$2');
}

return returnValue;
}
73 changes: 53 additions & 20 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,25 +158,10 @@ describe('parse', () => {
op("ne", "userType", "Employee")
)
);
test(
`userType eq "Employee" and not (emails co "example.com" or emails co "example.org") and userType ne "Employee"`,
and(
op("eq", "userType", "Employee"),
{
op: "not",
filter: or(
op("co", "emails", "example.com"),
op("co", "emails", "example.org")
)
},
op("ne", "userType", "Employee")
)
);
test(
`userType eq "Employee" or not (emails co "example.com" or emails co "example.org") and userType ne "Employee"`,
or(
op("eq", "userType", "Employee"),
test(
`userType eq "Employee" and not (emails co "example.com" or emails co "example.org") and userType ne "Employee"`,
and(
op("eq", "userType", "Employee"),
{
op: "not",
filter: or(
Expand All @@ -186,8 +171,56 @@ describe('parse', () => {
},
op("ne", "userType", "Employee")
)
)
);
);
test(
`userType eq "Employee" or not (emails co "example.com" or emails co "example.org") and userType ne "Employee"`,
or(
op("eq", "userType", "Employee"),
and(
{
op: "not",
filter: or(
op("co", "emails", "example.com"),
op("co", "emails", "example.org")
)
},
op("ne", "userType", "Employee")
)
)
);
test(
`(userType eq "Employee" or userType eq "Employer") and emails sw "foo" and not (emails co "example.com" or emails co "example.org")`,
and(
or(
op("eq", "userType", "Employee"),
op("eq", "userType", "Employer"),
),
op("sw", "emails", "foo"),
{
op: "not",
filter: or(
op("co", "emails", "example.com"),
op("co", "emails", "example.org")
)
},
)
);
test(
`userType eq "Employee" and (emails sw "foo" or not (emails co "example.com" or emails co "example.org"))`,
and(
op("eq", "userType", "Employee"),
or(
op("sw", "emails", "foo"),
{
op: "not",
filter: or(
op("co", "emails", "example.com"),
op("co", "emails", "example.org")
)
},
),
)
);
test(
`userType eq "Employee" and (emails.type eq "work")`,
and(eq("userType", "Employee"), eq("emails.type", "work"))
Expand Down
33 changes: 33 additions & 0 deletions test/stringify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,5 +219,38 @@ describe('stringify', () => {
)
)
);
test(
`(userType eq "Employee" or userType eq "Employer") and emails sw "foo" and not (emails co "example.com" or emails co "example.org")`,
and(
or(
op("eq", "userType", "Employee"),
op("eq", "userType", "Employer"),
),
op("sw", "emails", "foo"),
{
op: "not",
filter: or(
op("co", "emails", "example.com"),
op("co", "emails", "example.org")
)
},
)
);
test(
`userType eq "Employee" and (emails sw "foo" or not (emails co "example.com" or emails co "example.org"))`,
and(
op("eq", "userType", "Employee"),
or(
op("sw", "emails", "foo"),
{
op: "not",
filter: or(
op("co", "emails", "example.com"),
op("co", "emails", "example.org")
)
},
),
)
);
});
});