UNPKG

1.5 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 * {{#any items selected=true}} ... {{/any}}
10 */
11function helper(paper) {
12 paper.handlebars.registerHelper('any', function () {
13
14 var args = [],
15 opts,
16 predicate,
17 any;
18
19 // Translate arguments to array safely
20 for (var i = 0; i < arguments.length; i++) {
21 args.push(arguments[i]);
22 }
23
24 // Take the last argument (content) out of testing array
25 opts = args.pop();
26 predicate = opts.hash;
27
28 if (!_.isEmpty(predicate)) {
29 // With options hash, we check the contents of first argument
30 any = _.any(args[0], predicate);
31 } else {
32 // DEPRECATED: Moved to #or helper
33 // Without options hash, we check all the arguments
34 any = _.any(args, function (arg) {
35 if (_.isArray(arg)) {
36 return !!arg.length;
37 }
38 // If an empty object is passed, arg is false
39 else if (_.isEmpty(arg) && _.isObject(arg)) {
40 return false;
41 }
42 // Everything else
43 else {
44 return !!arg;
45 }
46 });
47 }
48
49 if (any) {
50 return opts.fn(this);
51 }
52
53 return opts.inverse(this);
54 });
55}
56
57module.exports = helper;