twing icon indicating copy to clipboard operation
twing copied to clipboard

Filter sort

Open riklamme opened this issue 5 years ago • 3 comments

Maybe i missing something... maybe its not working as you espect. maybe im to long on this stuff, or maybe im just in idiot... or my engels is so bad at all....

Problem: `{% set fruits = [ { name: 'Apples', quantity: 5 }, { name: 'Oranges', quantity: 2 }, { name: 'Grapes', quantity: 4 } ] %}

    {% for fruit in fruits|filter(f => f.quantity > 2) %}
        {{ fruit.name }}
    {% endfor %}`

This doesnt work. It gives me the error "[1] SyntaxError: Unexpected token 'this'" Is there some genius out there, who can tell me whats wrong:

Used:

  • Node = 12.16.1
  • Twing = 4.0.3

riklamme avatar Jul 30 '20 08:07 riklamme

Yes i feel like an idiot, perfect feeling atm. Upgraded the twing module to > 5 and its working... excus6

riklamme avatar Jul 30 '20 08:07 riklamme

@riklamme , you're not an idiot. filter is supported by Twing 4 so it should work there too. I agree that we don't maintain anything less than the latest version, but it would be interesting to know what happens. I'll investigate out of curiosity.

ericmorand avatar Jul 30 '20 12:07 ericmorand

filter is working as suppost in 5. another issue is there with the sort filter, nunjucks has an attribute argument for selecting the propperty in your object

as workaround i created my own filter

 * Sorting filter
 * @example {{ array|sorting('order') }}
 */
const sorting = new TwingFilter('sorting', (map, prop) => {
    const compare = (a, b) => (a[1][prop] > b[1][prop] ? 1 : -1);

    if (!map) {
        console.error('TwingFilter: ObjectSort => array/map is undefined');
        return;
    } else if (!prop) {
        console.error('TwingFilter: ObjectSort => prop is undefined');
        return;
    } else {
        return Promise.resolve(new Map([...map.entries()].sort(compare)));
    }
});```



is there another way

riklamme avatar Jul 30 '20 13:07 riklamme

Not reproducible anymore in either Twing@5 or Twing@6. Probably fixed sometimes along the road.

ericmorand avatar Nov 27 '23 19:11 ericmorand