JSON Compiler
Json-compiler can do replacements on your js and json objects and compile them like templates. It's inspired by mustache and json-templater. It supports:
- string replacement
- object replacement
- array replacement
- replacement with functions
- helpers
Installation
npm install json-compiler --save
Usage
String replacement
String replacement is made by using variables like {{foo}}
.
var Compiler = ; var data = foo: 'bar' bar: foo: 'bar_foo' ; var object = 'foo_{{foo}}': '{{bar.foo}}'; Compiler; // {foo_bar: 'bar_foo'}
Helper functions
Helper functions are applied as {{func}}
or {{#func}}
.
var data = {}; var helpers: { return 1 + 2; } { return + ; }; var object = foo: '{{#sum}}{{calc}},{{calc}}{{/sum}}'; Compiler; // {foo: '6'}
Array replacement (example 1)
Array replacement is made by using variables like [[foo]]
.
var data = foo: foo: 'foo_foo' bar: foo: 'bar_foo' bar: '1' foo: 'foo_bar' bar: foo: 'bar_bar' bar: '2' ; var helpers: { return + ; }; var object = '[[foo]]': 'foo' key1: '{{bar.foo}}' key2: '{{#sum}}{{bar.bar}},{{bar.bar}}{{/sum}}' key3: 'customValue'; Compiler; /*[ ['foo_foo', {key1: 'bar_foo', key2: 2, key3: 'customValue'}], ['foo_bar', {key1: 'bar_bar', key2: 4, key3: 'customValue'}]]*/
Array replacement (example 2)
var data = foo: 'foo' 'bar' 'bar' 'foo' ;var object = '[[foo]]' Compiler; /*[ ['foo', 'bar'], ['bar', 'foo']]*/
Tests
Run tests locally with:
npm test
Author
Palmabit.com