You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: implement support for operator precedence (#17)
BREAKING CHANGE:
The `operators` option changes from an object like `{ aboutEq: '~=', ... }` into an array with custom operators, having a name and precedence, like `[{ name: 'aboutEq', op: '~=', at: '==', vararg: false, leftAssociative: true }, ...]`.
Copy file name to clipboardExpand all lines: LICENSE.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
The ISC License
2
2
3
-
Copyright (c) 2024 by Jos de Jong
3
+
Copyright (c) 2024-2025 by Jos de Jong
4
4
5
5
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
Copy file name to clipboardExpand all lines: README.md
+29-15Lines changed: 29 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ Try it out on the online playground: <https://jsonquerylang.org>
10
10
11
11
## Features
12
12
13
-
- Small: just `3.3 kB` when minified and gzipped! The JSON query engine without parse/stringify is only `1.7 kB`.
13
+
- Small: just `3.7 kB` when minified and gzipped! The JSON query engine without parse/stringify is only `1.7 kB`.
14
14
- Feature rich (50+ powerful functions and operators)
15
15
- Easy to interoperate with thanks to the intermediate JSON format.
16
16
- Expressive
@@ -152,22 +152,42 @@ Here:
152
152
}
153
153
```
154
154
155
-
You can have a look at the source code of the functions in`/src/functions.ts`for more examples.
156
-
-`operators` is an optional map with operators, for example `{ eq: '==' }`. The defined operators can be used in a text query. Only operators with both a left and right hand side are supported, like `a == b`. They can only be executed when there is a corresponding function. For example:
155
+
You can have a look at the source code of the functions in [`/src/functions.ts`](/src/functions.ts) for more examples.
156
+
157
+
-`operators` is an optional array definitions for custom operators. Each definition describes the newoperator, the name of the function that it maps to, and the desired precedence of the operator: the same, before, or after one of the existing operators (`at`, `before`, or`after`):
The defined operators can be used in a text query. Only operators with both a left and right hand side are supported, like `a == b`. They can only be executed when there is a corresponding function. For example:
157
167
158
168
```js
159
-
import { buildFunction } from 'jsonquery'
160
-
169
+
import { buildFunction } from '@jsonquerylang/jsonquery'
170
+
161
171
const options = {
162
-
operators: {
163
-
notEqual: '<>'
164
-
},
172
+
// Define a new function "notEqual".
165
173
functions: {
166
174
notEqual: buildFunction((a, b) =>a!==b)
167
-
}
175
+
},
176
+
177
+
// Define a new operator "<>" which maps to the function "notEqual"
178
+
// and has the same precedence as operator "==".
179
+
operators: [
180
+
{ name:'aboutEq', op:'~=', at:'==' }
181
+
]
168
182
}
169
183
```
170
184
185
+
To allow using a chain of multiple operators without parenthesis, like `a and b and c`, the option `leftAssociative` can be set `true`. Withoutthis, an exception will be thrown, which can be solved by using parenthesis like `(a and b) and c`.
186
+
187
+
When the function of the operator supports more than two arguments, like `and(a, b, c, ...)`, the option `vararg` can be set `true`. In that case, a chain of operators like `a and b and c` will be parsed into the JSON Format `["and", a, b, c, ...]`. Operators that do not support variable arguments, like `1 + 2 + 3`, will be parsed into a nested JSON Format like `["add", ["add", 1, 2], 3]`.
188
+
189
+
All build-in operators and their precedence are listed on the documentation page in the section [Operators](https://jsonquerylang.org/docs/#operators).
190
+
171
191
Here an example of using the function `jsonquery`:
172
192
173
193
```js
@@ -258,9 +278,6 @@ The query engine passes the raw arguments to all functions, and the functions ha
258
278
259
279
```ts
260
280
const options = {
261
-
operators: {
262
-
notEqual: '<>'
263
-
},
264
281
functions: {
265
282
notEqual: (a:JSONQuery, b:JSONQuery) => {
266
283
const aCompiled =compile(a)
@@ -286,9 +303,6 @@ To automatically compile and evaluate the arguments of the function, the helper
0 commit comments