From 78bb05cc04dc9287838f47e8f84c8718c6a09eff Mon Sep 17 00:00:00 2001 From: juggledad Date: Sat, 14 Nov 2020 06:08:35 -0500 Subject: [PATCH] Changed code to allow for default values to be used --- function/random/random.js | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/function/random/random.js b/function/random/random.js index b8a52e4f..aaa73aa6 100644 --- a/function/random/random.js +++ b/function/random/random.js @@ -9,31 +9,32 @@ module.exports = function(RED) { this.inte = n.inte || false; this.property = n.property||"payload"; var node = this; - var tmp = {}; + var tmp = {}; this.on("input", function(msg) { - if (node.low === "") { - tmp.low = 1; - if ('from' in msg) { - tmp.low = msg.from} + if (node.low) { // if the the node has a value use it + tmp.low = node.low + } else { // if 'from' in the msg, use it or default to 1 + tmp.low = ('from' in msg) ? msg.from : 1; } - - if (node.high === "") { - tmp.high = 10; - if ('to' in msg) {tmp.high = msg.to} + + if (node.high) { // if the the node has a value use it + tmp.high = node.high + } else { // if 'to' in the msg, use it or default to 1 + tmp.high = ('to' in msg) ? msg.to : 10; } - // if tmp.low or tmp.high is not a number flag an error and do not send out a msg + // if tmp.low or high are not numbers, send an error msg if ( (isNaN(tmp.low)) || (isNaN(tmp.high)) ) { this.error({node:"random",text:"one of the input values is not a number"}) } else { - // force tmp.high/low to Numbers since they may be a string + // force tmp.high/low to Numbers since they may be a string tmp.low = Number(tmp.low) tmp.high = Number(tmp.high) - // flip the values if low > high so random will work + // flip the values if low > high so random will work var value = 0; if (tmp.low > tmp.high) { value = tmp.low @@ -41,9 +42,9 @@ module.exports = function(RED) { tmp.high = value } - // if returning an integer, do a parseInt() on the low and high values - // before generate the random number. This must be done to insure the rounding - // doesn't go up if using something like 4.7 or you can end up with 5 + // if returning an integer, do a parseInt() on the low and high values + // before generate the random number. This must be done to insure the rounding + // doesn't go up if using something like 4.7 or you can end up with 5 if ( (node.inte == "true") || (node.inte === true) ) { tmp.low = parseInt(tmp.low); tmp.high = parseInt(tmp.high); @@ -55,7 +56,7 @@ module.exports = function(RED) { RED.util.setMessageProperty(msg,node.property,value); node.send(msg); - } + } }); } RED.nodes.registerType("random",RandomNode);