squirrelly icon indicating copy to clipboard operation
squirrelly copied to clipboard

Braces not working with pipes

Open perotom opened this issue 2 years ago • 7 comments

Describe the bug I would like to use braces in my expressions to do some calculations. When using pipes this creates some issues.

To Reproduce Try to use braces mixed with pipes like:

{{((it.num_array | min) - 5) | format}}

One can reproduce it in the playground for the display array examples like:

{{(it.fruits | join(" + ")) + "test"}}

https://squirrelly.js.org/playground/

Expected behavior Braces should work as "usual" which normally are used to change evaluation order.

Package & Environment Details

  • Environment: Chrome Version 114.0.5735.198 (Official Build) (x86_64)
  • Version: 9.0.0

perotom avatar Jul 05 '23 09:07 perotom

It's unfortunately not possible to combine filters with operations in that way. After a filter |, the only valid syntax is name (args) and then optionally more filters.

bgub avatar Jul 05 '23 17:07 bgub

What a pity. Can you think of any other workaround? Or is it possible to use math functions like min max and then do some arithmetic? Thank you

perotom avatar Jul 05 '23 18:07 perotom

@perotom sure. The trick is to use multiple filters chained together, eg.

{{it.num_array | min | subtract(5) | format}} or {{it.fruits | join(" + ") | append("test")}}

bgub avatar Jul 05 '23 19:07 bgub

@nebrelbug Sounds like a nice idea but let's just flip the subtraction in your example: {{5 | subtract(it.num_array | min) | format}} Sadly this simple example doesn't work. Overall it is an excellent library but this "simple" flaw really is a deal breaker. Any ideas for workarounds?

perotom avatar Oct 03 '23 06:10 perotom

@perotom yeah, Squirrelly unfortunately only supports top-level filters. Your best bet would be to use JS expressions inside of your filter. As an example:

{{ 5 | subtract(it.num_array.min()) | format }}

bgub avatar Oct 03 '23 17:10 bgub

@nebrelbug Thanks for the idea, sadly we have a lot of subtractions with different variables so this approach won't work out in the long run. Do you think it is easy to add support for braces? Or does it break everything? Would be glad to add the support.

perotom avatar Oct 04 '23 05:10 perotom

It's unfortunately probably not feasible for this project :(.

bgub avatar Oct 04 '23 15:10 bgub