2014-10-10 18:54:07 +04:00
|
|
|
// # Is Helper
|
|
|
|
// Usage: `{{#is "paged"}}`, `{{#is "index, paged"}}`
|
|
|
|
// Checks whether we're in a given context.
|
2017-04-04 19:07:35 +03:00
|
|
|
var proxy = require('./proxy'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
logging = proxy.logging,
|
|
|
|
i18n = proxy.i18n;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
module.exports = function is(context, options) {
|
2014-10-10 18:54:07 +04:00
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
var currentContext = options.data.root.context;
|
|
|
|
|
|
|
|
if (!_.isString(context)) {
|
2016-10-04 18:33:43 +03:00
|
|
|
logging.warn(i18n.t('warnings.helpers.is.invalidAttribute'));
|
2014-10-10 18:54:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function evaluateContext(expr) {
|
|
|
|
return expr.split(',').map(function (v) {
|
|
|
|
return v.trim();
|
|
|
|
}).reduce(function (p, c) {
|
2016-06-11 21:23:27 +03:00
|
|
|
return p || _.includes(currentContext, c);
|
2014-10-10 18:54:07 +04:00
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (evaluateContext(context)) {
|
|
|
|
return options.fn(this);
|
|
|
|
}
|
|
|
|
return options.inverse(this);
|
|
|
|
};
|
|
|
|
|