-
Notifications
You must be signed in to change notification settings - Fork 274
Open
Description
in
operator doesn't work like Twig PHP when comparing arrays
For example, this evaluates to "true" with Twig PHP but "false" with twig.js:
{{ ["foo", "bar"] in [['foo', 'bar'], ['qux', 'baz']] ? 'true' : 'false' }}
This falls in this test:
twig.js/src/twig.expression.operator.js
Lines 20 to 23 in 788437f
if (b.indexOf !== undefined) { | |
// String | |
return (a === b || a !== '') && b.indexOf(a) > -1; | |
} |
indexOf
is not undefined
for arrays.
Also, ==
operator is broken with array (it only seems to compare array lengths):
{{ ['foo'] == ['bar'] ? 'true' : 'false' }}
Evaluates to "true" with twig.js but "false" with Twig PHP.
This time, I think the corresponding code is here:
twig.js/src/twig.expression.operator.js
Lines 170 to 178 in 788437f
if (operator !== 'in' && operator !== 'not in' && operator !== '??') { | |
if (a && Array.isArray(a)) { | |
a = a.length; | |
} | |
if (b && Array.isArray(b)) { | |
b = b.length; | |
} | |
} |