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

handlebars-utils

v1.0.6

Published

Utils for handlebars helpers. Externalized from handlebars, to allow helpers to use the utils without having to depend on handlebars itself.

Downloads

1,299,092

Readme

handlebars-utils NPM version NPM monthly downloads NPM total downloads Linux Build Status

Utils for handlebars helpers. Externalized from handlebars, to allow helpers to use the utils without having to depend on handlebars itself.

Follow this project's author, Jon Schlinkert, for updates on this project and others.

(TOC generated by verb using markdown-toc)

Install

Install with npm:

$ npm install --save handlebars-utils

Usage

var utils = require('handlebars-utils');

API

.isBlock

Returns true if a helper is a block helper.

Params

  • options {Object}: Helper options object
  • returns {Boolean}

Example

Handlebars.registerHelper('example', function(options) {
  if (utils.isBlock(options)) {
    // do something if this is a block helper
  } else {
    // do something else if this is a not block helper
  }
});

.fn

Returns the given value or renders the block if it's a block helper.

Params

  • val {any}
  • options {Object}
  • context {Object}
  • returns {String}: Either returns the value, or renders the block.

Example

Handlebars.registerHelper('example', function(val, locals, options) {
  return utils.fn(val, locals, options);
});

.inverse

Returns the given value or renders the inverse block if it's a block helper.

Params

  • val {any}
  • options {Object}
  • context {Object}
  • returns {String}: Either returns the value, or renders the inverse block.

Example

Handlebars.registerHelper('example', function(val, locals, options) {
  return utils.inverse(val, locals, options);
});

.value

Gets the return value for a helper, by either rendering the block or inverse block if it's a block helper, or returning the given value (when truthy) or an empty string (when falsey) if it's a non-block expression.

Params

  • val {any}
  • options {Object}
  • context {Object}
  • returns {String}

Example

Handlebars.registerHelper('example', function(val, locals, options) {
  return utils.value(val, locals, options);
});

.isOptions

Returns true if the given value is a handlebar options object.

Params

  • val {Object}
  • returns {Boolean}

Example

Handlebars.registerHelper('example', function(val, locals, options) {
  if (utils.isOptions(locals)) {
    options = locals;
    locals = {};
  }
  // do stuff
});

.isUndefined

Returns true if the given value is undefined or is a handlebars options hash (which means that a value was not passed by the user).

Params

  • value {any}
  • returns {Boolean}

Example

Handlebars.registerHelper('example', function(val, options) {
  if (utils.isUndefined(val)) {
    return '';
  }
  // do stuff
});

.isApp

Returns true if an app propery is on the context, which means the context was created by assemble, templates, verb, or any other library that follows this convention.

Params

  • value {any}
  • returns {Boolean}

Example

Handlebars.registerHelper('example', function(val, options) {
  var context = options.hash;
  if (utils.isApp(this)) {
    context = Object.assign({}, this.context, context);
  }
  // do stuff
});

.options

Creates an options object from the context, locals and options. Handlebars' options.hash is merged onto the options, and if the context is created by templates, this.options will be merged onto the options as well.

Params

  • context {Object}
  • locals {Object}: Options or locals
  • options {Object}
  • returns {Boolean}

.context

Get the context to use for rendering.

Params

  • thisArg {Object}: Optional invocation context this
  • returns {Object}

.isObject

Returns true if the given value is an object.

Params

  • val {Object}
  • returns {Boolean}

Example

console.log(utils.isObject(null));
//=> false
console.log(utils.isObject([]));
//=> false
console.log(utils.isObject(function() {}));
//=> false
console.log(utils.isObject({}));
//=> true

.isEmpty

Returns true if the given value is "empty".

Params

  • value {any}
  • returns {Boolean}

Example

console.log(utils.isEmpty(0));
//=> false
console.log(utils.isEmpty(''));
//=> true
console.log(utils.isEmpty([]));
//=> true
console.log(utils.isEmpty({}));
//=> true

.result

Returns the given value. If the value is a function it will be called with the current context, otherwise the value is returned.

Params

  • val {any}
  • returns {any}

Example

console.log(utils.result('foo'));
//=> 'foo'
console.log(utils.result(function() {
  return 'foo';
}));
//=> 'foo'

.identity

Returns the given value as-is, unchanged.

Params

  • val {any}
  • returns {any}

Example

console.log(utils.result('foo'));
//=> 'foo'
console.log(utils.result(function() {
  return 'foo';
}));
//=> [function]

.isString

Return true if val is a non-empty string.

Params

  • val {any}: The value to check
  • returns {Boolean}

.arrayify

Cast the given val to an array.

Params

  • val {any}
  • returns {Array}

Example

console.log(utils.arrayify(''));
//=> []
console.log(utils.arrayify('foo'));
//=> ['foo']
console.log(utils.arrayify(['foo']));
//=> ['foo']

.tryParse

Try to parse the given string as JSON. Fails gracefully and always returns an object if the value cannot be parsed.

Params

  • string {String}
  • returns {Object}

About

Related projects

You might also be interested in these projects:

  • assemble: Get the rocks out of your socks! Assemble makes you fast at creating web projects… more | homepage
  • handlebars-helpers: More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate… more | homepage
  • template-helpers: Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or… more | homepage
  • templates: System for creating and managing template collections, and rendering templates with any node.js template engine… more | homepage

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Please read the contributing guide for advice on opening issues, pull requests, and coding standards.

Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Running tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

Author

Jon Schlinkert

License

Copyright © 2017, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on September 04, 2017.