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 inclusion operator for strings #83

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 17 additions & 3 deletions src/query/QueryEvaluator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ type scenario = {
describe('QueryEvaluator', () => {
const evaluator = new QueryEvaluator();

it('throw on unknown literal', () => {
it('skip on unknown literal', () => {
const parsedQuery = evaluator.parse('unknown @> "my-service"');

expect(() => {evaluator.evaluate(parsedQuery, {})}).toThrow('identifier unknown does not exist');
expect(evaluator.evaluate(parsedQuery, {})).toBe(false)
});

it('throw on unknown operator', () => {
Expand Down Expand Up @@ -95,6 +94,21 @@ describe('QueryEvaluator', () => {
{ context: { tags: ['foo', 'baz'] }, expectedResult: true },
],
},
{
query: 'folderTitle @> "foo"',
examples: [
{ context: { folderTitle: 'test' }, expectedResult: false },
{ context: { folderTitle: 'foobar' }, expectedResult: true },
]
},
{
query: 'folderTitle @> "foo" && tags @> "bar"',
examples: [
{ context: { folderTitle: 'test' }, expectedResult: false },
{ context: { folderTitle: 'foobar' }, expectedResult: false },
{ context: { folderTitle: 'foobar', tags: ['foo', 'bar', 'baz'] }, expectedResult: true },
]
}
];

testCases.forEach(testCase => {
Expand Down
13 changes: 2 additions & 11 deletions src/query/QueryEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ import jsep from 'jsep';

export type EvaluationContext = Record<string, any>;

const includes = (haystack: any, needle: any): boolean => {
if (!Array.isArray(haystack)) {
throw Error(`@> operator can only be used on an array`);
}

return (haystack as any[]).includes(needle);
};
const includes = (haystack: string | string[], needle: string): boolean => haystack.includes(needle);

export class QueryEvaluator {
constructor() {
Expand All @@ -43,10 +37,7 @@ export class QueryEvaluator {
case 'BinaryExpression':
return this.evaluateBinaryExpression(root as jsep.BinaryExpression, context);
case 'Identifier':
if (!context.hasOwnProperty((root as jsep.Identifier).name)) {
throw Error(`identifier ${(root as jsep.Identifier).name} does not exist`);
}

if (!context.hasOwnProperty((root as jsep.Identifier).name)) return ''
return context[(root as jsep.Identifier).name];
case 'Literal':
return (root as jsep.Literal).value;
Expand Down