Git Product home page Git Product logo

procurator's Introduction

procurator

A tiny, super fast, stream based replacement template engine written in typescript. Now recursive! (keep reading to learn what that means)

This module has few features.

  • Simple variables
  • Defaults for variables
  • Support for nested placeholders (recursive)
  • Support for nested variables (Using homefront)

Installation

Simply use your package manager of choice.

  • npm i --save procurator
  • yarn add procurator

Usage

procurator(parameters[, recursive = true, limit = 100])

Code speaks. Let's do this.

some.template.js

module.exports = {
  // Simple replace
  hello: '{{ foo }}',

  // With a default
  cake: '{{ bar : lies }}',

  // Defaults get trimmed, let's use quotes
  bacon: '{{ bat : ' holds the truth ' }}',

  // Let's use double quotes
  empire: '{{ baz:"I haz\'s one" }}',

  // Let's default to an empty string
  hello: '{{ foo: }}',

  // Whaaaaaat, nested keys!?
  username: '{{ user.name:"Guest" }}',
};

app.js

const { stream } = require('procurator');
const fs         = require('fs');

let readStream  = fs.createReadStream('./some.template.js');
let writeStream = fs.createWriteStream('./out-file.js');
let parameters  = {foo: 'Batman', bat: 'is holy', user: {name: 'Swag-meister'}};
let recursive   = true; // New: replace more than once to allow for nested variables
let limit       = 100; // New: the maximum replacement depth. Throws an Error when reached.

readStream.pipe(stream(parameters, recursive, limit)).pipe(writeStream);

Produces ./out-file.js:

module.exports = {
  // Simple replace
  hello: 'Batman',

  // With a default
  cake: 'lies',

  // Defaults get trimmed, let's use quotes
  bacon: 'is holy',

  // Let's use double quotes
  empire: 'I haz one',
};

Note: The file extension doesn't matter. I used .js but it can be anything.

Sync

procurator(target, parameters[, recursive = true, limit = 100])`

Sometimes you just want to apply replaces in memory. For that purpose, a memory and code-size efficient method has been added.

const procurator = require('procurator');
const target     = 'Hello {{addressed: "world"}}! How are you doing {{ when: "today"}}?';
const recursive  = true; // New: replace more than once to allow for nested variables
const limit      = 100; // New: the maximum replacement depth. Throws an Error when reached.

console.log(procurator.replace(target, {addressed: 'developer'}, recursive, limit));

// Outputs: Hello developer! How are you doing today?

Nested placeholders

As of v2.0.0, Procurator supports nested placeholders. This means you can do the following (using sync for simplicity):

const procurator = require('procurator');
const target     = 'Hello {{addressed: "{{title: "Mr."}} world"}}! How are you doing {{ when: "today" }}?';
const recursive  = true; // New: replace more than once to allow for nested variables
const limit      = 100; // New: the maximum replacement depth. Throws an Error when reached.

console.log(procurator.replace(target, {title: 'Mrs.'}, recursive, limit));

// Outputs: Hello Mrs. world! How are you doing today?

This also works for parameters themselves; meaning that you can replace placeholders with strings that also contain placeholders.

Known limitations

Multi line placeholders

It's currently not possible to spread a placeholder over multiple lines. If you have a use case for this please create an issue.

License

MIT

procurator's People

Contributors

rwoverdijk avatar rawphs avatar christiaanscheermeijer avatar

Stargazers

Faris Tangastani avatar Oguzhan Ozdemir avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

rwoverdijk rawphs

procurator's Issues

Helpers

Add support for helper functions (join, ucfirst, camel to underscore, etc).

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.