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

simple-json-template

v1.1.0

Published

Simple template engine to generate json structures

Downloads

7

Readme

simple-json-template

Build Status Coverage

A Simple JSON template engine to render JSON structured data

Example

docker-compose Template (yaml)

version: '3',
services: 
  '~~~for': 'services as (service, i)'
  '${service.name}':
    image: 'myrepo/${service.name}:${service.version}'
    build:
      context: '${service.name}'
      dockerfile: '${service.name}/Dockerfile'
    ports:
      '~~~for': 'service.ports as (containerPort, j)'
      '~~~each': '${9000 + j + (i * 10)}:${containerPort}'
    'environment':
      '~~~for': 'service.envs as env'
      '${env.name}': '${env.value}'

Data (json)

{
  "services": [
    {
      "name": "order-service",
      "version": "1.0",
      "ports": [ 8080 ],
      "envs": [
        {
          "name": "product_service_url",
          "value": "http://product-service:8080"
        }
      ]
    },
    {
      "name": "product-service",
      "version": "3.5",
      "ports": [ 8080, 9001 ],
      "envs": [
        {
          "name": "database_host",
          "value": "mysql.hosts"
        },
        {
          "name": "database_name",
          "value": "product-database"
        },
        {
          "name": "database_username",
          "value": "product-database"
        },
        {
          "name": "database_password",
          "value": "mypass123"
        }
      ]
    }
  ]
}

Output (yaml)

version: '3',
services: 
  order-service:
    image: 'myrepo/order-service:1.0'
    build:
      context: .
      dockerfile: 'order-service/Dockerfile'
    ports:
      - '9000:8080'
    environment:
      product_service_url: 'http://product-service:8080'
  product-service:
    image: 'myrepo/product-service:3.5'
    build:
      context: .
      dockerfile: 'product-service/Dockerfile'
    ports:
      - '9010:8080'
      - '9011:9001'
    environment:
      database_host: 'mysql.hosts'
      database_name: 'product-database'
      database_username: 'product-database'
      database_password: 'mypass123'
     

Install

npm install simple-json-template

Usage

var TemplateEngine = require("simple-json-template");

var result = TemplateEngine.render({
  say: "${message}"
}, {
  message: "Hello!"
});

Syntax

A Simple JSON Template is basically an object structure which describes how it should be rendered based on input data. The template can be defined in JSON, Yaml or in Javascript itself.

Expressions

Expressions are a string of JavaScript code to get data or manipulate it. For example: ${myList.length}. Or a more complicated expression: ${myList.filter(item => !item.hidden).map(item => item.name).join(', ')}. The JavaScript code is executed in a sandbox provided by vm2

Variables

You can use variables inside an expression. These variables can be provided when rendering the template:

var result = TemplateEngine.render({
  say: "${message}"
}, {
  message: "Hello!"
});
// 'say' renders as "Hello!"

Variables can also be found in the template itself:

var result = TemplateEngine.render({
  message: "Hello",
  output: {
    name: "Bill!",
    say: "${_root.message} ${_this.name}"
  }
});
// 'output.say' renders as "Hello Bill!"

The variable _this returns the current object where the expression is and the _root variable returns the root object of the template.

~~~if Statement

The if statement can be used to show parts of template based on a condition.

Example 1: if statement is true

var result = TemplateEngine.render({
  user: {
    name: "Bob"  
  },
  '~~~if': "_this.user.toLowerCase() === 'bob'"
});
// renders as:
// {
//   user: {
//     name: "Bob",
//   }
// }

Example 2: if statement is false

var result = TemplateEngine.render({
  user: {
    name: "John"  
  },
  '~~~if': "_this.user.toLowerCase() === 'bob'"
});
// renders as:
// {}

Example 3: if statement with then

var result = TemplateEngine.render({
  user: {
    name: "Bob"  
  },
  '~~~if': "_this.user.toLowerCase() === 'bob'",
  '~~~then': {
    user: {
      name: "Bob Awesome!"
    }
  }
});
// renders as:
// {
//   user: {
//     name: "Bob Awesome!",
//   }
// }

Example 4: if statement with else

var result = TemplateEngine.render({
  user: {
    name: "John"  
  },
  '~~~if': "_this.user.toLowerCase() === 'bob'",
  '~~~else': {
    user: {
      name: "Not Bob"
    }
  }
});
// renders as:
// {
//   user: {
//     name: "Not Bob",
//   }
// }

~~~for Statement

The for statement can be used for looping over a list.

Example 1: loop over list

var result = TemplateEngine.render({
  user: {
    '~~~for': "users as user",
    name: "${user}"
  }
}, {
  users: ["Bob", "Alise"]
});
// renders as:
// {
//   users: [
//     {
//       name: "Bob",
//     },
//     {
//       name: "Alise",
//     }]
// }

Example 2: use index and length

var result = TemplateEngine.render({
  user: {
    '~~~for': "users as (user, index, length)",
    id: "${index}/${length}",
    name: "${user}"
  }
}, {
  users: ["Bob", "Alise"]
});
// renders as:
// {
//   users: [
//     {
//       id: "0/2"
//       name: "Bob",
//     },
//     {
//       id: "1/2"
//       name: "Alise",
//     }]
// }

Example 3: loop using each

var result = TemplateEngine.render({
  user: {
    '~~~for': "users as user",
    '~~~each': {
      name: "${user}"
    }
  }
}, {
  users: ["Bob", "Alise"]
});
// renders as:
// {
//   users: [
//     {
//       name: "Bob",
//     },
//     {
//       name: "Alise",
//     }]
// }

Example 4: loop using property keys

var result = TemplateEngine.render({
  user: {
    '~~~for': "users as user, id",
    '${user}': {
      id: "${id}"
    }
  }
}, {
  users: ["Bob", "Alise"]
});
// renders as:
// {
//   users: {
//     Bob: {
//       id: "0"
//     },
//     Alise: {
//       id: "1",
//     }
//   }
// }

Licence

MIT