deep-template

1.0.0 • Public • Published

deep-template

Create "curried" templates from deep JS structures using ES6 template syntax

Install

In a browser:

<script src="deep-template.js"></script>

Using npm:

$ npm i -g npm
$ npm i --save deep-template

Usage

deepTemplate(String, Object) -> String

var deepTemplate = require('deep-template');
var buildPath = deepTemplate('/api/users/${id}/');
buildPath({id: '549873456448'}); // -> "/api/users/549873456448/"

deepTemplate(Object, Object) -> Object

var configTemplate = {
    a: ['/api/users/${id}/${action}'],
    b: { 
        deep: {
            foo: [
                { 
                    stuff: 'DEFAULT_ENV=${env}'
                }
            ] 
        }
    },
    c: function() { console.log('avoided') },
    d: /keepRegExp/gim,
    e: 'keep simple texts',
    f: 10
};
var defaults = {action: 'defaultAction', env: '/usr/bin/bash'};
var configBuilder = deepTemplate(configTemplate, defaults);
configBuilder({id: '549873456448'}); 
/* -> {
        a: ["/api/users/549873456448/defaultAction"],
        b: {
            deep: {
                foo: [{
                    stuff: "DEFAULT_ENV=/usr/bin/bash"
                }]
            }
        },
        c: function() { console.log('avoided') },
        d: /keepRegExp/gim,
        e: "keep simple texts",
        f: 10
    }
*/

deepTemplate(String, Array) -> String

var arrayBuildPath = deepTemplate('/api/users/${0}/${1}/${2}', [null, null, 'ascending']);
arrayBuildPath(['549873456448', 'getUser']); // -> "/api/users/549873456448/getUser/ascending"

Package Sidebar

Install

npm i deep-template

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

6.47 kB

Total Files

5

Last publish

Collaborators

  • leodutra