One of the reasons I haven't personally pursued it is because of the complexity of keeping it simple.
Would we allow any valid JavaScript? Could it be 100s of semi-colon statements long all in a single line of text? Would it expect to have access to all the standard APIs (node.send()... flow.get()... etc etc) and allow the side-effects they can have?
What semantics would be allowed?
All of these things can be answered, but they need someone to come up the answers and propose something fairly concrete.
Well I'd start it off simple - single line with ability to access msg and context properties.
I'm just seeing it as a JSONata replacement - to do the little things that don't warrant a separate function node before or after a switch or change node
This can already be done in several ways though both through external services and through an external code module linked into globals. This approach would really add complexity and potentially create tightly coupled systems which isn't a good design pattern.
I agree. Search for and replace anything from a semi-colon onwards in the code and remove it before saving, also limited to a sensible number of characters - 255 should be more than enough. Execute in a node.js VM for safety. Some overhead to that but a lot less than JSONata. Allow setting into a single "thing" as with other options. Attach to a return statement so users don't need to remember that and cannot not use it.
Some additional complexity could be allowed in future releases as utility was proven and requests clarified and it might even get to a point of allowing similar complexity to JSONata eventually complete with an editor but that would be considered over time. Start with a simple MVP.
Wouldn't that mean to invest energy & time into a "limited" something that'll soon initiate an "I would like to see this & that feature as well" discussion?
I'd like to propose to
re-use the existing sandbox of a function node (almost) as it is
remove just the msg passing ability (send() & done()) ... and perhaps some (very!) few functions more, based on more in-depth considerations
take the return(ed) value for further processing in the change rules.
keep the timeout in case someone intends to do heavy async jobs in those functions.
re-use as well the editor element of the function node
add a flag in settings.js to enable/disable this feature.
We'll end up with a great & powerful processing tool inside the change node.
Cons? To be discussed...
Which is not what I'd like
I just want to be able to do the sort of little JSONata things that I currently do but without having to ask @E1cid how to do it first
If I want complex - then even I'm OK with using a function node
i.e in the Blockly node (which is what I use when I want to write functions) there is a simple JS block
to let me do something that there isn't a pre-defined Blockly block can do
and that's all I'm really wanting to use in a change/switch node
Yes, but often best to get something minimalist and solid through the door than having to wait months/years for someone to have the time and energy to implement something complex. Also gives time to get things right. Deliver early, fail fast is a common mantra for complex designs.
Exactly!
That's why I proposed to re-use something that's already existing and proven for years, rather than investing in a new feature, especially of limited capability.
Can you explain more about external services and external code module linked into globals?
Here is an example, not javascript but Bash, which I can run via an exec node but I would like to run from a change node or elsewhere.
To meet the requirement for a simple oneliner I have crammed it all into one line
printf "\\n{frequency: %d, temperature: %d}\\n" $(vcgencmd measure_clock arm | sed -E 's/(.*=)(.*)/\2/') $(vcgencmd measure_temp | sed -E 's/(.*=)([0-9]*)(.*)/\2/')
Note that I'm not suggesting Node-red understands or verifies this, just hands it off to a helper app - Bash.
Nope, it would really add the ability to stitch together Node-red, legacy code and OS utilities while maintaining source code within Node-red.
Yes that's probably true. Mind you, I am not sure that complex Jsonata should be allowed in a good system design either.