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

obj-template

v2.1.3

Published

Return a new object treating strings as templates

Downloads

2

Readme

obj-template

You run:

var objTemplate = require('obj-template');

var config = {
  baseURL: 'http://www.example.com',
  urls: [
    "<%= baseURL %>/homepage",
    "<%= baseURL %>/menu",
    "<%= baseURL %>/contacts"
  ]
};

var newConfig = objTemplate(config);

and it returns a new object (it is a deep clone of the original object):

{
  baseURL: 'http://www.example.com',
  urls: [
    "http://www.example.com/homepage",
    "http://www.example.com/menu",
    "http://www.example.com/contacts"
  ]
}

It can be useful to write short configuration objects, without being too repetitive.

objTemplate uses as default lodash "template" function. It can also take any equivalent function as a templateFunc in the options (the second argument):

var handlebars = require('handlebars');
var objTemplate = require('obj-template');

var config = {
  baseURL: 'http://www.example.com',
  urls: [
    "{{baseURL}}/homepage",
    "{{baseURL}}/menu",
    "{{baseURL}}/contacts"
  ]
};

var newConfig = objTemplate(config, { templateFunc: handlebars.compile });

isTemplate

Before converting a string to a template, the function checks if the string is a template. If you changed the templateFunc, you should also implement a function "isTemplate" that returns true if a string is a template.

var handlebars = require('handlebars');
var objTemplate = require('obj-template');

function isHandlebars(s) {
  var re = new RegExp('{{.+}}');
  return !!re.exec(s);
}

var config = {
  baseURL: 'http://www.example.com',
  urls: [
    "{{baseURL}}/homepage",
    "{{baseURL}}/menu",
    "{{baseURL}}/contacts"
  ]
};

var newConfig = objTemplate(config, { templateFunc: handlebars.compile, isTemplate: isHandlebars });

Scope

Every value is contained in a scope related to the object where is defined:

var objTemplate = require('obj-template');

var config = {
  url: 'http://www.example.com',
  items: [
    {
      title: 'Section1',
      url: '<%= url %>/section1', // ignores the property with the same name of the current object
      navigation: [
        "<%= url %>/homepage", // use current object as scope
        "<%= url %>/menu",
        "<%= url %>/contacts"
      ]
    },
    {
      title: 'Section2',
      navigation: [
        "<%= url %>/homepage", // use root scope
        "<%= url %>/menu",
        "<%= url %>/contacts"
      ]
    }
  ]
};

var newConfig = objTemplate(config);

returns:

var newConfig = {
  url: 'http://www.example.com',
  items: [
    {
      title: 'Section1',
      url: 'http://www.example.com/section1',
      navigation: [
        "http://www.example.com/section1/homepage",
        "http://www.example.com/section1/menu",
        "http://www.example.com/section1/contacts"
      ]
    },
    {
      title: 'Section2',
      navigation: [
        "http://www.example.com/homepage",
        "http://www.example.com/menu",
        "http://www.example.com/contacts"
      ]
    }
  ]
};

The algorithm ensure that the scope of the lowers level are done first (breadth first). Take into consideration that within the same object there is a risk to reference a property that is still not ready. For example, this works:

objTemplate({
  a : 'test',
  b: '<%= a %>',
  c: '<%= b %>'
});

This doesn't:

objTemplate({
  a: '<%= b %>'
  b: '<%= c %>',
  c : 'test',
});

This risks is mitigated by the fact that most js implementations use the order in which properties are defined (with the exception of "numeric" keys). All js implementations use the same order consistently. ES6 standardised this way of ordering: http://2ality.com/2015/10/property-traversal-order-es6.html. This reduces to the following advice:

  • Take into consideration the order in which properties are defined in the object
  • unless there are numeric keys (like "3" or "14" but unlike "03") because they are sorted in a different way

stack

You can access to the current object stack using the special property _stack.

{
  fruit: 'apple',
  test2: {
    fruit: 'orange',
    breadcrumbs: '<%= _stack.map(function (o) { return o.fruit; }).join(", ") %>'
  }
}

returns:

{
  fruit: 'apple',
  test2: {
    fruit: 'orange',
    breadcrumbs: 'apple, orange'
  }
}

dontclone

The function returns a new object, deep cloned from the original one. If you prefer to modify the original object you can pass "dontclone: true" in the options.

var obj = {
  a : 'test',
  b: '<%= a %>'
};
objTemplate(obj, { dontclone: true });

templateCache

This options inject a cache used for the templates. We have verified it can greatly increase the speed calling the function multiple times on a object containing the same strings (using handlebars for example).

var obj = {
  a : 'test',
  b: '<%= a %>'
};
var templateCache = {};
objTemplate(obj, { templateCache: {} });

If you are not using the default templating engine (lodash template), to avoid unnecessary caching you should also pass a function "isTemplate" that returns true if a string is a template.