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 () {