UNPKG

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