Skip to content

Commit

Permalink
Add reject filter
Browse files Browse the repository at this point in the history
  • Loading branch information
ogonkov committed Mar 29, 2020
1 parent ec8eae5 commit 48263d7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions nunjucks/src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var lib = require('./lib');
var r = require('./runtime');
var tests = require('./tests');

var exports = module.exports = {};

Expand Down Expand Up @@ -266,6 +267,16 @@ function random(arr) {

exports.random = random;

function reject(arr, testName = 'truthy', secondArg) {
const test = tests[testName] || tests.falsy;

return arr.filter(function applyToTest(item) {
return test(item, secondArg) === false;
});
}

exports.reject = reject;

function rejectattr(arr, attr) {
return arr.filter((item) => !item[attr]);
}
Expand Down
16 changes: 16 additions & 0 deletions tests/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,22 @@
finish(done);
});

it('reject', function(done) {
var context = {
numbers: [0, 1, 2, 3, 4, 5]
};

equal('{{ numbers | reject("odd") | join }}', context, '024');

equal('{{ numbers | reject("even") | join }}', context, '135');

equal('{{ numbers | reject("divisibleby", 3) | join }}', context, '1245');

equal('{{ numbers | reject() | join }}', context, '0');

finish(done);
});

it('rejectattr', function(done) {
var foods = [{
tasty: true
Expand Down

0 comments on commit 48263d7

Please sign in to comment.