diff --git a/docs.md b/docs.md index a917f47..5e9d423 100644 --- a/docs.md +++ b/docs.md @@ -2,13 +2,13 @@ `fx` can work in two modes: cli and interactive. To start interactive mode pipe into `fx` any JSON: -``` +```bash $ curl ... | fx ``` Or you can pass file argument as first parameter: -``` +```bash $ fx my.json ``` @@ -17,7 +17,7 @@ If any argument was passed, `fx` will apply it and prints to stdout. ## Anonymous function Use an anonymous function as reducer which gets JSON and processes it: -``` +```bash $ echo '{"foo": [{"bar": "value"}]}' | fx 'x => x.foo[0].bar' value ``` @@ -26,7 +26,7 @@ value 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: -``` +```bash $ echo '{"foo": [{"bar": "value"}]}' | fx 'this.foo[0].bar' value ``` @@ -34,13 +34,13 @@ value ## Dot It is possible to omit `this` keyword: -``` +```bash $ echo '{"foo": [{"bar": "value"}]}' | fx .foo[0].bar value ``` If single dot is passed, JSON will be processed without modification: -``` +```bash $ echo '{"foo": "bar"}' | fx . { "foo": "bar" @@ -50,7 +50,7 @@ $ echo '{"foo": "bar"}' | fx . ## Chain You can pass any number of anonymous functions for reducing JSON: -``` +```bash $ echo '{"foo": [{"bar": "value"}]}' | fx 'x => x.foo' 'this[0]' 'this.bar' value ``` @@ -59,12 +59,12 @@ value If passed code contains `yield` keyword, [generator expression](https://github.com/sebmarkbage/ecmascript-generator-expression) will be used: -``` +```bash $ curl ... | fx 'for (let user of this) if (user.login.startsWith("a")) yield user' ``` Access to JSON through `this` keyword: -``` +```bash $ echo '["a", "b"]' | fx 'yield* this' [ "a", @@ -72,7 +72,7 @@ $ echo '["a", "b"]' | fx 'yield* this' ] ``` -``` +```bash $ echo '["a", "b"]' | fx 'yield* this; yield "c";' [ "a", @@ -85,7 +85,7 @@ $ echo '["a", "b"]' | fx 'yield* this; yield "c";' You can update existing JSON using spread operator: -``` +```bash $ echo '{"count": 0}' | fx '{...this, count: 1}' { "count": 1 @@ -95,7 +95,7 @@ $ echo '{"count": 0}' | fx '{...this, count: 1}' ## Using packages Use any npm package by installing it globally: -``` +```bash $ npm install -g lodash $ cat package.json | fx 'require("lodash").keys(this.dependencies)' ``` @@ -112,7 +112,7 @@ Object.assign(global, require('lodash/fp')) And now you will be able to call all lodash methods. For example, see who's been committing to react recently: -``` +```bash curl 'https://api.github.com/repos/facebook/react/commits?per_page=100' \ | fx 'groupBy("commit.author.name")' 'mapValues(size)' toPairs 'sortBy(1)' reverse 'take(10)' fromPairs ``` @@ -126,12 +126,12 @@ curl 'https://api.github.com/repos/facebook/react/commits?per_page=100' \ If you need something different then JSON (for example arguments for xargs) do not return anything from reducer. `undefined` value is printed into stderr by default. -``` +```bash echo '[]' | fx 'void 0' undefined ``` -``` +```bash echo '[1,2,3]' | fx 'this.forEach(x => console.log(x))' 2>/dev/null | xargs echo 1 2 3 ``` @@ -139,7 +139,7 @@ echo '[1,2,3]' | fx 'this.forEach(x => console.log(x))' 2>/dev/null | xargs echo ## Other examples Convert object to array: -``` +```bash $ cat package.json | fx 'Object.keys(this.dependencies)' [ "@medv/prettyjson" @@ -148,7 +148,7 @@ $ cat package.json | fx 'Object.keys(this.dependencies)' By the way, fx has shortcut for `Object.keys(this)`. Previous example can be rewritten as: -``` +```bash $ cat package.json | fx this.dependencies ? ```