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

hbs-jsonpath-helper

v0.2.0

Published

A simple handlebars helper that enables you to use jsonpath (to access and traverse nested data) within your templates

Downloads

141

Readme

hbs-jsonpath-helper

A simple handlebars helper that enables you to use jsonpath (to access and traverse nested data) within your templates

Resources:

Installation & Usage

npm install --save handlebars hbs-jsonpath-helper
const Handlebars = require('handlebars');
const jsonPathHelper = require('hbs-jsonpath-helper');

jsonPathHelper.register();
// Or, you can explicitly pass a Handlebars instance
jsonPathHelper.register(Handlebars);

// Compile template
const template = Handlebars.compile(`
{{jp-get "$.store.book[0].title"}}
`);

// Data passed to the template
const data = { /* ... */ }

// Use compiled template:
const str = template(data); // "Nigel Rees"

API & Examples

For the following data source

const data = {
  store: {
    book: [
      {
        category: 'reference',
        author: 'Nigel Rees',
        title: 'Sayings of the Century',
        price: 8.95,
      },
      {
        category: 'fiction',
        author: 'Evelyn Waugh',
        title: 'Sword of Honour',
        price: 12.99,
      },
      {
        category: 'fiction',
        author: 'Herman Melville',
        title: 'Moby Dick',
        isbn: '0-553-21311-3',
        price: 8.99,
      },
      {
        category: 'fiction',
        author: 'J. R. R. Tolkien',
        title: 'The Lord of the Rings',
        isbn: '0-395-19395-8',
        price: 22.99,
      },
    ],
    bicycle: {
      color: 'red',
      price: 19.95,
    },
  },
};

jp-get

Retrieve a value from current context using jsonpath

Template:

<h1>Books</h1>
<ul>
  {{#each (jp-get "$.store.book[*]")}}
      <li>
        <div class="title">{{title}}</div>
        <div class="author">{{author}}</div>
      </li>
  {{/each}}
</ul>

Result:

<h1>Books</h1>
<ul>
      <li>
        <div class="title">Sayings of the Century</div>
        <div class="author">Nigel Rees</div>
      </li>
      <li>
        <div class="title">Sword of Honour</div>
        <div class="author">Evelyn Waugh</div>
      </li>
      <li>
        <div class="title">Moby Dick</div>
        <div class="author">Herman Melville</div>
      </li>
      <li>
        <div class="title">The Lord of the Rings</div>
        <div class="author">J. R. R. Tolkien</div>
      </li>
</ul>

jp-get-in

Retrieve a value from specified target using jsonpath

Template:

<h1>Books</h1>
<ul>
  {{#each (jp-get-in "book[*]" store)}}
      <li>
        <div class="title">{{title}}</div>
        <div class="author">{{author}}</div>
      </li>
  {{/each}}
</ul>

Result:

<h1>Books</h1>
<ul>
      <li>
        <div class="title">Sayings of the Century</div>
        <div class="author">Nigel Rees</div>
      </li>
      <li>
        <div class="title">Sword of Honour</div>
        <div class="author">Evelyn Waugh</div>
      </li>
      <li>
        <div class="title">Moby Dick</div>
        <div class="author">Herman Melville</div>
      </li>
      <li>
        <div class="title">The Lord of the Rings</div>
        <div class="author">J. R. R. Tolkien</div>
      </li>
</ul>

jp-descend

Use the value derived from current context using jsonpath as the context for embedded block

Template:

<h1>Authors</h1>
<ul>
  {{#jp-descend "$.store.book[*].author"}}
      {{#each .}}
          <li>{{.}}</li>
      {{/each}}
  {{/jp-descend}}
</ul>

Result:

<h1>Authors</h1>
<ul>
          <li>Nigel Rees</li>
          <li>Evelyn Waugh</li>
          <li>Herman Melville</li>
          <li>J. R. R. Tolkien</li>
</ul>

jp-descend-in

Use the value derived from specified target using jsonpath as the context for embedded block

Template:

<h1>Authors</h1>
<ul>
  {{#jp-descend-in "book[*].author" store}}
      {{#each .}}
          <li>{{.}}</li>
      {{/each}}
  {{/jp-descend-in}}
</ul>

Result:

<h1>Authors</h1>
<ul>
          <li>Nigel Rees</li>
          <li>Evelyn Waugh</li>
          <li>Herman Melville</li>
          <li>J. R. R. Tolkien</li>
</ul>

License

MIT