Skip to content

Commit

Permalink
Add support for ParenthesizedExpression in filter function
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMa committed Feb 14, 2023
1 parent 5bf2b5f commit fabcd75
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/internalFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,16 @@ export const internalFilter = <T extends Object>(
});
}

if (ast.type === 'ParenthesizedExpression') {
return internalFilter(
ast.expression,
rows,
resultFast,
path,
highlights,
);
}

if (!ast.left) {
throw new Error('Expected left to be defined.');
}
Expand Down
24 changes: 20 additions & 4 deletions test/liqe/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ const persons: readonly Person[] = [
phoneNumber: '404-050-2611',
subscribed: true,
},
{
height: 150,
name: 'foo bar',
nick: 'old dog',
},
{
height: 194,
name: 'fox',
nick: 'quick fox',
},
];

const testQuery = test.macro((t, expectedResultNames: string[]) => {
Expand Down Expand Up @@ -90,14 +100,14 @@ test('height:[200 TO 225]', testQuery, ['robert', 'noah']);
test('height:[200 TO 225}', testQuery, ['robert']);
test('height:{220 TO 225}', testQuery, []);

test('NOT David', testQuery, ['john', 'mike', 'robert', 'noah']);
test('-David', testQuery, ['john', 'mike', 'robert', 'noah']);
test('NOT David', testQuery, ['john', 'mike', 'robert', 'noah', 'foo bar', 'fox']);
test('-David', testQuery, ['john', 'mike', 'robert', 'noah', 'foo bar', 'fox']);
test('David OR John', testQuery, ['david', 'john', 'noah']);
test('Noah AND John', testQuery, ['noah']);
test('John AND NOT Noah', testQuery, ['john']);
test('David OR NOT John', testQuery, ['david', 'mike', 'robert']);
test('David OR NOT John', testQuery, ['david', 'mike', 'robert', 'foo bar', 'fox']);
test('John AND -Noah', testQuery, ['john']);
test('David OR -John', testQuery, ['david', 'mike', 'robert']);
test('David OR -John', testQuery, ['david', 'mike', 'robert', 'foo bar', 'fox']);

test('name:David OR John', testQuery, ['david', 'john', 'noah']);

Expand Down Expand Up @@ -131,3 +141,9 @@ test('phoneNumber:"404-050-2611"', testQuery, ['noah']);
test('phoneNumber:404', testQuery, ['noah']);

test('balance:364', testQuery, ['noah']);

test('(David)', testQuery, ['david']);
test('(name:david OR name:john)', testQuery, ['david', 'john']);
test('(name:"foo bar" AND nick:"quick fox") OR name:fox', testQuery, ['fox']);
test('(name:fox OR name:"foo bar" AND nick:"old dog")', testQuery, ['foo bar']);
test('(name:fox OR (name:"foo bar" AND nick:"old dog"))', testQuery, ['fox', 'foo bar']);

0 comments on commit fabcd75

Please sign in to comment.