Skip to content

Commit 4d828b9

Browse files
authored
Account for potential not in conditional in custom query (#3081)
1 parent 8537bf4 commit 4d828b9

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

src/lib/utilities/is.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const conditionals = [
6060
'is',
6161
'is not',
6262
'in',
63+
'not in',
6364
] as const;
6465

6566
const joins = ['and', 'or'] as const;
@@ -189,5 +190,5 @@ export const isStartsWith = (x: unknown) => {
189190
export const isInConditional = (x: unknown) => {
190191
if (!isString(x)) return false;
191192

192-
return x.toLocaleLowerCase() === 'in';
193+
return ['in', 'not in'].includes(x.toLocaleLowerCase());
193194
};

src/lib/utilities/query/tokenize.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const combinedQuery =
1111
'`WorkflowId`="Hello" and `WorkflowType`="World" and `StartTime` BETWEEN "2022-04-18T18:09:49-06:00" AND "2022-04-20T18:09:49-06:00"';
1212
const valuesWithSpacesQuery =
1313
'`Custom Key Word`="Hello there world" AND `WorkflowId`="one and two = three" OR `WorkflowType`="example=\'one\'"';
14-
const keywordListQuery = '`CustomKeywordListField`in("Hello", "World")';
1514
const startsWithQuery = '`WorkflowType` STARTS_WITH "Hello"';
1615

1716
describe('tokenize', () => {
@@ -106,14 +105,21 @@ describe('tokenize', () => {
106105
]);
107106
});
108107

109-
it('should tokenize the keywordListQuery', () => {
110-
const query = keywordListQuery;
108+
it('should tokenize KeywordList queries', () => {
109+
const keywordListInQuery = '`CustomKeywordListField`in("Hello", "World")';
110+
const keywordListNotInQuery =
111+
'`CustomKeywordListField`not in("Hello", "World")';
111112

112-
expect(tokenize(query)).toEqual([
113+
expect(tokenize(keywordListInQuery)).toEqual([
113114
'CustomKeywordListField',
114115
'in',
115116
'("Hello", "World")',
116117
]);
118+
expect(tokenize(keywordListNotInQuery)).toEqual([
119+
'CustomKeywordListField',
120+
'not in',
121+
'("Hello", "World")',
122+
]);
117123
});
118124

119125
it('should tokenize with "`" not used in custom attributes with spaces', () => {

src/lib/utilities/query/tokenize.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,24 +72,29 @@ export const tokenize = (string: string): Tokens => {
7272
}
7373
}
7474

75-
// Conditional can be up to three characters long (!==)
76-
const midConditional = `${string[cursor]}${string[cursor + 1]}`;
77-
const maxConditional = `${string[cursor]}${string[cursor + 1]}${
78-
string[cursor + 2]
79-
}`;
75+
// Check for conditionals that are two, three or six characters long (e.g. not in)
76+
const minConditional = string.slice(cursor, cursor + 2);
77+
const midConditional = string.slice(cursor, cursor + 3);
78+
const maxConditional = string.slice(cursor, cursor + 6);
79+
8080
if (isConditional(maxConditional)) {
8181
buffer += maxConditional;
8282
addBufferToTokens();
83+
cursor += 6;
84+
continue;
85+
} else if (isConditional(midConditional)) {
86+
buffer += midConditional;
87+
addBufferToTokens();
8388
cursor += 3;
8489
continue;
8590
} else if (
86-
isConditional(midConditional) &&
91+
isConditional(minConditional) &&
8792
(isSpace(string[cursor + 2]) ||
8893
isQuote(string[cursor + 2]) ||
8994
isParenthesis(string[cursor + 2]))
9095
) {
9196
// To prevent false positives like "inspect" being a "in" conditional, check for space, quote, or parenthesis after the midConditional
92-
buffer += midConditional;
97+
buffer += minConditional;
9398
addBufferToTokens();
9499
cursor += 2;
95100
continue;

0 commit comments

Comments
 (0)