Twig icon indicating copy to clipboard operation
Twig copied to clipboard

Add "some" and "every" filters

Open Jean-Beru opened this issue 3 years ago • 2 comments

This PR adds 2 new tests inspired from Javascript's functions: some and every.

Usage :

Value {{ ([1, 2, 3] has some v => v == 2) ? 'found' : 'not found' }}

Key {{ ([1, 2, 3] has some (v, k) => k == 2) ? 'found' : 'not found' }}

Every values {{ ([1, 2, 3] has every v => 42 > v) ? 'match' : 'do not match' }}

Every keys {{ ([1, 2, 3] has every (v, k) => 42 > k) ? 'match' : 'do not match' }}

Jean-Beru avatar Mar 24 '22 16:03 Jean-Beru

Something returning a boolean looks like a use case for a Twig test rather than a Twig filter.

stof avatar Mar 24 '22 16:03 stof

You're right. I changed these filter to binary expression (like starts with). The \Twig\ExpressionParser::parseExpression had to be updated to allow arrow functions in right operator.

Jean-Beru avatar Mar 25 '22 10:03 Jean-Beru

Thank you @Jean-Beru.

fabpot avatar Dec 26 '22 16:12 fabpot

This is still missing in the docs.

jdreesen avatar Dec 27 '22 13:12 jdreesen

@jdreesen Would you like to work on some docs? Or maybe @Jean-Beru?

fabpot avatar Dec 27 '22 15:12 fabpot

I forgot it, my bad. This is done in https://github.com/twigphp/Twig/pull/3797.

Jean-Beru avatar Dec 28 '22 13:12 Jean-Beru