added better validation of inputs, added node status

This commit is contained in:
juggledad 2020-11-12 08:03:23 -05:00 committed by GitHub
parent 175dc4c422
commit e68d012bf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,49 +4,63 @@ module.exports = function(RED) {
function RandomNode(n) { function RandomNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.low = Number(n.low) || ""; this.status({}); // blank status on a deploy
this.high = Number(n.high) || "";
this.inte = n.inte || false; this.inte = n.inte || false;
this.property = n.property||"payload"; this.property = n.property||"payload";
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg) {
if (n.low !== "") {this.low = parseFloat(n.low)}
else {this.low = n.low};
if (n.high !== "") {this.high = parseFloat(n.high)}
else {this.high = n.high};
if (node.low === "") {
node.low = 1;
if ('from' in msg) {node.low = msg.from}
}
if (node.high === "") {
node.high = 10;
if ('to' in msg) {node.high = msg.to}
}
if (node.low === "") { // if returning an interger, do a parseInt() on the low and high values
node.low = 1 if ( (node.inte == "true") || (node.inte === true) ) {
if ('from' in msg) { node.low = parseInt(node.low);
if ( (typeof msg.from === 'number') || ( (typeof msg.from === 'string') && (!isNaN(Number(msg.from)) ) )) { node.high = parseInt(node.high);
node.low = Number(msg.from) }
}
} // flip the values if low > high so random will work
} var value;
if (node.low > node.high) {
if (node.high === "") { value = node.low
node.high = 10 node.low = node.high
if ('to' in msg) { node.high = value
if ( (typeof msg.to === 'number') || ( (typeof msg.to === 'string') && (!isNaN(Number(msg.to)) ) )) { }
node.high = Number(msg.to) // generate the random number
} if ( (node.inte == "true") || (node.inte === true) ) {
} node.low = Math.round(node.low)
} node.high = Math.round(node.high)
value = Math.round(Math.random() * (node.high - node.low + 1) + node.low - 0.5);
} else {
value = Math.random() * (node.high - node.low) + node.low;
}
// flip the values if low > high RED.util.setMessageProperty(msg,node.property,value);
var value;
if (node.low > node.high) {
value = node.low
node.low = node.high
node.high = value
}
if (node.inte == "true" || node.inte === true) { // output a status under the node
value = Math.round(Math.random() * (node.high - node.low + 1) + node.low - 0.5); var color = "grey";
} if ( (isNaN(node.low)) || (isNaN(node.high)) ) {
else { this.status({fill:"red",shape:"dot",text:"random: from " + node.low + " to " + node.high})
value = Math.random() * (node.high - node.low) + node.low; this.error({node:"random",text:"one of the input values is not a number"})
} } else {
RED.util.setMessageProperty(msg,node.property,value); this.status({fill:"grey",shape:"dot",text:"random: from " + node.low + " to " + node.high + " value=" + value});
node.send(msg); node.send(msg);
}
}); });
} }
RED.nodes.registerType("random",RandomNode); RED.nodes.registerType("random",RandomNode);