ventojs / vento

🌬 A template engine for Deno & Node
https://vento.js.org/
MIT License
212 stars 12 forks source link

Issues mixing JavaScript with filters in set #8

Closed bpollack closed 1 year ago

bpollack commented 1 year ago

While {{ set }} allows pipes on the righthand side, and it allows JavaScript on the righthand side, it can't handle JavaScript mixed with pipes. For example, assuming that arr is some form of array, and filt is some filter that expects an array, then this works:

{{ set foo = arr |> filt }}

and this works

{{ set foo = arr.filter(a => a !== 'bar') }}

But this will fail with > unexpected:

{{ set foo = arr.filter(a => a !== 'bar') |> filt }}

The fundamental issue is that |> is getting viewed as a JavaScript expression, not a pipe, and |> is indeed not a valid JavaScript sigil. Gratuitous levels of parentheses that would make even the most grizzled Lisp user proud do not seem to be able to navigate around this.

I honestly kind of get not being able to combine these, but it'd be nice either if the error message were more helpful, or if the documentation on this edge case were clearer

oscarotero commented 1 year ago

What is the version you're using? I just tested it and it works fine:

const data = { arr: ["foo", "bar", "baz"] };
const filt = (arr) => arr.map((a) => a.toUpperCase()).join(" ")
{{ set foo = arr.filter(a => a !== 'bar') |> filt }}

{{ foo }}

It outputs FOO BAZ

bpollack commented 1 year ago

0.5.0; I'll try 0.6.0 when I have a moment tonight

bpollack commented 1 year ago

I can confirm this works correctly on v0.6.0, so I'll close. Thanks for preemptively fixing this before I had a chance to file the bug :)