How to check *types* with `if` in `function` node

I get how the if() works. (well: I thought I did)

if (x == 0)
{
    // do something here;
}
//  continue as normal.

But say I want to check x is..... a number? (and is between 0 and 8)
(ok, that's just another if nested in there)

How do I do that?
(Check it is a number?)
I know in the switch node there are options of type.....
But I don't know the finer details of how that works.
So I'm asking here, now.

This my best effort for now.

[{"id":"5204cbf368ea0df2","type":"function","z":"aaa025a7d2bb5b14","name":"message format check","func":"//  Split the payload.\nlet check = msg.payload.split(\",\");\n//node.warn(check);\n\nlet error = 0;\nlet a = \"\";\n\n//  Check RGB part of message.\nif (check[0] != \"rgb\")\n{\n    //  RGB wrong\n    //node.warn(\"RGB part wrong\");\n    error = 1;\n}\n\nif (error == 0)\n{\n    a = parseInt(check[1]);\n    //node.warn(\"A \" + a);\n    if (isNaN(a))\n    {\n        //  Part 1 problem.\n        //node.warn(\"Part 1 problem.\");\n        error = 1;\n    }\n}\n\nif (error == 0)\n{\n    a = parseInt(check[2]);\n    //node.warn(\"A \" + a);\n    if (isNaN(a)) {\n        //  Part 2 problem.\n        //node.warn(\"Part 2 problem.\");\n        error = 1;\n    }\n}\n\nif (error == 0)\n{\n    a = parseInt(check[3]);\n    //node.warn(\"A \" + a);\n    if (isNaN(a)) {\n        //  Part 3 problem.\n        //node.warn(\"Part 3 problem.\");\n        error = 1;\n    }\n}\n\nif (error == 0) \n{\n    a = parseInt(check[4]);\n    //node.warn(\"A \" + a);\n    if (isNaN(a)) {\n        //  Part 4 problem.\n        //node.warn(\"Part 4 problem.\");\n        error = 1;\n    }\n}\n\nif (error == 0)\n{\n    //  All good\n    return msg;\n}\nelse\n{\n    node.warn(\"We have a problem\");\n    return [null,msg];\n}\n","outputs":2,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":150,"wires":[[],[]],"outputLabels":["Good message","Bad message"]}]
if ("number" === typeof x && x >= 0 && x <= 8)
{
    // do something here;
}
//  continue as normal.

typeof is your friend here...

1 Like

Sorry.

I was quick to mark it as the solution.

It IS the solution but I am not translating what you said to what I want.

Your "number".....
and x and y.

I'll keep looking and see if I can work it out.

"number" === typeof x: typeof x checks the type of x and returns a string. If it's a number (as you tried to test) it returns "number".

x>=0 && x<=8 checks if 0<= x <= 8 ... , to verify that x

is between 0 and 8

You don't need another if but can just concatenate the various criterias with &&. JS will bail out when the first fails ( === false).

Again: Thanks.

But somehow I am not seeing it.

I should have posted this bit and we can work from there.

if (error == 0)
{
    a = parseInt(check[1]);
    //node.warn("A " + a);
    if (isNaN(a))
    {
        //  Part 1 problem.
        //node.warn("Part 1 problem.");
        error = 1;
    }
}

Yes, there is a bit/lot missing compared to what yours does.

That is what I have - as a start point.

So I am not seeing how this fits into what I have.
if ("number" === typeof x && x >= 0 && x <= 8)

Yes, but I am not understanding it all as much as I would like.

I got stuck on/with oftype rather than typeof.
After I found that, I looked but it didn't help me as much as I would like.

I got that code I posted but ......

Anyway.

Pasting the important part of the code:

//  Split the payload.
let check = msg.payload.split(",");
//node.warn(check);

let error = 0;
let a = "";

if (error == 0)
{
    a = parseInt(check[1]);
    node.warn("A " + a);
    if ("number" === typeof a)
    {
        //  Part 1 problem.
        node.warn("Part 1 problem.");
        error = 1;
    }
}

Given I am sending it rgb,0,0,0,0
Why do I get an error?

You don't get an error; you get your expected result.

Remember your first question?

That's what this line does; now it's a but x ... but that's the only difference:

So you successfully tested for a number; in the next lines you yet emit a .warn that says you have a Part 1 problem.

Guessing now: If you intend to assure, that a is NOT a number, do the test like

if ("number" !== typeof a)

1 Like

My head is spinning.

Not from what you showed me. It just likes to spin.

There are 4 parts to parse/scan.
0 has to be rgb, - which is easy to test.
1 - has to be a NUMBER from 0 to 8. The code has to catch if NOT meeting that.
(Sorry, my bad. (maybe)..... But I had posted my effort and that is how the main function node works.)
2 - as 1, but form 0 to 255
3 and 4 like 2.

After your let a = parseInt(check[1]); a will either be a valid number or else NaN
But there is no value in testing if (typeof a === "number") because typeof NaN is still "number"
What you can do is test if a is between 0 and 256:

//  Split the payload.
let check = msg.payload.split(",");
//node.warn(check);

let a = parseInt(check[1]);
let type = typeof a
node.warn(`typeof ${a} is ${type}`);
if (a >= 0 && a<256) {
    node.warn (`${a} is a valid number`)
}
else {
    node.warn (`${a} is not a valid number`)
}

return msg
1 Like

Perhaps like this...

//  Split the payload.
const check = msg.payload.split(",");
let error = false;

for (let i=0, l=check.length; i<l; i++) {
    const c = check[i];
    switch (i) {
        case 0: {
            if (c !== "rgb") {
                error = true;
                break;
            }
        }
        case 1: {
            if ("number" !== typeof c && c < 0 && c > 8) {   // you're sure it's '8' , not '7' ? 
                error = true;
                break;
            }
        }
        default: {
            if ("number" !== typeof c && c < 0 && c > 255) {
                error = true;
                break;
            }
        }
    }
}

if (error) {
    node.warn("There's a problem...");
}
1 Like

SORRY... (I seem to say that a lot here)

I have (for now) left out the range testing.

I just want to get a part working then make it more complicated. :wink:

Thanks for that.

The 8 is because 0 to 7 are the individual LEDs.
8 is ALL of them at once. :wink:

{{Thanks}}

I also (stubbornly) used 1 and 0 for the error only because I am a dinosaur and didn't think to use that style.

I'll keep looking at what you posted.

The point I am trying to make is that let a = parseInt(check[1]) forces typeof a to be "number" even if it is not a valid number (NaN)
At least that's how I understand it.

Did you try the code I posted?

Not just now.

As you can see I am a bit busy.

I am about to look at it very soon.

if ("number" === typeof x && x >= 0 && x <= 8)

Although that works, it can be confusing for people to grasp in concept if it is reversed like that.

if(typeof x === "number"){ ... }

Um,

I copy/paste that code as given to my function node and it throws up errors.

l is not defined.

and down the bottom, it seems that the the { } aren't closed correctly.

Alas I am not used the the { being appended to the previous line.
I'll load it into another editor that shows me formatting and see what it says.

Sorry ... there was a typo in this line... and some } missing.
That happens when I write code directly into the editor here...

1 Like

There still seems to be an open { } as the bottom line still ends with an indented } and no final closure to the last }