1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add drop mode to range node

and include tests
This commit is contained in:
Dave Conway-Jones 2022-10-29 17:34:29 +01:00
parent 7da3773f7f
commit 4cc18c25fe
No known key found for this signature in database
GPG Key ID: 88BA2B8A411BE9FF
5 changed files with 33 additions and 3 deletions

View File

@ -10,6 +10,7 @@
<option value="scale" data-i18n="range.scale.payload"></option> <option value="scale" data-i18n="range.scale.payload"></option>
<option value="clamp" data-i18n="range.scale.limit"></option> <option value="clamp" data-i18n="range.scale.limit"></option>
<option value="roll" data-i18n="range.scale.wrap"></option> <option value="roll" data-i18n="range.scale.wrap"></option>
<option value="drop" data-i18n="range.scale.drop"></option>
</select> </select>
</div> </div>
<br/> <br/>

View File

@ -32,11 +32,15 @@ module.exports = function(RED) {
if (value !== undefined) { if (value !== undefined) {
var n = Number(value); var n = Number(value);
if (!isNaN(n)) { 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.minin) { n = node.minin; }
if (n > node.maxin) { n = node.maxin; } if (n > node.maxin) { n = node.maxin; }
} }
if (node.action == "roll") { if (node.action === "roll") {
var divisor = node.maxin - node.minin; var divisor = node.maxin - node.minin;
n = ((n - node.minin) % divisor + divisor) % divisor + node.minin; n = ((n - node.minin) % divisor + divisor) % divisor + node.minin;
} }

View File

@ -34,11 +34,14 @@
the range specified within the target range.</p> the range specified within the target range.</p>
<p><i>Scale and wrap within the target range</i> means that the result will <p><i>Scale and wrap within the target range</i> means that the result will
be wrapped within the target range.</p> be wrapped within the target range.</p>
<p><i>Scale, but drop if outside input range</i> means that the result will
be scaled, but any inputs outside of the inout range will be dropped.</p>
<p>For example an input 0 - 10 mapped to 0 - 100.</p> <p>For example an input 0 - 10 mapped to 0 - 100.</p>
<table style="outline-width:#888 solid thin"> <table style="outline-width:#888 solid thin">
<tr><th width="80px">mode</th><th width="80px">input</th><th width="80px">output</th></tr> <tr><th width="80px">mode</th><th width="80px">input</th><th width="80px">output</th></tr>
<tr><td><center>scale</center></td><td><center>12</center></td><td><center>120</center></td></tr> <tr><td><center>scale</center></td><td><center>12</center></td><td><center>120</center></td></tr>
<tr><td><center>limit</center></td><td><center>12</center></td><td><center>100</center></td></tr> <tr><td><center>limit</center></td><td><center>12</center></td><td><center>100</center></td></tr>
<tr><td><center>wrap</center></td><td><center>12</center></td><td><center>20</center></td></tr> <tr><td><center>wrap</center></td><td><center>12</center></td><td><center>20</center></td></tr>
<tr><td><center>drop</center></td><td><center>12</center></td><td><center><i>(no output)</i></center></td></tr>
</table> </table>
</script> </script>

View File

@ -813,7 +813,8 @@
"scale": { "scale": {
"payload": "Scale the message property", "payload": "Scale the message property",
"limit": "Scale and limit to the target range", "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.", "tip": "Tip: This node ONLY works with numbers.",
"errors": { "errors": {

View File

@ -106,6 +106,27 @@ describe('range Node', function() {
genericRangeTest("clamp", 0, 10, 0, 1000, false, -1, 0, done); 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) { 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"]]}, 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:[]}]; {id:"helperNode1", type:"helper", wires:[]}];