Skip to content

Commit

Permalink
Add support for query languages
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Dec 25, 2019
1 parent 290c2a0 commit de3d60b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
27 changes: 27 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
+ [Edit-in-place](#edit-in-place)
+ [Using packages](#using-packages)
* [Using .fxrc](#using-fxrc)
+ [Query language](#query-language)
* [Formatting](#formatting)
* [Other examples](#other-examples)
* [Streaming mode](#streaming-mode)
Expand Down Expand Up @@ -130,6 +131,32 @@ curl 'https://api.github.com/repos/facebook/react/commits?per_page=100' \
> export NODE_PATH=`npm root -g`
> ```
### Query language
If you want to use query language, for example [jsonata](http://jsonata.org/) you can use helper function like this:
```js
global.jsonata = expr => require('jsonata')(expr).evaluate
```
And use it like this:

```bash
curl ... | fx 'jsonata("$sum(Order.Product.(Price * Quantity))")'
```

Instead you can create next alias in _.bashrc_ file:

```bash
alias jsonata='FX_APPLY=jsonata fx'
```

And now all code arguments to `jsonata` will be passed through `jsonata` helper. And now you can use it like this:

```bash
curl ... | jsonata '$sum(Order.Product.(Price * Quantity))'
```

## Formatting

If you need output other than JSON (for example arguments for xargs), do not return anything from the reducer.
Expand Down
4 changes: 4 additions & 0 deletions reduce.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use strict'

function reduce(json, code) {
if (process.env.FX_APPLY) {
return global[process.env.FX_APPLY](code)(json)
}

if ('.' === code) {
return json
}
Expand Down

0 comments on commit de3d60b

Please sign in to comment.