From 7b51f30d11e182103a6b4909c7d8d0a9b89c075b Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Thu, 28 Aug 2025 15:18:02 +0100 Subject: [PATCH 1/2] Fix rounding errors for range node when using float inputs and integer outputs And add extra tests --- .../@node-red/nodes/core/function/16-range.js | 4 ++++ test/nodes/core/function/16-range_spec.js | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/packages/node_modules/@node-red/nodes/core/function/16-range.js b/packages/node_modules/@node-red/nodes/core/function/16-range.js index 61ffd53fb..3071cd99a 100644 --- a/packages/node_modules/@node-red/nodes/core/function/16-range.js +++ b/packages/node_modules/@node-red/nodes/core/function/16-range.js @@ -24,6 +24,10 @@ module.exports = function(RED) { this.maxin = Number(n.maxin); this.minout = Number(n.minout); this.maxout = Number(n.maxout); + if (this.round) { + this.maxout = Math.floor(this.maxout); + this.minout = Math.ceil(this.minout); + } this.property = n.property||"payload"; var node = this; diff --git a/test/nodes/core/function/16-range_spec.js b/test/nodes/core/function/16-range_spec.js index 620d21b12..5bc8cbc32 100644 --- a/test/nodes/core/function/16-range_spec.js +++ b/test/nodes/core/function/16-range_spec.js @@ -106,6 +106,14 @@ describe('range Node', function() { genericRangeTest("clamp", 0, 10, 0, 1000, false, -1, 0, done); }); + it('clamps numbers within a range - above max fp', function(done) { + genericRangeTest("clamp", 0, 9.5, 0.5, 100.5, true, 13.1, 100, done); + }); + + it('clamps numbers within a range - below min fp', function(done) { + genericRangeTest("clamp", 0, 9.5, 0.3, 100.5, true, -13.1, 1, done); + }); + it('drops msg if in drop mode and input outside range', function(done) { var flow = [{"id":"rangeNode1","type":"range","minin":2,"maxin":8,"minout":20,"maxout":80,"action":"drop","round":true,"name":"rangeNode","wires":[["helperNode1"]]}, {id:"helperNode1", type:"helper", wires:[]}]; From 4ae8fcbc45a13fd9198e692ac7a3ab84c20f991f Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Fri, 29 Aug 2025 15:08:23 +0100 Subject: [PATCH 2/2] Fix range node to handle input min > max and add tests and add pin hover to show confiog --- .../@node-red/nodes/core/function/16-range.html | 8 ++++++++ .../@node-red/nodes/core/function/16-range.js | 8 ++++++++ test/nodes/core/function/16-range_spec.js | 8 ++++++++ 3 files changed, 24 insertions(+) diff --git a/packages/node_modules/@node-red/nodes/core/function/16-range.html b/packages/node_modules/@node-red/nodes/core/function/16-range.html index 21019703d..a9c508a02 100644 --- a/packages/node_modules/@node-red/nodes/core/function/16-range.html +++ b/packages/node_modules/@node-red/nodes/core/function/16-range.html @@ -64,6 +64,14 @@ }, inputs: 1, outputs: 1, + inputLabels: function() { return this.minin + " - " + this.maxin }, + outputLabels: function(i) { + var outie = this.minout + " - " + this.maxout + if (this.action === "clamp") { outie = "[" + outie + "]" } + if (this.action === "drop") { outie = "x" + outie + "x" } + if (this.action === "roll") { outie = ">" + outie + ">" } + return outie; + }, icon: "range.svg", label: function() { if (this.minout !== "" && this.maxout !== "") { return this.name||this.minout + " - " + this.maxout; } diff --git a/packages/node_modules/@node-red/nodes/core/function/16-range.js b/packages/node_modules/@node-red/nodes/core/function/16-range.js index 3071cd99a..10affbdc7 100644 --- a/packages/node_modules/@node-red/nodes/core/function/16-range.js +++ b/packages/node_modules/@node-red/nodes/core/function/16-range.js @@ -24,6 +24,14 @@ module.exports = function(RED) { this.maxin = Number(n.maxin); this.minout = Number(n.minout); this.maxout = Number(n.maxout); + if (this.minin > this.maxin) { + let tmp = this.minin; + this.minin = this.maxin; + this.maxin = tmp; + tmp = this.minout; + this.minout = this.maxout; + this.maxout = tmp; + } if (this.round) { this.maxout = Math.floor(this.maxout); this.minout = Math.ceil(this.minout); diff --git a/test/nodes/core/function/16-range_spec.js b/test/nodes/core/function/16-range_spec.js index 5bc8cbc32..cef16da98 100644 --- a/test/nodes/core/function/16-range_spec.js +++ b/test/nodes/core/function/16-range_spec.js @@ -114,6 +114,14 @@ describe('range Node', function() { genericRangeTest("clamp", 0, 9.5, 0.3, 100.5, true, -13.1, 1, done); }); + it('handles input range where max < min', function(done) { + genericRangeTest("scale", 100, 0, 0, -100, false, 40, -60, done); + }); + + it('handles input range where max < min', function(done) { + genericRangeTest("scale", 100, 0, -100, 0, false, 10, -10, done); + }); + it('drops msg if in drop mode and input outside range', function(done) { var flow = [{"id":"rangeNode1","type":"range","minin":2,"maxin":8,"minout":20,"maxout":80,"action":"drop","round":true,"name":"rangeNode","wires":[["helperNode1"]]}, {id:"helperNode1", type:"helper", wires:[]}];