Skip to content

Commit 5915fe9

Browse files
Add support for arrow functions to filter filter.
1 parent f7c4666 commit 5915fe9

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/twig.filters.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,17 @@ module.exports = function (Twig) {
835835
},
836836
spaceless(value) {
837837
return value.replace(/>\s+</g, '><').trim();
838+
},
839+
filter(value, params) {
840+
if (is('Array', value)) {
841+
return value.filter(_a => {
842+
const data = {};
843+
data[params.params] = _a;
844+
845+
const template = Twig.exports.twig({data: params.body});
846+
return template.render(data) === 'true';
847+
});
848+
}
838849
}
839850
};
840851

test/test.filters.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,18 @@ describe('Twig.js Filters ->', function () {
859859
});
860860
});
861861

862+
describe('filter ->', function () {
863+
it('should filter an array (with perenthesis)', function () {
864+
let testTemplate = twig({data: '{{ [1,5,2,7,8]|filter((f) => f % 2 == 0) }}'});
865+
testTemplate.render().should.equal('2,8');
866+
});
867+
868+
it('should filter an array (without perenthesis)', function () {
869+
let testTemplate = twig({data: '{{ [1,5,2,7,8]|filter(f => f % 2 == 0) }}'});
870+
testTemplate.render().should.equal('2,8');
871+
});
872+
});
873+
862874
it('should chain', function () {
863875
const testTemplate = twig({data: '{{ ["a", "b", "c"]|keys|reverse }}'});
864876
testTemplate.render().should.equal('2,1,0');

0 commit comments

Comments
 (0)