-
Notifications
You must be signed in to change notification settings - Fork 36
/
any.js
54 lines (47 loc) · 1.46 KB
/
any.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
'use strict';
const utils = require('./3p/utils');
const deepMatches = require('./3p/utils/lib/deepMatches');
/**
* Yield block if any object within a collection matches supplied predicate
*
* @example
* {{#any items selected=true}} ... {{/any}}
*/
const factory = () => {
return function (...args) {
let any;
// Take the last arg which is a Handlebars options object out of args array
const opts = args.pop();
const predicate = opts.hash;
if (!utils.isEmpty(predicate)) {
if (utils.isArray(args[0])) {
// With options hash, we check the contents of first argument
any = args[0].some((v) => deepMatches(v, predicate));
}
} else {
// DEPRECATED: Moved to #or helper
// Without options hash, we check all the arguments
any = args.some(function (arg) {
if (utils.isArray(arg)) {
return !!arg.length;
}
// If an empty object is passed, arg is false
else if (utils.isEmpty(arg) && utils.isObject(arg)) {
return false;
}
// Everything else
else {
return !!arg;
}
});
}
if (any) {
return opts.fn(this);
}
return opts.inverse(this);
};
};
module.exports = [{
name: 'any',
factory: factory,
}];