You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fx/README.md

212 lines
5.0 KiB
Markdown

<p align="center"><img src="https://user-images.githubusercontent.com/141232/35405308-4b41f446-0238-11e8-86c1-21f407cc8460.png" height="100" alt="fx"></p>
<p align="center"><img src="https://user-images.githubusercontent.com/141232/47933350-f0f22900-df06-11e8-9cf2-88492c1be774.gif" width="530" alt="fx example"></p>
_* Function eXecution_
7 years ago
[![Build Status](https://travis-ci.org/antonmedv/fx.svg?branch=master)](https://travis-ci.org/antonmedv/fx)
[![npm version](https://badgen.net/npm/v/fx)](https://www.npmjs.com/package/fx)
7 years ago
Command-line JSON processing tool
## Features
* Don't need to learn new syntax
* Plain JavaScript
* Formatting and highlighting
6 years ago
* Standalone binary
* Interactive mode 🎉
7 years ago
## Install
```
$ npm install -g fx
```
7 years ago
Or download standalone binary from [releases](https://github.com/antonmedv/fx/releases) page.
7 years ago
## Usage
Pipe into `fx` any JSON and anonymous function for reducing it.
```
$ echo '{...}' | fx [code ...]
7 years ago
```
Start interactive mode without passing any arguments:
7 years ago
```
$ curl ... | fx
7 years ago
```
### Anonymous function
Use an anonymous function as reducer which gets JSON and processes it:
7 years ago
```
$ echo '{"foo": [{"bar": "value"}]}' | fx 'x => x.foo[0].bar'
6 years ago
value
7 years ago
```
### Binding
7 years ago
If you don't pass anonymous function `param => ...`, code will be automatically transformed into anonymous function.
And you can get access to JSON by `this` keyword:
```
$ echo '{"foo": [{"bar": "value"}]}' | fx 'this.foo[0].bar'
6 years ago
value
```
### Dot
It is possible to omit `this` keyword:
```
$ echo '{"foo": [{"bar": "value"}]}' | fx .foo[0].bar
value
7 years ago
```
If single dot is passed, JSON will be processed without modification:
```
$ echo '{"foo": "bar"}' | fx .
{
"foo": "bar"
}
```
### Chain
7 years ago
You can pass any number of anonymous functions for reducing JSON:
```
$ echo '{"foo": [{"bar": "value"}]}' | fx 'x => x.foo' 'this[0]' 'this.bar'
6 years ago
value
7 years ago
```
### Generator
7 years ago
If passed code contains `yield` keyword, [generator expression](https://github.com/sebmarkbage/ecmascript-generator-expression)
will be used:
```
$ curl ... | fx 'for (let user of this) if (user.login.startsWith("a")) yield user'
7 years ago
```
Access to JSON through `this` keyword:
```
$ echo '["a", "b"]' | fx 'yield* this'
7 years ago
[
"a",
"b"
7 years ago
]
```
```
$ echo '["a", "b"]' | fx 'yield* this; yield "c";'
7 years ago
[
"a",
"b",
"c"
7 years ago
]
```
### Update
7 years ago
You can update existing JSON using spread operator:
```
$ echo '{"count": 0}' | fx '{...this, count: 1}'
7 years ago
{
"count": 1
7 years ago
}
```
### Using packages
Use any npm package by installing it globally:
```
$ npm install -g lodash
$ cat package.json | fx 'require("lodash").keys(this.dependencies)'
```
### Using .fxrc
Create _.fxrc_ file in `$HOME` directory, and require any packages or define global functions.
For example, access all lodash methods without `_` prefix. Put in your `.fxrc` file:
```js
Object.assign(global, require('lodash'))
```
And now you will be able to call all lodash methods. For example, see who's been committing to react recently:
```
curl 'https://api.github.com/repos/facebook/react/commits?per_page=100' \
| fx 'mapValues(groupBy(this, "commit.committer.name"), size)'
```
> To be able require global modules make sure you have correct `NODE_PATH` env variable.
> ```bash
> export NODE_PATH=/usr/local/lib/node_modules
> ```
### Formatting
If you need something different then JSON (for example arguments for xargs) do not return anything from reducer.
7 years ago
`undefined` value printed into stderr by default.
```
echo '[]' | fx 'void 0'
undefined
```
```
echo '[1,2,3]' | fx 'this.forEach(x => console.log(x))' 2>/dev/null | xargs echo
1 2 3
```
### Other examples
7 years ago
Convert object to array:
```
$ cat package.json | fx 'Object.keys(this.dependencies)'
7 years ago
[
"@medv/prettyjson"
7 years ago
]
```
6 years ago
By the way, fx has shortcut for `Object.keys(this)`. Previous example can be rewritten as:
```
$ cat package.json | fx this.dependencies ?
```
### Interactive mode
Start interactive mode without passing any arguments:
```
$ curl ... | fx
```
6 years ago
Click on fields to expand or collapse JSON tree, use mouse wheel to scroll view.
Next commands available in interactive mode:
6 years ago
| Key | Command |
|----------------------------|-------------------------|
| `q` or `Esc` or `Ctrl`+`c` | Exit |
| `e`/`E` | Expand/Collapse all |
| `up`/`down` or `k`/`j` | Scroll up/down one line |
| `g`/`G` | Goto top/bottom |
#### Selecting text
You may found what you can't just select text in fx. This is due the fact that all mouse events redirected to stdin. To be able select again you need instruct your terminal not to do it. This can be done by holding special keys while selecting:
6 years ago
| Key | Terminal |
|------------------|---------------|
| `Option`+`Mouse` | iTerm2, Hyper |
| `Fn`+`Mouse` | Terminal.app |
6 years ago
| `Shift`+`Mouse` | Linux |
7 years ago
## Related
6 years ago
* [xx](https://github.com/antonmedv/xx) - fx-like JSON tool (*go*)
6 years ago
* [ymlx](https://github.com/matthewadams/ymlx) - fx-like YAML cli processor
7 years ago
## License
[MIT](https://github.com/antonmedv/fx/blob/master/LICENSE)