From dca18b4131a3e56641a40cefec069f2e9bfcdb64 Mon Sep 17 00:00:00 2001 From: Antoine Veldhoven Date: Tue, 14 Nov 2023 00:05:44 +0100 Subject: [PATCH 1/2] 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 () { From 7ab4254d8665908ad0a5ab79917535fb28f86582 Mon Sep 17 00:00:00 2001 From: Antoine Veldhoven Date: Tue, 14 Nov 2023 00:12:22 +0100 Subject: [PATCH 2/2] Fix casting of arrays. --- src/twig.expression.operator.js | 13 +++++++------ test/test.expressions.js | 6 ++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/twig.expression.operator.js b/src/twig.expression.operator.js index 1ef23ab5..be84c914 100644 --- a/src/twig.expression.operator.js +++ b/src/twig.expression.operator.js @@ -178,12 +178,13 @@ module.exports = function (Twig) { } if (operator !== 'in' && operator !== 'not in' && operator !== '??') { - if (a && Array.isArray(a)) { - a = a.length; - } - - if (operator !== '?' && (b && Array.isArray(b))) { - b = b.length; + if (operator !== '?' && a && Array.isArray(a) && typeof b === 'boolean') { + a = Twig.lib.boolval(a); + } else if (operator !== '?' && b && Array.isArray(b) && typeof a === 'boolean') { + b = Twig.lib.boolval(b); + } else if (operator === '==' && ((a && Array.isArray(a)) || (b && Array.isArray(b)))) { + a = JSON.stringify(a); + b = JSON.stringify(b); } } diff --git a/test/test.expressions.js b/test/test.expressions.js index c0d61a71..dc40fbc8 100644 --- a/test/test.expressions.js +++ b/test/test.expressions.js @@ -325,7 +325,13 @@ describe('Twig.js Expressions ->', function () { it('should correctly cast arrays', function () { const testTemplate = twig({data: '{{ a == true }}'}); testTemplate.render({a: ['value']}).should.equal('true'); + testTemplate.render({a: ['value', 'another']}).should.equal('true'); testTemplate.render({a: []}).should.equal('false'); + + const testTemplate2 = twig({data: '{{ true == a }}'}); + testTemplate2.render({a: ['value']}).should.equal('true'); + testTemplate2.render({a: ['value', 'another']}).should.equal('true'); + testTemplate2.render({a: []}).should.equal('false'); }); it('should correctly cast arrays in control structures', function () {