UNPKG

1.23 kBJavaScriptView Raw
1'use strict';
2
3var _ = require('lodash');
4
5/**
6 * Yield block only if all arguments are valid
7 *
8 * @example
9 * {{#all items theme_settings.optionA theme_settings.optionB}} ... {{/all}}
10 */
11function helper(paper) {
12 paper.handlebars.registerHelper('all', function () {
13
14 var args = [], opts, result;
15
16 // Translate arguments to array safely
17 for (var i = 0; i < arguments.length; i++) {
18 args.push(arguments[i]);
19 }
20
21 // Take the last argument (content) out of testing array
22 opts = args.pop();
23
24 // Check if all the arguments are valid / truthy
25 result = _.all(args, function (arg) {
26 if (_.isArray(arg)) {
27 return !!arg.length;
28 }
29 // If an empty object is passed, arg is false
30 else if (_.isEmpty(arg) && _.isObject(arg)) {
31 return false;
32 }
33 // Everything else
34 else {
35 return !!arg;
36 }
37 });
38
39 // If everything was valid, then "all" condition satisfied
40 if (result) {
41 return opts.fn(this);
42 } else {
43 return opts.inverse(this);
44 }
45 });
46}
47
48module.exports = helper;