Implements the #has Block helper

closes #2115
- Added new #has block helper
- Added several tests for #has helper
This commit is contained in:
Fabian Becker 2014-02-08 21:12:04 +01:00 committed by Hannah Wolfe
parent 7a6f4811f6
commit 12f8f99088
2 changed files with 91 additions and 0 deletions

View File

@ -572,6 +572,37 @@ coreHelpers.foreach = function (context, options) {
return ret;
};
// ### Has Helper
// `{{#has tag="video, music"}}`
// Checks whether a post has at least one of the tags
coreHelpers.has = function (options) {
var tags = _.pluck(this.tags, 'name'),
tagList = options && options.hash ? options.hash.tag : false;
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);
}
if (!tagList) {
errors.logWarn("Invalid or no attribute given to has helper");
return;
}
if (tagList && evaluateTagList(tagList, tags)) {
return options.fn(this);
}
return options.inverse(this);
};
// ### Pagination Helper
// `{{pagination}}`
// Outputs previous and next buttons, along with info about the current page
@ -702,6 +733,8 @@ registerHelpers = function (adminHbs, assetHash) {
registerThemeHelper('foreach', coreHelpers.foreach);
registerThemeHelper('has', coreHelpers.has);
registerThemeHelper('page_url', coreHelpers.page_url);
registerThemeHelper('pageUrl', coreHelpers.pageUrl);

View File

@ -427,6 +427,64 @@ describe('Core Helpers', function () {
});
});
describe('has Block Helper', function () {
it('has loaded has block helper', function () {
should.exist(handlebars.helpers.has);
});
it('should handle tag list that validates true', function () {
var fn = sinon.spy(),
inverse = sinon.spy();
helpers.has.call(
{tags: [{ name: 'foo'}, { name: 'bar'}, { name: 'baz'}]},
{hash: { tag: 'invalid, bar, wat'}, fn: fn, inverse: inverse}
);
fn.called.should.be.true;
inverse.called.should.be.false;
});
it('should handle tags with case-insensitivity', function () {
var fn = sinon.spy(),
inverse = sinon.spy();
helpers.has.call(
{tags: [{ name: 'ghost'}]},
{hash: { tag: 'GhoSt'}, fn: fn, inverse: inverse}
);
fn.called.should.be.true;
inverse.called.should.be.false;
});
it('should handle tag list that validates false', function () {
var fn = sinon.spy(),
inverse = sinon.spy();
helpers.has.call(
{tags: [{ name: 'foo'}, { name: 'bar'}, { name: 'baz'}]},
{hash: { tag: 'much, such, wow'}, fn: fn, inverse: inverse}
);
fn.called.should.be.false;
inverse.called.should.be.true;
});
it('should not do anything when an invalid attribute is given', function () {
var fn = sinon.spy(),
inverse = sinon.spy();
helpers.has.call(
{tags: [{ name: 'foo'}, { name: 'bar'}, { name: 'baz'}]},
{hash: { invalid: 'nonsense'}, fn: fn, inverse: inverse}
);
fn.called.should.be.false;
inverse.called.should.be.false;
});
});
describe('url Helper', function () {
it('has loaded url helper', function () {
should.exist(handlebars.helpers.url);