useful-handlebars-helpers
Version:
More than 150 Handlebars helpers in 17 categories. Works in browsers and in Node.js.
66 lines (57 loc) • 1.68 kB
JavaScript
;
const util = require('handlebars-utils');
const object = require('./object');
const array = require('./array');
const helpers = module.exports;
/**
* Inline, subexpression, or block helper that returns true (or the block)
* if the given collection is empty, or false (or the inverse block, if
* supplied) if the collection is not empty.
*
* ```handlebars
* <!-- array: [] -->
* {{#isEmpty array}}AAA{{else}}BBB{{/isEmpty}}
* <!-- results in: 'AAA' -->
*
* <!-- array: [] -->
* {{isEmpty array}}
* <!-- results in: true -->
* ```
* @param {Object} `collection`
* @param {Object} `options`
* @return {String}
* @block
* @api public
*/
helpers.isEmpty = function(collection, options) {
if (!util.isOptions(options)) {
options = collection;
return util.fn(true, this, options);
}
if (Array.isArray(collection) && !collection.length) {
return util.fn(true, this, options);
}
const isEmpty = typeof collection === 'object' && !Object.keys(collection).length;
return util.value(isEmpty, this, options);
};
/**
* Block helper that iterates over an array or object. If
* an array is given, `.forEach` is called, or if an object
* is given, `.forOwn` is called, otherwise the inverse block
* is returned.
*
* @param {Object|Array} `collection` The collection to iterate over
* @param {Object} `options`
* @return {String}
* @block
* @api public
*/
helpers.iterate = function(collection, options) {
if (Array.isArray(collection)) {
return array.forEach.apply(null, arguments);
}
if (util.isObject(collection)) {
return object.forOwn.apply(null, arguments);
}
return options.inverse(this);
};