Reference function within another function node

Hi

Consider 2 function nodes, A and B.
Is it possible to reference a function written in A from node B?

I've tried searching for "referencing functions" and "scope of functions in function node".

Example:

[
    {
        "id": "95c7830baecc1f89",
        "type": "function",
        "z": "558ac96c.c926d8",
        "name": "A: Spreadsheet numbering functions",
        "func": "//Use these to convert to and from spreadsheet column numbering\n//https://codereview.stackexchange.com/questions/16124/implement-numbering-scheme-like-a-b-c-aa-ab-aaa-similar-to-converting?rq=1\nfunction convertToNumberingScheme(number) {\n  var letters  = \"\";\n  const charCodeOfA = \"A\".charCodeAt(0);\n\n  do {\n    number -= 1;\n    letters = String.fromCharCode(charCodeOfA + (number % 26)) + letters;\n    number = (number / 26) >> 0; // quick `floor`\n  } while(number > 0);\n\n  return letters;\n}\n\n//https://stackoverflow.com/questions/9905533/convert-excel-column-alphabet-e-g-aa-to-number-e-g-25\nfunction lettersToNumber(letters){\n    return letters.split('').reduce((r, a) => r * 26 + parseInt(a, 36) - 9, 0);\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 2490,
        "y": 1060,
        "wires": [
            []
        ]
    },
    {
        "id": "3ad0b91b0c6272be",
        "type": "inject",
        "z": "558ac96c.c926d8",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 2360,
        "y": 1120,
        "wires": [
            [
                "55717a09a50eba9a"
            ]
        ]
    },
    {
        "id": "1b48414330830eff",
        "type": "debug",
        "z": "558ac96c.c926d8",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 2630,
        "y": 1120,
        "wires": []
    },
    {
        "id": "55717a09a50eba9a",
        "type": "function",
        "z": "558ac96c.c926d8",
        "name": "B: Test",
        "func": "const   radix = 26;\nvar     radixTest = [];\n\nfor(var i=1; i <182; i++){\n    radixTest[i] = {};\n    radixTest[i].string = i.toString(radix);\n    radixTest[i].num = parseInt(radixTest[i].string, radix);\n    radixTest[i].convertToNumberingScheme = convertToNumberingScheme(i);\n    radixTest[i].lettersToNumber = lettersToNumber(radixTest[i].convertToNumberingScheme)\n}\n\nreturn {radixTest:radixTest};\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 2500,
        "y": 1120,
        "wires": [
            [
                "1b48414330830eff"
            ]
        ]
    }
]

I feel like I've seen this in some node's example, but I cannot remember which one.

Thanks,
Ashfaak

Yes, but it is not very "node-red". You lose the graphical wiring/traceability aspect. A more "low-code" solution is to use link-call nodes (or sub flows) & put your utility functions in separate flows then re-use those.

However, if you already have a utility function & simply want to share it without copy/paste, then add it to an object and store it in either flow or global context.

then in function B, you can grab it and use it...

//get convertToNumberingScheme  from  flow.util
const { convertToNumberingScheme, lettersToNumber} = flow.get("util");
let x1 = convertToNumberingScheme("x1");
let x = lettersToNumber("x");

working demo flow
[
    {
        "id": "95c7830baecc1f89",
        "type": "function",
        "z": "4c5ad8c7caa80822",
        "name": "A: Spreadsheet numbering functions",
        "func": "//Use these to convert to and from spreadsheet column numbering\n//https://codereview.stackexchange.com/questions/16124/implement-numbering-scheme-like-a-b-c-aa-ab-aaa-similar-to-converting?rq=1\nfunction convertToNumberingScheme(number) {\n  let letters  = \"\";\n  const charCodeOfA = \"A\".charCodeAt(0);\n\n  do {\n    number -= 1;\n    letters = String.fromCharCode(charCodeOfA + (number % 26)) + letters;\n    number = (number / 26) >> 0; // quick `floor`\n  } while(number > 0);\n\n  return letters;\n}\n\n//https://stackoverflow.com/questions/9905533/convert-excel-column-alphabet-e-g-aa-to-number-e-g-25\nfunction lettersToNumber(letters){\n    return letters.split('').reduce((r, a) => r * 26 + parseInt(a, 36) - 9, 0);\n}\n\n\nmsg.payload = {\n  convertToNumberingScheme,\n  lettersToNumber\n}\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1718,
        "y": 192,
        "wires": [
            [
                "32df1e7b64c344c2"
            ]
        ]
    },
    {
        "id": "3ad0b91b0c6272be",
        "type": "inject",
        "z": "4c5ad8c7caa80822",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 1588,
        "y": 252,
        "wires": [
            [
                "55717a09a50eba9a"
            ]
        ]
    },
    {
        "id": "1b48414330830eff",
        "type": "debug",
        "z": "4c5ad8c7caa80822",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1858,
        "y": 252,
        "wires": []
    },
    {
        "id": "55717a09a50eba9a",
        "type": "function",
        "z": "4c5ad8c7caa80822",
        "name": "B: Test",
        "func": "const radix = 26;\nconst radixTest = [];\n\n//get convertToNumberingScheme  from  flow.util\nconst { convertToNumberingScheme, lettersToNumber } = flow.get(\"util\");\n\nfor (let i = 1; i < 182; i++) {\n    radixTest[i] = {};\n    radixTest[i].string = i.toString(radix);\n    radixTest[i].num = parseInt(radixTest[i].string, radix);\n    radixTest[i].convertToNumberingScheme = convertToNumberingScheme(i);\n    radixTest[i].lettersToNumber = lettersToNumber(radixTest[i].convertToNumberingScheme)\n}\n\nreturn { radixTest: radixTest };\n\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1728,
        "y": 252,
        "wires": [
            [
                "1b48414330830eff"
            ]
        ]
    },
    {
        "id": "05745c60136cfcab",
        "type": "inject",
        "z": "4c5ad8c7caa80822",
        "name": "init",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 1478,
        "y": 192,
        "wires": [
            [
                "95c7830baecc1f89"
            ]
        ]
    },
    {
        "id": "32df1e7b64c344c2",
        "type": "change",
        "z": "4c5ad8c7caa80822",
        "name": "store functions in flow.util",
        "rules": [
            {
                "t": "set",
                "p": "util",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 2018,
        "y": 192,
        "wires": [
            []
        ]
    }
]
1 Like

Thank you Steve-Mcl!

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