From 31c3ed3e2abac678e65f98e5ecf7fd4fe8b14343 Mon Sep 17 00:00:00 2001 From: shaneshen-okta Date: Wed, 17 Aug 2022 15:14:14 -0400 Subject: [PATCH 1/4] add two test cases --- test/stringify.test.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/stringify.test.ts b/test/stringify.test.ts index 7286449..f46b686 100644 --- a/test/stringify.test.ts +++ b/test/stringify.test.ts @@ -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") + ) + }, + ), + ) + ); }); }); From eb6823216c26a380c0a30f3e62545e5d291e3a48 Mon Sep 17 00:00:00 2001 From: shaneshen-okta Date: Wed, 17 Aug 2022 15:16:38 -0400 Subject: [PATCH 2/4] fix stringfy --- src/stringify.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/stringify.ts b/src/stringify.ts index 70aae0b..23c78b2 100644 --- a/src/stringify.ts +++ b/src/stringify.ts @@ -18,14 +18,14 @@ 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} `); + const filtersAsString = f.filters.map(filter => stringify(filter)).join(` ${f.op} `); returnValue = `(${filtersAsString})`; break; case "and": returnValue = f.filters.map(filter => stringify(filter, false)).join(` ${f.op} `); - break; + return returnValue; case "not": - returnValue = `${f.op} (${stringify(f.filter, true)})`; + returnValue = `${f.op} (${stringify(f.filter)})`; break; case "[]": returnValue = `${f.attrPath}[${stringify(f.valFilter)}]`; From 4eea982fd00ad929cfa6614c61aee31947811d92 Mon Sep 17 00:00:00 2001 From: shaneshen-okta Date: Thu, 18 Aug 2022 07:26:08 -0400 Subject: [PATCH 3/4] optimize stringfy --- src/stringify.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/stringify.ts b/src/stringify.ts index 23c78b2..cb606e4 100644 --- a/src/stringify.ts +++ b/src/stringify.ts @@ -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": @@ -19,11 +19,11 @@ export function stringify(f: Filter, trimParens = true): string { break; case "or": const filtersAsString = f.filters.map(filter => stringify(filter)).join(` ${f.op} `); - returnValue = `(${filtersAsString})`; + returnValue = wrapOr ? `(${filtersAsString})` : filtersAsString; break; case "and": - returnValue = f.filters.map(filter => stringify(filter, false)).join(` ${f.op} `); - return returnValue; + returnValue = f.filters.map(filter => stringify(filter, true)).join(` ${f.op} `); + break; case "not": returnValue = `${f.op} (${stringify(f.filter)})`; break; @@ -31,8 +31,6 @@ export function stringify(f: Filter, trimParens = true): string { returnValue = `${f.attrPath}[${stringify(f.valFilter)}]`; break; } - if (trimParens) { - returnValue = returnValue.replace(/(^\()(.*)(\)$)/, '$2'); - } + return returnValue; } From 22481f99ee53da903da683bca0483e59c2b3357a Mon Sep 17 00:00:00 2001 From: shaneshen-okta Date: Thu, 18 Aug 2022 07:32:13 -0400 Subject: [PATCH 4/4] add same test cases to parse --- test/parse.test.ts | 73 +++++++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 20 deletions(-) diff --git a/test/parse.test.ts b/test/parse.test.ts index 65d899e..f94ad1b 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -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( @@ -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"))