Skip to content

Commit ee76dfe

Browse files
committed
feat: support multiple operators without parenthesis
1 parent 09b7cb6 commit ee76dfe

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

src/parse.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,23 @@ export function parse(query: string, options?: JSONQueryParseOptions): JSONQuery
5151
}
5252

5353
const parseOperator = () => {
54-
const left = parseParenthesis()
54+
let left = parseParenthesis()
5555

5656
skipWhitespace()
5757

5858
// we sort the operators from longest to shortest, so we first handle "<=" and next "<"
5959
for (const name of sortedOperatorNames) {
6060
const op = allOperators[name]
61-
if (query.substring(i, i + op.length) === op) {
61+
while (query.substring(i, i + op.length) === op) {
6262
i += op.length
63+
6364
skipWhitespace()
65+
6466
const right = parseParenthesis()
6567

66-
return [name, left, right]
68+
left = [name, left, right]
69+
70+
skipWhitespace()
6771
}
6872
}
6973

test-suite/parse.test.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,19 @@
163163
},
164164
{
165165
"category": "operator",
166-
"description": "should throw an error when using multiple operators without parenthesis",
166+
"description": "should support multiple operators without parenthesis",
167167
"tests": [
168-
{ "input": ".a == \"A\" and .b == \"B\"", "throws": "Unexpected part 'and .b == \"B\"'" },
169168
{
170169
"input": "2 and 3 and 4",
171-
"throws": "Unexpected part 'and 4' (pos: 8)"
172-
},
170+
"output": ["and", ["and", 2, 3], 4]
171+
}
172+
]
173+
},
174+
{
175+
"category": "operator",
176+
"description": "should throw an error when using multiple operators without parenthesis",
177+
"tests": [
178+
{ "input": ".a == \"A\" and .b == \"B\"", "throws": "Unexpected part 'and .b == \"B\"'" },
173179
{ "input": ".a + 2 * 3", "throws": "Unexpected part '* 3' (pos: 7)" }
174180
]
175181
},

0 commit comments

Comments
 (0)