Git Product home page Git Product logo

skelt's Introduction

skelt

Build Status

Skelt is a templating engine for object literals.

Installation

Install with:

npm install -S skelt

or

yarn add skelt

Usage

When you need to build complex JS objects dynamically from a predefined template object, use Skelt to benefit from testability capabilities.

Skelt is a deadly simple function: the first argument represent your template object, whereas the second argument is a parameters object with all values required by the template.

skelt = (template, parameters) => object

Here is the compulsory "hello, world" example:

import skelt from 'skelt';

const template = {
    welcome: ({ name }) => `Hello ${name}`
};

skelt(template, { name: 'world' });
// {
//    welcome: 'Hello world'
// }

At build step, the template is walked and resolved recursively. That means that all functions values are executed with the parameters object as argument. Non-function values are just passed as is.

import skelt from 'skelt';

const personTemplate = {
    fullName: ({firstName, lastName }) => `${firstName} ${lastName}`,
    age: ({ age }) => age,
    misc: {
        isAdult: ({ age }) => age > 21,
    }
};

skelt(personTemplate, { firstName: 'John', lastName: 'Doe', age: 42 });
// {
//    fullName: 'John Doe',
//    age: 42,
//    misc: {
//        isAdult: true,
//    },
// }

The template can also be a function returning an object, which somehow makes writing templates easier:

import skelt from 'skelt';

const isAdult = ({ age }) => age > 21;

const personTemplate = ({ age, firstName, lastName }) => ({
    fullName: `${firstName} ${lastName}`,
    age,
    misc: {
        isAdult,
    }
});

skelt(personTemplate, { lastname: 'John', firstname: 'Doe', age: 42 });

// same result as above

That means you can build very complex objects from simple templates using composition, as shown in the example below.

import skelt from 'skelt';

const url = ({ locale }) => `http://www.example.com/${locale}`;
const localize = choices => ({ locale }) => choices[locale]; // higher-order function!
const title = localize({
    fr: 'Bienvenue sur mon site',
    en: 'Welcome on my website',
});

const homeTemplate = {
    url,
    title,
    body: localize({
        fr: 'Bonjour le monde',
        de: 'Hello, world',
    }),
    tags: localize({
        fr: ['accueil', 'test'],
        en: ['welcome', 'test'],
    })
};

skelt(homeTemplate, { locale: 'fr' });
// {
//    url: 'http://www.example.com/fr',
//    title: 'Bienvenue sur mon site',
//    body: 'Bonjour le monde',
//    tags: ['accueil', 'test'],
// }

Use Memoize To Improve Performance

Because of the full traversal and resolving of the template, performance can sometimes be suboptimal.

To speed up the computing time for templating, you can use the memoize function of your choice in your template. This way, functions are only called the first time the template is rendered. Here is an example:

import skelt from 'skelt';
import memoize from 'lodash/memoize';

import fibonnaci from './some-fibonnaci-module';

skelt({
    function: 'fibonnaci',
    result: memoize(fibonnaci),
}, { n: 10 });

// {
//    function: 'fibonnaci',
//    result: 55, // computed only the first time for n = 10
// }

Contributing

Run the tests with this command:

make test

License

Skelt is licensed under the MIT Licence, courtesy of Marmelab and ARTE.

skelt's People

Contributors

alexisjanvier avatar dependabot[bot] avatar fzaninotto avatar jdemangeon avatar jpetitcolas avatar kmaschta avatar zyhou avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

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.