Share JSONata functions between nodes

Hi,

In my flow, I used some kind of hashing function in a Change node JSONata. This operation happens multiple times, in multiple flows and each time I have to copy/paste my node and change the paths.

I fear the day I'll change the function, finding all the nodes and updating them. Is there a way to "create" a JSONata function (says $hash()) and use it in multiple nodes?

I found a feature request about that but that's for creating module. I imagine something more like the Function library.

Is this already feasible? If not, is it a good idea?

1 Like

You could use a function node to create a simple hash:

String.prototype.hashCode = function() {
    var hash = 0;
    if (this.length === 0) {
        return hash;
    }
    for (var i = 0; i < this.length; i++) {
        var char = this.charCodeAt(i);
        hash = ((hash<<5)-hash)+char;
        hash = hash & hash; // Convert to 32bit integer
    }
    return hash;
}

input = "/home/test".hashCode()

return {payload:input}

One approach would be to put that Change node into a Subflow and then use instances of the subflow wherever you need the function - leaving you one place to edit the function.

The possible downside is it would operate on the same message property with all instances - so would you may need to do something around the subflow instances to move what you wanted to hash to a common property and then move it back afterwards.

As for registering custom JSONata functions in the flow, if someone wanted to propose how that would work in practise, then we could certainly consider it.

2 Likes

Yes, that was my "workaround"

Right now I used an env var (only parameter I could set in a subflow) to give a path in msg and use a recursive function to change it in place. I stumble upon JSONata Transform and that's very close.

If I can help this project, that would be a pleasure! Could you point me to the first steps?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.