Add support for numeric literals - binary (prefix 0b), octal (prefix 0o) and hexadecimal (prefix: 0x)

Hi all,

See Numeric Literals spec

var bin = 0b1111;
var oct = 0o17;
var hex = 0xF;

where is that not supported ?

I have a RPi3/Raspbian & run a docker image with Node-RED 2.1.3, and set up some interactions with Zelio Logic through Modbus TCP

When I create an Inject node, adding msg.payload = any ordinary input like (numeric)1 does not fire a red error frame around an input field but (numeric)0b0000000000000001 does (and also a warning on Deploy - "The workspace contains some nodes that are not properly configured")

Ah right the inject node - ok. Makes sense. Not sure how easy it would be to do generically for all numeric input type fields - would need some thought.
(works fine in a function node of course)

This also works despite warnings in the inject node too but not in every node with numeric input I have tried

For ex., when I added a Change node with a Rule
Set msg.payload to the value (expression) payload.arr[1] & 32768 - it works fine, but with payload.arr[1] & 0b1000000000000000 it does not work after deployment with an error "Invalid JSONata expression: Syntax error: "b1000000000000000""

We could add support for non-decimal base representations in our standard TypedInput widget for the 'number' type - as used by the Inject node and in other places.

There would be a risk that a contrib node uses that widget without using the underlying utility we provide for interpreting the value because they assume it is a number that can be parsed with the standard JavaScript functions parseFloat or parseInt. A quick test shows those functions don't handle binary/octal/hex representations (with one exception - parseInt("0x10") does work, but that's it.

As for the JSONata mode - we can only support what JSONata itself supports. Given JSON does not support hex or octal number representations, then it is unlike that JSONata would add support for anything else.

1 Like

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