Separate letters and add a hyphen

Hello community, I am trying to separate the word that is entered in the "Código" field, that is, the user will enter, for example, AWS001, and I want that when the debuj node arrives, it is separated by a script as shown below "AWS-001"
image

[
    {
        "id": "582fe26bc3e697bb",
        "type": "ui_form",
        "z": "e9c7705cf137b29e",
        "name": "",
        "label": "",
        "group": "1241bafcb7aa8ef3",
        "order": 0,
        "width": "6",
        "height": "21",
        "options": [
            {
                "label": "Canal Izquierdo",
                "value": "pesoIzq",
                "type": "number",
                "required": true,
                "rows": null
            },
            {
                "label": "Canal Derecho",
                "value": "pesoDer",
                "type": "number",
                "required": true,
                "rows": null
            },
            {
                "label": "Peso Total",
                "value": "peso",
                "type": "number",
                "required": true,
                "rows": null
            },
            {
                "label": "Tipo",
                "value": "tipo",
                "type": "text",
                "required": true,
                "rows": null
            },
            {
                "label": "Código",
                "value": "codigo",
                "type": "text",
                "required": true,
                "rows": null
            }
        ],
        "formValue": {
            "pesoIzq": "",
            "pesoDer": "",
            "peso": "",
            "tipo": "",
            "codigo": ""
        },
        "payload": "",
        "submit": "Enviar",
        "cancel": "Cancelar",
        "topic": "topic",
        "topicType": "msg",
        "splitLayout": false,
        "x": 390,
        "y": 200,
        "wires": [
            [
                "bfe3e43b.85fa88"
            ]
        ]
    },
    {
        "id": "1241bafcb7aa8ef3",
        "type": "ui_group",
        "name": "Datos",
        "tab": "3eb7ca393c31b1fd",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "3eb7ca393c31b1fd",
        "type": "ui_tab",
        "name": "Datos",
        "icon": "dashboard",
        "disabled": false,
        "hidden": false
    }
]

You could add a function node after the form node as follows -

let txt = msg.payload.codigo
txt = txt.slice(0, 3) + "-" + txt.slice(3);
msg.payload.codigo=txt
return msg;
1 Like

Hi @Jhontan14, Here is another option with split and join nodes, but the @smcgann99 option is more optimal.

[{"id":"e92e6ddeed745dfc","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"582fe26bc3e697bb","type":"ui_form","z":"e92e6ddeed745dfc","name":"","label":"","group":"1241bafcb7aa8ef3","order":0,"width":"6","height":"21","options":[{"label":"Canal Izquierdo","value":"pesoIzq","type":"number","required":true,"rows":null},{"label":"Canal Derecho","value":"pesoDer","type":"number","required":true,"rows":null},{"label":"Peso Total","value":"peso","type":"number","required":true,"rows":null},{"label":"Tipo","value":"tipo","type":"text","required":true,"rows":null},{"label":"Código","value":"codigo","type":"text","required":true,"rows":null}],"formValue":{"pesoIzq":"","pesoDer":"","peso":"","tipo":"","codigo":""},"payload":"","submit":"Enviar","cancel":"Cancelar","topic":"topic","topicType":"msg","splitLayout":false,"className":"","x":230,"y":280,"wires":[["e2297423265418f6","35cb7a28b7c634de"]]},{"id":"2816a4477ad1a036","type":"debug","z":"e92e6ddeed745dfc","name":"Codigo","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":940,"y":280,"wires":[]},{"id":"e2297423265418f6","type":"change","z":"e92e6ddeed745dfc","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.codigo","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":280,"wires":[["d8632ea688173c00"]]},{"id":"d8632ea688173c00","type":"split","z":"e92e6ddeed745dfc","name":"","splt":"1","spltType":"len","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":550,"y":280,"wires":[["10fe6a7e7c4cb669"]]},{"id":"10fe6a7e7c4cb669","type":"function","z":"e92e6ddeed745dfc","name":"function 1","func":"var counter = context.get(\"COUNTER\") || 0;\ncounter = (counter + 1) % 5;\ncontext.set(\"COUNTER\", counter);\n\nif(counter == 3){\n    msg.payload = msg.payload + \"-\";\n}\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":280,"wires":[["153901709f7992a1"]]},{"id":"153901709f7992a1","type":"join","z":"e92e6ddeed745dfc","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":810,"y":280,"wires":[["2816a4477ad1a036"]]},{"id":"35cb7a28b7c634de","type":"debug","z":"e92e6ddeed745dfc","name":"msg completo","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":400,"y":220,"wires":[]},{"id":"1241bafcb7aa8ef3","type":"ui_group","name":"Datos","tab":"3eb7ca393c31b1fd","order":1,"disp":true,"width":"6","collapse":false},{"id":"3eb7ca393c31b1fd","type":"ui_tab","name":"Datos","icon":"dashboard","disabled":false,"hidden":false}]
1 Like

Or use a change node with jsonata expression

$substring(payload.codigo, 0,3) &'-'& $substring(payload.codigo, 3)

My best effort for the benefit of @E1cid :upside_down_face:

2 Likes

Or you can use the change function of the change node.

4 Likes

Now you are just showing off :wink:

3 Likes

if the first section has a number it won't work. :face_with_hand_over_mouth:

Sure. what if the person adds a hyphen already to.
Once all formats are shown i could offer other suggestions.
like (-?)(\d{3})$ and replace with -$2

Close, but my coding book is definetly not that big.

1 Like

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