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,
|
|
|
|
i18n = proxy.i18n;
|
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-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 || {};
|
|
|
|
|
2016-06-11 21:23:27 +03:00
|
|
|
var tags = _.map(this.tags, 'name'),
|
2014-10-10 18:54:07 +04:00
|
|
|
author = this.author ? this.author.name : null,
|
2017-08-15 18:00:17 +03:00
|
|
|
number = options.data.number,
|
|
|
|
index = options.data.index,
|
2017-08-15 18:25:06 +03:00
|
|
|
slug = this.slug,
|
|
|
|
id = this.id,
|
2014-10-10 18:54:07 +04:00
|
|
|
tagList = options.hash.tag || false,
|
|
|
|
authorList = options.hash.author || false,
|
2017-08-15 18:00:17 +03:00
|
|
|
numberList = options.hash.number || false,
|
|
|
|
indexList = options.hash.index || false,
|
2017-08-15 18:25:06 +03:00
|
|
|
slugParam = options.hash.slug || false,
|
|
|
|
idParam = options.hash.id || false,
|
2014-10-10 18:54:07 +04:00
|
|
|
tagsOk,
|
2017-08-15 18:00:17 +03:00
|
|
|
authorOk,
|
|
|
|
numberOk,
|
2017-08-15 18:25:06 +03:00
|
|
|
indexOk,
|
|
|
|
slugOk,
|
|
|
|
idOk;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2017-08-15 18:25:06 +03:00
|
|
|
if (!tagList && !authorList && !numberList && !indexList && !slugParam && !idParam) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
tagsOk = tagList && evaluateTagList(tagList, tags) || false;
|
|
|
|
authorOk = authorList && evaluateAuthorList(authorList, author) || false;
|
2017-08-15 18:00:17 +03:00
|
|
|
numberOk = numberList && evaluateIntegerMatch(numberList, number) || false;
|
|
|
|
indexOk = indexList && evaluateIntegerMatch(indexList, index) || false;
|
2017-08-15 18:25:06 +03:00
|
|
|
slugOk = slugParam && evaluateStringMatch(slugParam, slug, true) || false;
|
|
|
|
idOk = idParam && evaluateStringMatch(idParam, id, true) || false;
|
2014-10-10 18:54:07 +04:00
|
|
|
|
2017-08-15 18:25:06 +03:00
|
|
|
if (tagsOk || authorOk || numberOk || indexOk || slugOk || idOk) {
|
2014-10-10 18:54:07 +04:00
|
|
|
return options.fn(this);
|
|
|
|
}
|
|
|
|
return options.inverse(this);
|
|
|
|
};
|