npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

procurator

v3.0.1

Published

A tiny, stream based replacement template engine.

Downloads

43

Readme

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