mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-03-01 10:37:43 +00:00
Add option to dynamically pass in 'From' and 'To' - fixed bug when 'From' was greater than 'To' by flipping the two
This commit is contained in:
parent
3b7fb0aa95
commit
eaa1acf83a
@ -3,13 +3,44 @@ 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 = Number(n.low) || "";
|
||||
this.high = Number(n.high) || "";
|
||||
this.inte = n.inte || false;
|
||||
this.property = n.property||"payload";
|
||||
var node = this;
|
||||
|
||||
|
||||
this.on("input", function(msg) {
|
||||
|
||||
if (node.low === "") {
|
||||
node.low = 1
|
||||
if ('from' in msg) {
|
||||
if ( (typeof msg.from === 'number')
|
||||
|| ( (typeof msg.from === 'string') && (!isNaN(Number(msg.from)) ) )) {
|
||||
node.low = Number(msg.from)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (node.high === "") {
|
||||
node.high = 10
|
||||
if ('to' in msg) {
|
||||
if ( (typeof msg.to === 'number')
|
||||
|| ( (typeof msg.to === 'string') && (!isNaN(Number(msg.to)) ) )) {
|
||||
node.high = Number(msg.to)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// flip the values if low > high
|
||||
var value;
|
||||
if (node.low > node.high) {
|
||||
value = node.low
|
||||
node.low = node.high
|
||||
node.high = value
|
||||
}
|
||||
|
||||
if (node.inte == "true" || node.inte === true) {
|
||||
value = Math.round(Math.random() * (node.high - node.low + 1) + node.low - 0.5);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user