Use single quote in examples

This commit is contained in:
Anton Medvedev 2018-01-26 09:35:58 +07:00
parent c48a04560c
commit 97664caa65
2 changed files with 34 additions and 20 deletions

View File

@ -24,7 +24,7 @@ Pipe into `fx` any JSON and anonymous function for reducing it.
$ fx [code ...] $ fx [code ...]
``` ```
Pretty print JSON: Pretty print JSON without passing any arguments:
``` ```
$ echo '{"key":"value"}' | fx $ echo '{"key":"value"}' | fx
{ {
@ -32,34 +32,42 @@ $ echo '{"key":"value"}' | fx
} }
``` ```
Use anonymous function: ### Anonymous function
Use an anonymous function as reducer which gets JSON and processes it:
``` ```
$ echo '{"foo": [{"bar": "value"}]}' | fx "x => x.foo[0].bar" $ echo '{"foo": [{"bar": "value"}]}' | fx 'x => x.foo[0].bar'
"value" "value"
``` ```
### This Binding
If you don't pass anonymous function `param => ...`, code will be automatically transformed into anonymous function. 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: And you can get access to JSON by `this` keyword:
``` ```
$ echo '{"foo": [{"bar": "value"}]}' | fx "this.foo[0].bar" $ echo '{"foo": [{"bar": "value"}]}' | fx 'this.foo[0].bar'
"value" "value"
``` ```
### Chain
You can pass any number of anonymous functions for reducing JSON: You can pass any number of anonymous functions for reducing JSON:
``` ```
$ echo '{"foo": [{"bar": "value"}]}' | fx "x => x.foo" "this[0]" "this.bar" $ echo '{"foo": [{"bar": "value"}]}' | fx 'x => x.foo' 'this[0]' 'this.bar'
"value" "value"
``` ```
### Generator
If passed code contains `yield` keyword, [generator expression](https://github.com/sebmarkbage/ecmascript-generator-expression) If passed code contains `yield` keyword, [generator expression](https://github.com/sebmarkbage/ecmascript-generator-expression)
will be used: will be used:
``` ```
$ curl ... | fx "for (let user of this) if (user.login.startsWith('a')) yield user" $ curl ... | fx 'for (let user of this) if (user.login.startsWith("a")) yield user'
``` ```
Access to JSON through `this` keyword: Access to JSON through `this` keyword:
``` ```
$ echo '["a", "b"]' | fx "yield* this" $ echo '["a", "b"]' | fx 'yield* this'
[ [
"a", "a",
"b" "b"
@ -67,7 +75,7 @@ $ echo '["a", "b"]' | fx "yield* this"
``` ```
``` ```
$ echo '["a", "b"]' | fx "yield* this; yield 'c';" $ echo '["a", "b"]' | fx 'yield* this; yield "c";'
[ [
"a", "a",
"b", "b",
@ -75,18 +83,30 @@ $ echo '["a", "b"]' | fx "yield* this; yield 'c';"
] ]
``` ```
### Update
You can update existing JSON using spread operator: You can update existing JSON using spread operator:
``` ```
$ echo '{"count": 0}' | fx "{...this, count: 1}" $ echo '{"count": 0}' | fx '{...this, count: 1}'
{ {
"count": 1 "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)'
```
### Other examples
Convert object to array: Convert object to array:
``` ```
$ cat package.json | fx "yield* Object.keys(this.dependencies)" $ cat package.json | fx 'Object.keys(this.dependencies)'
[ [
"cardinal", "cardinal",
"get-stdin", "get-stdin",
@ -94,12 +114,6 @@ $ cat package.json | fx "yield* Object.keys(this.dependencies)"
] ]
``` ```
Use any npm package:
```
$ npm install -g lodash
$ cat package.json | fx "require('lodash').keys(this.dependencies)"
```
## Related ## Related

View File

@ -10,16 +10,16 @@ const cli = meow(`
$ fx [code ...] $ fx [code ...]
Examples Examples
$ echo '{"key": "value"}' | fx "x => x.key" $ echo '{"key": "value"}' | fx 'x => x.key'
"value" "value"
$ echo '[1,2,3]' | fx "this.map(x => x * 2)" $ echo '[1,2,3]' | fx 'this.map(x => x * 2)'
[2, 4, 6] [2, 4, 6]
$ echo '{"items": ["one", "two"]}' | fx "this.items" "this[1]" $ echo '{"items": ["one", "two"]}' | fx 'this.items' 'this[1]'
"two" "two"
$ echo '{"count": 0}' | fx "{...this, count: 1}" $ echo '{"count": 0}' | fx '{...this, count: 1}'
{"count": 1} {"count": 1}
`) `)