2014-10-10 18:54:07 +04:00
|
|
|
// # Is Helper
|
|
|
|
// Usage: `{{#is "paged"}}`, `{{#is "index, paged"}}`
|
|
|
|
// Checks whether we're in a given context.
|
2020-04-08 18:56:37 +03:00
|
|
|
const {logging, i18n} = require('../services/proxy');
|
2020-03-30 23:23:02 +03:00
|
|
|
const _ = require('lodash');
|
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);
|
|
|
|
};
|