Coder Social home page Coder Social logo

ymlx's Introduction

Command-line YAML processing tool

Features

  • Plain JavaScript to manipulate document(s)

Install

$ npm install -g ymlx

Usage

Pipe into ymlx any YAML and anonymous function for reducing it.

$ cat my.yaml | ymlx [code ...]

Anonymous function

Use an anonymous function as reducer which gets YAML as an object and processes it:

$ echo 'foo: [bar: value]' | ymlx 'x => x.foo[0].bar'
value

this Binding

If you don't pass anonymous function param => ..., code will be automatically transformed into anonymous function. And you can get access to YAML by this keyword:

$ echo 'foo: [bar: value]' | ymlx 'this.foo[0].bar'
value

Multiple Documents

You can pass a YAML file with multiple documents (using the --- separator), and your commands will be applied to each document.

# test.yml
---
foo:
- bar: value
---
foo:
- bar: another
$ cat test.yml | ymlx 'this.foo[0].bar'
---
value
---
another

Chain

You can pass any number of anonymous functions for reducing JSON:

$ echo 'foo: [bar: value]' | ymlx 'x => x.foo' 'this[0]' 'this.bar'
value

Generator

If passed code contains yield keyword, generator expression will be used:

$ curl ... | ymlx 'for (let user of this) if (user.login.startsWith("a")) yield user'

Access to YAML through this keyword:

# test.yml
- a
- b
$ cat test.yml | ymlx 'yield* this'
- a
- b
$ cat test.yml | ymlx 'yield* this; yield "c";'
- a
- b
- c

Update

You can update existing YAML using spread operator:

$ echo 'count: 0' | ymlx '{...this, count: 1}'
count: 1

Use npm package

Use any npm package by installing globally or in the current working directory:

$ npm install lodash # -g if you want
$ cat 'count: 0' | ymlx 'require("lodash").keys(this)'
- count

Formatting

$ echo '[1,2,3]' | ymlx 'this.reduce((a,n) => a += n.toString(), "concat: ")'
'concat: 123'

Inspiration

ymlx was inspired by fx

Related

  • jq – cli JSON processor on C
  • jsawk – like awk, but for JSON
  • json – another JSON manipulating cli library
  • jl – functional sed for JSON on Haskell

License

MIT

ymlx's People

Contributors

0xflotus avatar matthewadams avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ymlx's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.