2014-10-10 18:54:07 +04:00
|
|
|
// # Has Helper
|
|
|
|
// Usage: `{{#has tag="video, music"}}`, `{{#has author="sam, pat"}}`
|
|
|
|
//
|
|
|
|
// Checks if a post has a particular property
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
var proxy = require('./proxy'),
|
|
|
|
_ = require('lodash'),
|
|
|
|
logging = proxy.logging,
|
2017-08-17 14:04:11 +03:00
|
|
|
i18n = proxy.i18n,
|
2017-08-17 14:22:29 +03:00
|
|
|
validAttrs = ['tag', 'author', 'slug', 'id', 'number', 'index', 'any', 'all'];
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2017-08-15 18:00:17 +03:00
|
|
|
function evaluateTagList(expr, tags) {
|
|
|
|
return expr.split(',').map(function (v) {
|
|
|
|
return v.trim();
|
|
|
|
}).reduce(function (p, c) {
|
|
|
|
return p || (_.findIndex(tags, function (item) {
|
|
|
|
// Escape regex special characters
|
|
|
|
item = item.replace(/[\-\/\\\^$*+?.()|\[\]{}]/g, '\\$&');
|
|
|
|
item = new RegExp('^' + item + '$', 'i');
|
|
|
|
return item.test(c);
|
|
|
|
}) !== -1);
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function evaluateAuthorList(expr, author) {
|
|
|
|
var authorList = expr.split(',').map(function (v) {
|
|
|
|
return v.trim().toLocaleLowerCase();
|
|
|
|
});
|
|
|
|
|
|
|
|
return _.includes(authorList, author.toLocaleLowerCase());
|
|
|
|
}
|
|
|
|
|
|
|
|
function evaluateIntegerMatch(expr, integer) {
|
|
|
|
var nthMatch = expr.match(/^nth:(\d+)/);
|
|
|
|
if (nthMatch) {
|
|
|
|
return integer % parseInt(nthMatch[1], 10) === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return expr.split(',').reduce(function (bool, _integer) {
|
|
|
|
return bool || parseInt(_integer, 10) === integer;
|
|
|
|
}, false);
|
|
|
|
}
|
|
|
|
|
2017-08-15 18:25:06 +03:00
|
|
|
function evaluateStringMatch(expr, str, ci) {
|
|
|
|
if (ci) {
|
|
|
|
return expr && str && expr.toLocaleLowerCase() === str.toLocaleLowerCase();
|
|
|
|
}
|
|
|
|
|
|
|
|
return expr === str;
|
|
|
|
}
|
|
|
|
|
2017-08-17 14:22:29 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {String} type - either some or every - the lodash function to use
|
|
|
|
* @param {String} expr - the attribute value passed into {{#has}}
|
|
|
|
* @param {Object} obj - "this" context from the helper
|
|
|
|
* @param {Object} data - global params
|
|
|
|
*/
|
|
|
|
function evaluateList(type, expr, obj, data) {
|
|
|
|
return expr.split(',').map(function (prop) {
|
|
|
|
return prop.trim().toLocaleLowerCase();
|
|
|
|
})[type](function (prop) {
|
|
|
|
if (prop.match(/^@/)) {
|
|
|
|
return _.has(data, prop.replace(/@/, '')) && !_.isEmpty(_.get(data, prop.replace(/@/, '')));
|
|
|
|
} else {
|
|
|
|
return _.has(obj, prop) && !_.isEmpty(_.get(obj, prop));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-04 19:07:35 +03:00
|
|
|
module.exports = function has(options) {
|
2014-10-10 18:54:07 +04:00
|
|
|
options = options || {};
|
|
|
|
options.hash = options.hash || {};
|
2017-08-17 14:04:11 +03:00
|
|
|
options.data = options.data || {};
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2017-08-17 14:04:11 +03:00
|
|
|
var self = this,
|
|
|
|
attrs = _.pick(options.hash, validAttrs),
|
2017-08-17 14:22:29 +03:00
|
|
|
data = _.pick(options.data, ['blog', 'config', 'labs']),
|
2017-08-17 14:04:11 +03:00
|
|
|
checks = {
|
|
|
|
tag: function () { return attrs.tag && evaluateTagList(attrs.tag, _.map(self.tags, 'name')) || false; },
|
|
|
|
author: function () { return attrs.author && evaluateAuthorList(attrs.author, _.get(self, 'author.name')) || false; },
|
|
|
|
number: function () { return attrs.number && evaluateIntegerMatch(attrs.number, options.data.number) || false; },
|
|
|
|
index: function () { return attrs.index && evaluateIntegerMatch(attrs.index, options.data.index) || false; },
|
|
|
|
slug: function () { return attrs.slug && evaluateStringMatch(attrs.slug, self.slug, true) || false; },
|
2017-08-17 14:22:29 +03:00
|
|
|
id: function () { return attrs.id && evaluateStringMatch(attrs.id, self.id, true) || false; },
|
|
|
|
any: function () { return attrs.any && evaluateList('some', attrs.any, self, data) || false; },
|
|
|
|
all: function () { return attrs.all && evaluateList('every', attrs.all, self, data) || false; }
|
2017-08-17 14:04:11 +03:00
|
|
|
},
|
|
|
|
result;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2017-08-17 14:04:11 +03:00
|
|
|
if (_.isEmpty(attrs)) {
|
2016-10-04 18:33:43 +03:00
|
|
|
logging.warn(i18n.t('warnings.helpers.has.invalidAttribute'));
|
2014-10-10 18:54:07 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-17 14:04:11 +03:00
|
|
|
result = _.some(attrs, function (value, attr) {
|
|
|
|
return checks[attr]();
|
|
|
|
});
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2017-08-17 14:04:11 +03:00
|
|
|
if (result) {
|
2014-10-10 18:54:07 +04:00
|
|
|
return options.fn(this);
|
|
|
|
}
|
2017-08-17 14:04:11 +03:00
|
|
|
|
2014-10-10 18:54:07 +04:00
|
|
|
return options.inverse(this);
|
|
|
|
};
|