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
-`data` is the JSON document that will be queried, often an array with objects.
370
369
-`query` is a JSON document containing a JSON query, either the text format or the parsed JSON format.
371
370
-`options` is an optional object that can contain the following properties:
372
-
-`functions` is an optional map with function creators. A function creator has optional arguments as input and must return a function that can be used to process the query data. For example:
371
+
-`functions` is an optional map with custom function creators. A function creator has optional arguments as input and must return a function that can be used to process the query data. For example:
Note that configuring the option `functions` will overwrite the default functions. In order to extend the existing functions it is necessary to import the build-in functions and extend the custom `functions` object with themasin the example above.
390
381
391
382
If the parameters are not a static value but can be a query themselves, the function `compile` can be used to compile them. For example, the actual implementation of the function `filter` is the following:
392
383
393
384
```js
394
-
import { functions } from '@jsonquerylang/jsonquery'
395
-
396
385
const options = {
397
386
functions: {
398
-
// keep all built-in functions
399
-
...functions,
400
-
401
-
// overwrite function "filter" with our own implementation
402
387
// usage example: 'filter(.age > 20)'
403
388
filter: (predicate) => {
404
389
const_predicate=compile(predicate)
@@ -410,44 +395,36 @@ Here:
410
395
411
396
You can have a look at the source code of the functions in`/src/functions.ts`for more examples.
412
397
413
-
-`operators` is an optional array with maps of operators having the same precedence, ordered from highest to lowest precedence. The default array with operators is the following, with the precedence going from lowest to highest:
-`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`):
426
399
427
-
When extending the built-in operators with a custom operator, the built-in operators can be imported via `import { operators } from '@jsonquerylang/jsonquery'` and then extended by mapping over them and adding the custom operator to the group with the right precedence level (see example below), or adding a newprecedence level if needed.
400
+
```ts
401
+
type CustomOperator =
402
+
| { name: string; op: string; at: string }
403
+
| { name: string; op: string; after: string }
404
+
| { name: string; op: string; before: string }
405
+
```
428
406
429
407
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:
430
408
431
409
```js
432
-
import { buildFunction, functions, operators } from '@jsonquerylang/jsonquery'
410
+
import { buildFunction } from '@jsonquerylang/jsonquery'
433
411
434
412
const options = {
435
413
// Define a new function "notEqual".
436
414
functions: {
437
-
...functions,
438
415
notEqual: buildFunction((a, b) =>a!==b)
439
416
},
440
417
441
418
// Define a new operator "<>" which maps to the function "notEqual"
442
419
// and has the same precedence as operator "==".
443
-
operators:operators.map((group) => {
444
-
returnObject.values(group).includes('==')
445
-
? { ...group, notEqual:'<>' }
446
-
: group
447
-
})
420
+
operators: [
421
+
{ name:'aboutEq', op:'~=', at:'==' }
422
+
]
448
423
}
449
424
```
450
425
426
+
The build-in operators can be found in the source code: [/src/operators.ts](https://github.com/jsonquerylang/jsonquery/blob/develop/src/operators.ts).
427
+
451
428
Here an example of using the function `jsonquery`:
452
429
453
430
```js
@@ -537,12 +514,8 @@ The function `buildFunction` is a helper function to create a custom function. I
537
514
The query engine passes the raw arguments to all functions, and the functions have to compile the arguments themselves when they are dynamic. For example:
0 commit comments