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

146 lines
2.9 KiB
Markdown

7 years ago
<img src="https://user-images.githubusercontent.com/141232/35405308-4b41f446-0238-11e8-86c1-21f407cc8460.png" height="100" alt="fx">
# [![Build Status](https://travis-ci.org/antonmedv/fx.svg?branch=master)](https://travis-ci.org/antonmedv/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
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.
```
$ fx [code ...]
```
Pretty print JSON without passing any arguments:
7 years ago
```
$ echo '{"key":"value"}' | fx
{
"key": "value"
}
```
### 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'
7 years ago
"value"
```
### This 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'
7 years ago
"value"
```
### 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'
7 years ago
"value"
```
### 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"
]
```
```
$ echo '["a", "b"]' | fx 'yield* this; yield "c";'
7 years ago
[
"a",
"b",
"c"
]
```
### Update
7 years ago
You can update existing JSON using spread operator:
```
$ echo '{"count": 0}' | fx '{...this, count: 1}'
7 years ago
{
"count": 1
}
```
### Use npm package
Use any npm package by installing it globally:
```
$ npm install -g lodash
$ cat package.json | fx 'require("lodash").keys(this.dependencies)'
```
### 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
[
"cardinal",
"get-stdin",
"meow"
]
```
7 years ago
## Related
* [jq](https://github.com/stedolan/jq) cli JSON processor on C
* [jsawk](https://github.com/micha/jsawk) like awk, but for JSON
* [json](https://github.com/trentm/json) another JSON manipulating cli library
7 years ago
* [jl](https://github.com/chrisdone/jl) functional sed for JSON on Haskell
* [ymlx](https://github.com/matthewadams/ymlx) - `fx`-like YAML cli processor
7 years ago
## License
MIT