2014-10-10 18:54:07 +04:00
|
|
|
// # Is Helper
|
|
|
|
// Usage: `{{#is "paged"}}`, `{{#is "index, paged"}}`
|
|
|
|
// Checks whether we're in a given context.
|
|
|
|
var _ = require('lodash'),
|
|
|
|
errors = require('../errors'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../i18n'),
|
2014-10-10 18:54:07 +04:00
|
|
|
is;
|
|
|
|
|
|
|
|
is = function (context, options) {
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
var currentContext = options.data.root.context;
|
|
|
|
|
|
|
|
if (!_.isString(context)) {
|
2015-11-12 15:29:45 +03:00
|
|
|
errors.logWarn(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);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = is;
|