# Share JSONata functions between nodes

**URL:** https://discourse.nodered.org/t/share-jsonata-functions-between-nodes/25198
**Category:** General
**Created:** [20 April 2020 15:55 UTC](https://discourse.nodered.org/t/share-jsonata-functions-between-nodes/25198 "2020-04-20T15:55:48Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![JbIPS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jbips/32/9246_2.png) [@JbIPS](https://discourse.nodered.org/u/JbIPS)
#### Post date: [20 April 2020 15:55 UTC](https://discourse.nodered.org/t/share-jsonata-functions-between-nodes/25198/1 "2020-04-20T15:55:48Z")

</div>

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](https://discourse.nodered.org/t/feature-request-register-jsonata-function/7381) 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?

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [20 April 2020 16:01 UTC](https://discourse.nodered.org/t/share-jsonata-functions-between-nodes/25198/2 "2020-04-20T16:01:55Z")

</div>

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

```auto
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}

```

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [20 April 2020 16:02 UTC](https://discourse.nodered.org/t/share-jsonata-functions-between-nodes/25198/3 "2020-04-20T16:02:09Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![JbIPS](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jbips/32/9246_2.png) [@JbIPS](https://discourse.nodered.org/u/JbIPS)
#### Post date: [20 April 2020 18:30 UTC](https://discourse.nodered.org/t/share-jsonata-functions-between-nodes/25198/4 "2020-04-20T18:30:25Z")

</div>

> [@knolleary](#):
>
> 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.

Yes, that was my "workaround"

> [@knolleary](#):
>
> 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.

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](https://docs.jsonata.org/other-operators#-------transform) and that's very close.

> [@knolleary](#):
>
> 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.

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

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [19 June 2020 18:30 UTC](https://discourse.nodered.org/t/share-jsonata-functions-between-nodes/25198/5 "2020-06-19T18:30:28Z")

</div>

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