From dca18b4131a3e56641a40cefec069f2e9bfcdb64 Mon Sep 17 00:00:00 2001 From: Antoine Veldhoven Date: Tue, 14 Nov 2023 00:05:44 +0100 Subject: [PATCH] Fix in/containment of array in array of arrays. --- src/twig.expression.operator.js | 5 +++++ test/test.expressions.js | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/twig.expression.operator.js b/src/twig.expression.operator.js index 027382e1..1ef23ab5 100644 --- a/src/twig.expression.operator.js +++ b/src/twig.expression.operator.js @@ -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); diff --git a/test/test.expressions.js b/test/test.expressions.js index 3563bd80..c0d61a71 100644 --- a/test/test.expressions.js +++ b/test/test.expressions.js @@ -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 () { @@ -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 () {