@bracketedrebels/jsonpipes
TypeScript icon, indicating that this package has built-in type declarations

1.0.8 • Public • Published

JSON Pipes

Customizable JSON to JSON transforming tool, inspired by Angular.

Tests

$ npm test

Features

  • Simple familiar syntax

    Syntax is based on Angular template pipes syntax, but more strict and, hence, declarative.

  • Still JSON

    JSON with pipes has no problem with JSON validity. Just indicate string that it should be processed with help of ` symbol.

  • Extensible

    You are welcome to add new pipes with any amount of parameters.

  • Short API

    Tool has good and ease to use API. Just 3 methods to chain together: use pipes, compile template and execute it over data.

  • Asyncronious

    Template execution is completely asyncronious. If pipes you are using are well designed, you will get even more asyncronisity. Read further for more.

API

use(alias: string, pipe: Pipe): this

Use this method when you wish to extend compiler with custom pipes. For example, in order to use pipe makegood you have to register it within the jsonpipes instance, using this method. Note, that this method return this instance and you are able to chain methods and write declarative compact code.

compile(template: any): this

This method takes any validly parsed JSON as input and compile it into specially optimized structure, that can be executed rapidly over provided data. You can cache instance of JSONPipes after this mathod invoked in order to execute it later. Note, that this method return this instance and you are able to chain methods and write declarative compact code.

execute(done: NodeCallback, data?: any): this

Use this method only after some template is compiled via .compile. This method will execute all jsonpipes occurrences in compiled template, using optinally provided data. As soon as execution is competed, done callback will be executed with error (if any) as first argument and result (if no errors) as second argument. Note, that this method return this instance and you are able to chain methods and write declarative compact code.

Examples

When creating a JSONPipes instance, you are allowed to provide a few options in order to customize execution behavior. Currently, there are two of them: entries and eliminate.

Assign a list of strings to eliminate option in order to specify which fields of execution result should be eliminated. For example, if you have a definitions field, that you will only address in your JSON, then you may want to eliminate them in result. For example:

template.json:

{
  "eliminate": {},
  "keep": {
    "eliminate": {}
  }
}
import { jsonpipes } from '@bracketedrebels/jsonpipes';
import * as template from './template.json';

jsonpipes({ eliminate: ['eliminate', 'keep.eliminate'] })
  .compile(template)
  .execute(compiled => console.log(compiled));
  // outputs { keep: {} }

Assign a list of strings to entries option in order to specify what fields values should be considered as executable. All fields of provided template that are not in provided list will not be executed. For example:

data.json:

{ 
  "answer": {
    "truthy": 42,
    "falsy": 13
  }
}

template.json:

{
  "executed" : "`answer.truthy`",
  "ignored" : "`answer.falsy`"
}
import { jsonpipes } from '@bracketedrebels/jsonpipes';
import * as data from './data.json';
import * as template from './template.json';

jsonpipes({ entries: ['executed'] })
  .compile(template)
  .execute(compiled => console.log(compiled), data);
  // outputs { executed: 42, ignored: '`answer.falsy`' }

Package Sidebar

Install

npm i @bracketedrebels/jsonpipes

Weekly Downloads

0

Version

1.0.8

License

MIT

Unpacked Size

53.1 kB

Total Files

22

Last publish

Collaborators

  • wolfsonchik