Skip to content

Commit

Permalink
Add support for selectattr and rejectattr filters.
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm committed Feb 4, 2015
1 parent 24024b5 commit bab41dc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/filters.js
Expand Up @@ -186,6 +186,18 @@ var filters = {
return arr[Math.floor(Math.random() * arr.length)];
},

rejectattr: function(arr, attr) {
return arr.filter(function (item) {
return !item[attr];
});
},

selectattr: function(arr, attr) {
return arr.filter(function (item) {
return !!item[attr];
});
},

replace: function(str, old, new_, maxCount) {
if (old instanceof RegExp) {
return str.replace(old, new_);
Expand Down
12 changes: 12 additions & 0 deletions tests/filters.js
Expand Up @@ -210,6 +210,18 @@
finish(done);
});

it('rejectattr', function(done) {
var foods = [{tasty: true}, {tasty: false}, {tasty: true}];
equal('{{ foods | rejectattr("tasty") | length }}', {foods: foods}, '1');
finish(done);
});

it('selectattr', function(done) {
var foods = [{tasty: true}, {tasty: false}, {tasty: true}];
equal('{{ foods | selectattr("tasty") | length }}', {foods: foods}, '2');
finish(done);
});

it('replace', function(done) {
equal('{{ "aaabbbccc" | replace("a", "x") }}', 'xxxbbbccc');
equal('{{ "aaabbbccc" | replace("a", "x", 2) }}', 'xxabbbccc');
Expand Down

0 comments on commit bab41dc

Please sign in to comment.