Skip to content

Commit

Permalink
Fix in/containment of array in array of arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
antoineveldhoven committed Nov 13, 2023
1 parent f08598b commit dca18b4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/twig.expression.operator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ module.exports = function (Twig) {
return null;
}

if (b && Array.isArray(b)) {
// Array
return JSON.stringify(b).includes(JSON.stringify(a));
}

if (b.indexOf !== undefined) {
// String
return (a === b || a !== '') && b.includes(a);
Expand Down
12 changes: 12 additions & 0 deletions test/test.expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,12 @@ describe('Twig.js Expressions ->', function () {

testTemplate = twig({data: '{{ "d" in ["a", "b", "c"] }}'});
testTemplate.render().should.equal(false.toString());

testTemplate = twig({data: '{{ ["a", "b"] in [["a", "b"], ["c", "d"]] }}'});
testTemplate.render().should.equal(true.toString());

testTemplate = twig({data: '{{ ["a", "c"] in [["a", "b"], ["c", "d"]] }}'});
testTemplate.render().should.equal(false.toString());
});

it('should support not in/containment functionality for arrays', function () {
Expand All @@ -391,6 +397,12 @@ describe('Twig.js Expressions ->', function () {

testTemplate = twig({data: '{{ "d" not in ["a", "b", "c"] }}'});
testTemplate.render().should.equal(true.toString());

testTemplate = twig({data: '{{ ["a", "b"] not in [["a", "b"], ["c", "d"]] }}'});
testTemplate.render().should.equal(false.toString());

testTemplate = twig({data: '{{ ["a", "c"] not in [["a", "b"], ["c", "d"]] }}'});
testTemplate.render().should.equal(true.toString());
});

it('should support in/containment functionality for strings', function () {
Expand Down

0 comments on commit dca18b4

Please sign in to comment.