Update node-red-random to add option to dynamically pass in 'To' and 'From' (#707)

* Add option to dynamically pass in 'From' and 'To' - fixed bug when 'From' was greater than 'To' by flipping the two

* Set initial values of 'From' and 'To to "" to allow dynamic overriding them
This commit is contained in:
juggledad
2020-12-13 07:58:13 -05:00
committed by GitHub
parent 0ac07a1c6f
commit 9eb031e912
5 changed files with 303 additions and 59 deletions

View File

@@ -19,4 +19,6 @@ If set to return an integer it can include both the low and high values.
If set to return a floating point value it will be from the low value, up to, but
**not** including the high value. `min <= n < max` - so selecting 1 to 6 will return values 1 <= n < 6 .
You can dynamically pass in the 'From' and 'To' values to the node using msg.to and/or msg.from. **NOTE:** hard coded values in the node **always take precedence**.
**Note:** This returns numbers - objects of type **number**.

View File

@@ -1,7 +1,14 @@
<script type="text/html" data-help-name="random">
<p>Generates a random number between a low and high value.</p>
<p>Generates a random number between a low and high value. Defaults to 1 to 10.</p>
<p>If set to return an integer it can <i>include</i> both the low and high values.
<code>min <= n <= max</code></p>
<p>If set to return a floating point value it will be from the low value, up to, but
not including the high value. <code>min <= n < max</code></p>
<p><b>Inputs</b></p>
<p>You can dynamically pass in the 'From' and 'To' values to the node. <b>NOTE:</b> hard coded values in the node <b>always take precedence.</b>
<ul>
<i><b>From</b> [msg.from] Number containing the low value to be used.</i>
<br>
<i><b>To</b> [msg.to] Number containing the high value to be used.</i>
</ul>
</script>

View File

@@ -31,8 +31,8 @@
color:"#E2D96E",
defaults: {
name: {value:""},
low: {value:"1"},
high: {value:"10"},
low: {value: 1,validate:function(v) { return !isNaN(v) || v.length === 0;} },
high: {value: 10,validate:function(v) { return !isNaN(v) || v.length === 0;} },
inte: {value:"true"},
property: {value:"payload",required:true}
},

View File

@@ -3,21 +3,73 @@ module.exports = function(RED) {
"use strict";
function RandomNode(n) {
RED.nodes.createNode(this,n);
this.low = Number(n.low || 1);
this.high = Number(n.high || 10);
this.low = n.low
this.high = n.high
this.inte = n.inte || false;
this.property = n.property||"payload";
var node = this;
var tmp = {};
this.on("input", function(msg) {
var value;
if (node.inte == "true" || node.inte === true) {
value = Math.round(Math.random() * (node.high - node.low + 1) + node.low - 0.5);
}
else {
value = Math.random() * (node.high - node.low) + node.low;
}
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
tmp.low = 1 // set this as the default low value
tmp.low_e = ""
if (node.low) { // if the the node has a value use it
tmp.low = node.low
} else if ('from' in msg) { // else see if a 'from' is in the msg
if (Number(msg.from)) { // if it is, and is a number, use it
tmp.low = Number(msg.from);
} else { // otherwise setup NaN error
tmp.low = NaN;
tmp.low_e = " From: " + msg.from; // setup to show bad incoming msg.from
}
}
tmp.high = 10 // set this as the default high value
tmp.high_e = "";
if (node.high) { // if the the node has a value use it
tmp.high = node.high
} else if ('to' in msg) { // else see if a 'to' is in the msg
if (Number(msg.to)) { // if it is, and is a number, use it
tmp.high = Number(msg.to);
} else { // otherwise setup NaN error
tmp.high = NaN
tmp.high_e = " To: " + msg.to // setup to show bad incoming msg.to
}
}
// if tmp.low or high are not numbers, send an error msg with bad values
if ( (isNaN(tmp.low)) || (isNaN(tmp.high)) ) {
this.error("Random: one of the input values is not a number. " + tmp.low_e + tmp.high_e);
} else {
// at this point we have valid values so now to generate the random number!
// flip the values if low > high so random will work
var value = 0;
if (tmp.low > tmp.high) {
value = tmp.low
tmp.low = tmp.high
tmp.high = value
}
// if returning an integer, do a math.ceil() on the low value and a
// Math.floor()high value before generate the random number. This must be
// done to insure the rounding doesn't round up if using something like 4.7
// which would end up with 5
if ( (node.inte == "true") || (node.inte === true) ) {
tmp.low = Math.ceil(tmp.low);
tmp.high = Math.floor(tmp.high);
// use this to round integers
value = Math.round(Math.random() * (tmp.high - tmp.low + 1) + tmp.low - 0.5);
} else {
// use this to round floats
value = (Math.random() * (tmp.high - tmp.low)) + tmp.low;
}
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
}
});
}
RED.nodes.registerType("random",RandomNode);