From 42d0aa5d734120ee78872d9a08276ce26adcdb40 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Sat, 29 Oct 2022 17:34:29 +0100 Subject: [PATCH] Add drop mode to range node and include tests --- .../nodes/core/function/16-range.html | 1 + .../@node-red/nodes/core/function/16-range.js | 8 +++++-- .../locales/en-US/function/16-range.html | 3 +++ .../nodes/locales/en-US/messages.json | 3 ++- test/nodes/core/function/16-range_spec.js | 21 +++++++++++++++++++ 5 files changed, 33 insertions(+), 3 deletions(-) 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 07bb1f080..1652a91db 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 @@ -10,6 +10,7 @@ +
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 a5dede4ea..61ffd53fb 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 @@ -32,11 +32,15 @@ module.exports = function(RED) { if (value !== undefined) { var n = Number(value); if (!isNaN(n)) { - if (node.action == "clamp") { + if (node.action === "drop") { + if (n < node.minin) { done(); return; } + if (n > node.maxin) { done(); return; } + } + if (node.action === "clamp") { if (n < node.minin) { n = node.minin; } if (n > node.maxin) { n = node.maxin; } } - if (node.action == "roll") { + if (node.action === "roll") { var divisor = node.maxin - node.minin; n = ((n - node.minin) % divisor + divisor) % divisor + node.minin; } diff --git a/packages/node_modules/@node-red/nodes/locales/en-US/function/16-range.html b/packages/node_modules/@node-red/nodes/locales/en-US/function/16-range.html index b391f5c04..f25363565 100644 --- a/packages/node_modules/@node-red/nodes/locales/en-US/function/16-range.html +++ b/packages/node_modules/@node-red/nodes/locales/en-US/function/16-range.html @@ -34,11 +34,14 @@ the range specified within the target range.

Scale and wrap within the target range means that the result will be wrapped within the target range.

+

Scale, but drop if outside input range means that the result will + be scaled, but any inputs outside of the inout range will be dropped.

For example an input 0 - 10 mapped to 0 - 100.

+
modeinputoutput
scale
12
120
limit
12
100
wrap
12
20
drop
12
(no output)
diff --git a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json index 62d5f351f..8b66bf5e9 100644 --- a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json @@ -813,7 +813,8 @@ "scale": { "payload": "Scale the message property", "limit": "Scale and limit to the target range", - "wrap": "Scale and wrap within the target range" + "wrap": "Scale and wrap within the target range", + "drop": "Scale, but drop msg if outside input range" }, "tip": "Tip: This node ONLY works with numbers.", "errors": { diff --git a/test/nodes/core/function/16-range_spec.js b/test/nodes/core/function/16-range_spec.js index a0dcd0078..620d21b12 100644 --- a/test/nodes/core/function/16-range_spec.js +++ b/test/nodes/core/function/16-range_spec.js @@ -106,6 +106,27 @@ describe('range Node', function() { genericRangeTest("clamp", 0, 10, 0, 1000, false, -1, 0, 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:[]}]; + helper.load(rangeNode, flow, function() { + var rangeNode1 = helper.getNode("rangeNode1"); + var helperNode1 = helper.getNode("helperNode1"); + helperNode1.on("input", function(msg) { + try { + msg.should.have.property('payload'); + msg.payload.should.equal(50); + done(); + } catch(err) { + done(err); + } + }); + rangeNode1.receive({payload:1}); + rangeNode1.receive({payload:9}); + rangeNode1.receive({payload:5}); + }); + }); + it('just passes on msg if payload not present', function(done) { var flow = [{"id":"rangeNode1","type":"range","minin":0,"maxin":100,"minout":0,"maxout":100,"action":"scale","round":true,"name":"rangeNode","wires":[["helperNode1"]]}, {id:"helperNode1", type:"helper", wires:[]}];