Skip to content

Array comparisons (in and ==) do not conform to TwigPHP  #691

Open
@dorian-marchal

Description

@dorian-marchal

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:

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:

if (operator !== 'in' && operator !== 'not in' && operator !== '??') {
if (a && Array.isArray(a)) {
a = a.length;
}
if (b && Array.isArray(b)) {
b = b.length;
}
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions