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

barrel-js

v1.2.1

Published

A simple & minimal template engine to build semantic templates for JSON objects and strings.

Downloads

14

Readme

barrel-js

barrel-js is a simple & minimal npm package to build semantic templates for JSON objects and strings.

Install

NPM

npm install barrel-js

Yarn

yarn add barrel-js

Getting started

compile()

JSON templates

barrel-js templates look like regular JSON objects, with embedded template expressions.

{
    "title": "Welcome",
    "body": "Hey ${first_name}! Great to have you here."
}

A template expression is wrapped into ${ + key + } symbols.

You can compile this templates in JavaScript by using the compile function, which takes two parameters

  1. the JSON or String template
  2. a context object of template keys and their corresponding values

Example

To receive the parsed template with its given context, execute the compile function:

const barrel = require('barrel-js');

let template = {
    "title": "Welcome",
    "body": "Hey ${first_name}! Great to have you here."
};

let context = {
    "first_name": "David"
};

let payload = barrel.compile(template, context);

This will result in following JSON object

{
    "title": "Welcome",
    "body": "Hey David! Great to have you here."
}

Escaping template expressions

Template expressions can be escaped by using \\ in front of the expression and the closing } symbol. For example "Hey \\${first_name\\}!" will result in "Hey ${first_name}!".

filter()

Filtering JSON properties

To filter a json payload for specific properties use the filter function.

const barrel = require('barrel-js');

let template = {
    "type": "section",
    "text": {
        "type": "plain_text",
        "text": "This is a section block."
    },
    "accessory": {
        "type": "button",
        "text": {
            "type": "plain_text",
            "text": "Button",
            "emoji": true
        },
        "value": "click_me_123"
    }
};

let result = barrel.filter(payload.get_object, "text")

This will result in following JSON object

[ 
   { 
      "type":"mrkdwn",
      "text":"This is a section block."
   },
   "This is a section block.",
   { 
      "type":"plain_text",
      "text":"This is a section block."
   },
   "This is a section block."
]

Use an optional maximum property to reduce the number of returned items.

barrel.filter(payload.get_array, "text", 1);

This will throw an index out of range error if the number of found items is lower than the provided max items property.

contains()

checking for JSON sub-structures

To check if a specific json structure exists in this payload use the contains function.

const barrel = require('barrel-js');

let template = {
    "type": "section",
    "text": {
        "type": "plain_text",
        "text": "This is a section block."
    },
    "accessory": {
        "type": "button",
        "text": {
            "type": "plain_text",
            "text": "Button",
            "emoji": true
        },
        "value": "click_me_123"
    }
};

let result = barrel.contains(template, { "type": "section" });

This will return either true or false.

match()

filtering for JSON sub-structures

To filter a JSON object or array for a specific json sub-structure use the match function.

const barrel = require('barrel-js');

let template = {
    "type": "section",
    "text": {
        "type": "plain_text",
        "text": "This is a section block."
    },
    "accessory": {
        "type": "button",
        "text": {
            "type": "plain_text",
            "text": "Button",
            "emoji": true
        },
        "value": true
    }
};

let result = barrel.match(template, { "type": "button", "value": true });

This will result in following JSON object

[ 
   {
        "type": "button",
        "text": {
            "type": "plain_text",
            "text": "Button",
            "emoji": true
        },
        "value": true
    }
]

License

MIT