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

object-template

v1.3.1

Published

Bidirectional JSON-based templating engine

Downloads

28

Readme

object-template

npm npm license npm downloads travis

Bidirectional JSON-based templating engine

Installation

Install object-template by running:

npm install --save object-template

Documentation

object-template is a templating engine that operates on JSON objects, providing the unique benefit of allowing bidirectional transformations, that is, from template and data to a result, and from a template and result to the original data.

For example, consider the following template:

{
  "foo": "My name is {{name}}"
}

Notice the use of double curly braces to denote string interpolation.

In order to compile this template, we need a name value. This is an example data object that can be used to compile the above template:

{
  "name": "John Doe"
}

The compilation result looks like this:

{
  "foo": "My name is John Doe"
}

Now consider that we have the compilation result and the template, and we want to be able to determine what was the original data used to compile it.

object-template will realise "My name is John Doe" was compiled from "My name is {{name}}", and therefore that name equals John Doe. Using this information, object-template will "decompile" the template and return back the following object to the user, which unsurprisingly equals the "data" object:

{
  "name": "John Doe"
}

The example objects contain one key and a single interpolation, but on real templates, there can be complex nesting levels and multiple interpolations (even many per property).

Default values

Default values can be provided using the || operator. The default value should be JSON encoded. In the example below, if the name value is not provided, the default value John Doe will be used instead.

{
  "foo": "My name is {{name || \"John Doe\"}}"
}

When decompiling a result that used a default value, the default value will be returned.

API

object-template.compile(template, data, [options]) ⇒ Object

Kind: static method of object-template
Summary: Compile a JSON template
Returns: Object - compilation result
Access: public

| Param | Type | Description | | --- | --- | --- | | template | Object | json template | | data | Object | template data | | [options] | Object | options | | [options.delimiters] | Array.<String> | delimiters | | [options.allowMissing] | Boolean | allow missing variables |

Example

const result = objectTemplate.compile({
  greeting: 'Hello, {{name}}!'
}, {
  name: 'John Doe'
})

console.log(result)
> {
>   greeting: 'Hello, John Doe!'
> }

object-template.decompile(template, result, [options]) ⇒ Object

Kind: static method of object-template
Summary: Decompile a JSON template
Returns: Object - template data
Access: public

| Param | Type | Description | | --- | --- | --- | | template | Object | json template | | result | Object | compilation result | | [options] | Object | options | | [options.delimiters] | Array.<String> | delimiters |

Example

const data = objectTemplate.decompile({
  greeting: 'Hello, {{name}}!'
}, {
  greeting: 'Hello, John Doe!'
})

console.log(data)
> {
>   name: 'John Doe'
> }

object-template.matches(template, object, [options]) ⇒ Boolean

Kind: static method of object-template
Summary: Check if a compiled object matches a template
Returns: Boolean - whether object matches template
Access: public

| Param | Type | Description | | --- | --- | --- | | template | Object | template object | | object | Object | compiled object | | [options] | Object | options | | [options.delimiters] | Array.<String> | delimiters |

Example

if (objectTemplate.matches({
  foo: '{{bar}}'
}, }
  foo: 'bar'
)) {
  console.log('This is a match!')
}

Tests

Run the test npm script:

npm test

Contribute

Before submitting a PR, please make sure that you include tests, and that the linter runs without any warning:

npm run lint

Support

If you're having any problem, please raise an issue on GitHub.

License

This project is free software, and may be redistributed under the terms specified in the license.