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

hogan-var

v1.0.1

Published

Analyze variable usages in Hogan.js templates

Downloads

2

Readme

hogan-var

Analyzes variable usages in Hogan.js templates. Useful for static analysis, documentation generation, and sample rendering of arbitrary templates.

Installation

npm install hogan-var

Usage

const hogan = require('hogan.js');
const scanVariables = require('hogan-var').default;

const text = `Welcome to {{{placeHtml}}}!
  {{#names.length}}
  Greetings to:
  {{#names}}
  - {{.}} {{>icon}}
  {{/names}}
  {{/names.length}}`;
const tree = hogan.parse(hogan.scan(text), text);
const vars = scanVariables(tree);
console.log(JSON.stringify(vars));

The code above outputs the following:

{
  "placeHtml": {
    "scalar": true,
    "unescaped": true
  },
  "names": {
    "array": true,
    "members": {
      "length": {
        "scalar": true,
        "section": true,
        "noninverted": true
      }
    },
    "section": true,
    "noninverted": true,
    "elements": {
      "scalar": true,
      "escaped": true
    }
  },
  "icon": {
    "partial": true
  }
}

API Reference

scanVariables(tokens, options, context, parentContexts) ⇒

Walks an array or tree of Hogan.js tokens (returned by scan or parse, respectively) and returns a mapping of variable references to objects describing the contexts in which they are used. Using a parsed token tree is recommended for obtaining the most accurate interpretation.

Each variable maps to an object that may contain the following properties:

  • name (string): The name of the variable, if includeName is true in the options
  • scalar (boolean): The variable was used in a scalar context: {{v}}, {{{v}}}, {{&v}}
  • escaped (boolean): The variable was used in an escaped reference: {{v}}
  • unescaped (boolean): The variable was used in an unescaped reference: {{{v}}}, {{&v}}
  • section (boolean): The variable was used in a section: {{#v}}, {{^v}}
  • noninverted (boolean): The variable was used in a normal/non-inverted section: {{#v}}
  • inverted (boolean): The variable was used in an inverted section: {{^v}}
  • partial (boolean): The variable/filename was used in a partial: {{>v}} (top-level context only)
  • array (boolean): The variable was used as an array in a section (containing {{.}})
  • members (Object): An object mapping member variable references to usage context
  • nested (Object): An object mapping nested variable references to usage context

The difference between members and nested is that members are known to be members of the containing variable, generally because they were referenced using dot-notation. Nested references may be references to members of the containing variable or references to members of any containing scope, including the top-level scope.

The following options can be used to control interpretation:

  • arrayLengthMember (string): name of member reference assumed to be an array length, default length
  • collapseNested (boolean): whether to assume section references with same name as an outer variable refer to the same variable, default true
  • includeName (boolean): whether to (redundantly) include the name of the variable in its property map, default false

Kind: global function
Returns: the populated context argument

| Param | Type | Description | | --- | --- | --- | | tokens | Array | an array or tree of Hogan.js tokens | | options | Object | options controlling interpretation | | context | Object | receives variables for the current scope, defaults to an empty object | | parentContexts | Array | an array of containing scopes, starting with the root |

License

hogan-var is available under the ISC license.