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

@karmaniverous/handlebars

v0.5.0

Published

Exposes Lodash & Numeral.js as Handlebars helpers, plus some other goodies.

Downloads

12

Readme

Handlebars Goodies

This repo adds a handful of very useful helpers to Handlebars.js.

dataAnchor

Renders an anchor tag with data attributes. Syntax:

{{dataAnchor '<anchor text>' '<data attribute 1>' '<value 1>' '<data attribute 2>' '<value 2>' ...}}

This template...

{{dataAnchor 'anchor text' 'merchantId' 'abc123' 'userId' 'xyz789'}}

... renders this HTML:

<a data-merchantId="abc123" data-userId="xyz789">anchor text</a>

lodash & numeral

These helpers expose the Lodash and Numeral.js libraries to your templates. Syntax:

{{lodash '<function>' <arg1> <arg2> ...}}
{{numeral '<function>' <arg1> <arg2> ...}}

Here's a combined example. If amount = 1000000 then:

{{numeral 'format' (lodash 'divide' amount 100) '$0,0.00'}}

renders:

$10,000.00

params

This helper converts its arguments into an array. Very useful for Lodash functions that expect an array argument. For example:

{{lodash 'get' object (params 'a' 'b' 'c')}}

logic

Performs logical operations on the arguments. Syntax:

{{logic '<operator>' <arg1> <arg2> ...}}

For example, all of these return true:

{{#if (logic 'and' true true true)}}
{{#if (logic 'or' true true false)}}
{{#if (logic 'not' false)}}:
{{#if (logic 'xor' true false false)}} (odd number of trues)

Parameters are evaluated for truthiness. Supported operations are and, or, not, and xor.

ifelse

A ternary operator. Syntax:

{{ifelse <condition> <value if truthy> <value if falsy>}}

json2tf

Renders an object as a Terraform literal using json2tf. The syntax is:

{{json2tf <object> [offset] [tabWidth]}}

For example:

import { Handlebars } from '@karmaniverous/handlebars';

const data = {
  amount: 1234.567,
  anchorText: 'anchor text',
  merchantId: 'abc123',
  userId: 'def456',
  extra: { a: [1, 2, { c: 'd' }] },
};

const template = `
  output "config" { 
    description = "Global config." 
    value = {{json2tf (lodash "omit" this "merchantId" "userId") 4 4}} 
  }`;

console.log(Handlebars.compile(template, { noEscape: true })(data));

/*
    output "config" { 
        description = "Global config." 
        value = {
            amount = 1234.567
            anchorText = "anchor text"
            extra = {
                a = [
                    1,
                    2,
                    {
                        c = "d"
                    }
                ]
            }
        } 
    }
*/

namify

Converts a string into a form valid for a particular target, substituting an optional delimiter for sequences of invalid characters. Syntax:

{{namify '<target>' <input string> [delimiter]}}

If no delimiter is supplied, it will default to a target-specific value.

For example:

{{namify 's3' 'My Resource Name'}}

# my-resource-name

These targets are currently available:

| target | Description | default delimiter | | -------- | ----------------------------------------------------------------------------------------------- | ------------------- | | s3 | S3 bucket names | '-' |

throwif

Checks an condition. If true, throws an error before evauating the block content. Syntax:

{{#throwif <condition> <message>}}No Error!{{/throwif}}