Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

FILTER ignores variables from the context #115

Open
max-prtsr opened this issue Jan 9, 2023 · 5 comments
Open

FILTER ignores variables from the context #115

max-prtsr opened this issue Jan 9, 2023 · 5 comments

Comments

@max-prtsr
Copy link

    const context = {
      prices: [
        { model: 'BMW', price: 60000 },
        { model: 'Audi', price: 50000 }
      ],
      selectedModel: 'BMW'
    }
    expect(axel.compile('FILTER(_.model = "BMW", prices)')(context)).toEqual([{ model: 'BMW', price: 60000 }])
    expect(axel.compile('FILTER(_.model = selectedModel, prices)')(context)).toEqual([{ model: 'BMW', price: 60000 }])

The first one works fine, the second doesn't work :(

What do I do wrong?

@DeLaGuardo
Copy link
Contributor

This is a bug. I'll try to fix it this weekend.

Happen because there is no concept of implicit visibility scopes in axel-f. As a workaround, you can use WITH to build such scope explicitly: WITH(x, selectedModel, FILTER(_.model = x, prices)). Not a very nice solution but it should work in any case.

@max-prtsr
Copy link
Author

max-prtsr commented Jan 10, 2023

@DeLaGuardo looks like I can make it work using FN

FILTER(FN(item, item.model = selectedModel.model), prices)

I found a solution here:
https://github.com/xapix-io/axel-f#working-with-collections-breaking

Maybe we should update the documentation?

But using collection methods like FILTER without WITH / FN would be really great

@max-prtsr
Copy link
Author

max-prtsr commented Jan 10, 2023

Also filter+fn doesn't work if we pass _ as the first argument :(
E.g. FILTER(FN(_, _.model = selectedModel.model), prices)
So using just filter would be nice

@DeLaGuardo
Copy link
Contributor

So far, the best workaround is to use WITH to create a named closure. Unfortunately, for now, this is the only reliable way to access the "global" environment from within the lambda function. I'll be back with a proper solution later this week.

@max-prtsr
Copy link
Author

@DeLaGuardo any news? :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants