mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-03-01 10:37:43 +00:00
added better validation of inputs, added node status
This commit is contained in:
parent
175dc4c422
commit
e68d012bf2
@ -4,49 +4,63 @@ module.exports = function(RED) {
|
||||
function RandomNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
|
||||
this.low = Number(n.low) || "";
|
||||
this.high = Number(n.high) || "";
|
||||
this.status({}); // blank status on a deploy
|
||||
|
||||
this.inte = n.inte || false;
|
||||
this.property = n.property||"payload";
|
||||
var node = this;
|
||||
|
||||
|
||||
|
||||
|
||||
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 === "") {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
// if returning an interger, do a parseInt() on the low and high values
|
||||
if ( (node.inte == "true") || (node.inte === true) ) {
|
||||
node.low = parseInt(node.low);
|
||||
node.high = parseInt(node.high);
|
||||
}
|
||||
|
||||
// flip the values if low > high so random will work
|
||||
var value;
|
||||
if (node.low > node.high) {
|
||||
value = node.low
|
||||
node.low = node.high
|
||||
node.high = value
|
||||
}
|
||||
// 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
|
||||
var value;
|
||||
if (node.low > node.high) {
|
||||
value = node.low
|
||||
node.low = node.high
|
||||
node.high = value
|
||||
}
|
||||
RED.util.setMessageProperty(msg,node.property,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);
|
||||
// output a status under the node
|
||||
var color = "grey";
|
||||
if ( (isNaN(node.low)) || (isNaN(node.high)) ) {
|
||||
this.status({fill:"red",shape:"dot",text:"random: from " + node.low + " to " + node.high})
|
||||
this.error({node:"random",text:"one of the input values is not a number"})
|
||||
} else {
|
||||
this.status({fill:"grey",shape:"dot",text:"random: from " + node.low + " to " + node.high + " value=" + value});
|
||||
node.send(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("random",RandomNode);
|
||||
|
Loading…
x
Reference in New Issue
Block a user