NightlyCommit / twing

First-class Twig engine for Node.js
BSD 2-Clause "Simplified" License
199 stars 23 forks source link

Filter sort #517

Closed riklamme closed 11 months ago

riklamme commented 4 years ago

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:

riklamme commented 4 years ago

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

ericmorand commented 4 years ago

@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.

riklamme commented 4 years ago

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
ericmorand commented 11 months ago

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