0.8.0 • Published 6 years ago

json-compiler v0.8.0

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

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 = require('json-compiler');

var data = {
    foo: 'bar',
    bar: {
        foo: 'bar_foo'
    }
};

var object = {
    'foo_{{foo}}': '{{bar.foo}}'
};

Compiler.compile(object, data, helpers);

// {foo_bar: 'bar_foo'}

Helper functions

Helper functions are applied as {{func}} or {{#func}}.

var data = {};

var helpers: {
    calc: function () {
        return 1 + 2;
    },
    sum: function (a, b) {
        return parseInt(a) + parseInt(b);
    }
};

var object = {
    foo: '{{#sum}}{{calc}},{{calc}}{{/sum}}'
};

Compiler.compile(object, data, helpers);

// {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: {
    sum: function (a, b) {
        return parseInt(a) + parseInt(b);
    }
};

var object = {
  '[[foo]]': ['foo', {key1: '{{bar.foo}}', key2: '{{#sum}}{{bar.bar}},{{bar.bar}}{{/sum}}', key3: 'customValue'}]
};

Compiler.compile(object, data, helpers);

/*
[
  ['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.compile(object, data);

/*
[
  ['foo', 'bar'],
  ['bar', 'foo']
]
*/

Tests

Run tests locally with:

  npm test

Author

Palmabit.com

Licence

See the MIT License

0.8.0

6 years ago

0.7.0

8 years ago

0.6.0

8 years ago

0.5.0

8 years ago

0.3.3

8 years ago

0.3.2

8 years ago

0.3.1

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago