From 238bcb86981d2624711d1f6c48cd71a1fee959c3 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Mon, 8 Jul 2019 15:54:31 +0100 Subject: [PATCH] Add "has key" rule to switch node + tests --- .../@node-red/nodes/core/logic/10-switch.html | 26 ++++++++++++++++++- .../@node-red/nodes/core/logic/10-switch.js | 4 ++- .../nodes/locales/en-US/messages.json | 3 ++- test/nodes/core/logic/10-switch_spec.js | 11 +++++++- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/logic/10-switch.html b/packages/node_modules/@node-red/nodes/core/logic/10-switch.html index 7dbe0b0ba..83d3f8765 100644 --- a/packages/node_modules/@node-red/nodes/core/logic/10-switch.html +++ b/packages/node_modules/@node-red/nodes/core/logic/10-switch.html @@ -48,6 +48,7 @@ {v:"lte",t:"<=",kind:'V'}, {v:"gt",t:">",kind:'V'}, {v:"gte",t:">=",kind:'V'}, + {v:"exists",t:"switch.rules.exists",kind:'V'}, {v:"btwn",t:"switch.rules.btwn",kind:'V'}, {v:"cont",t:"switch.rules.cont",kind:'V'}, {v:"regex",t:"switch.rules.regex",kind:'V'}, @@ -142,6 +143,7 @@ var typeField = rule.find(".node-input-rule-type-value"); var numField = rule.find(".node-input-rule-num-value"); var expField = rule.find(".node-input-rule-exp-value"); + var keyField = rule.find(".node-input-rule-key-value"); var btwnField1 = rule.find(".node-input-rule-btwn-value"); var btwnField2 = rule.find(".node-input-rule-btwn-value2"); var selectWidth; @@ -160,6 +162,8 @@ numField.typedInput("width",(newWidth-selectWidth-70)); } else if (type === "jsonata_exp") { expField.typedInput("width",(newWidth-selectWidth-70)); + } else if (type === "exists") { + keyField.typedInput("width",(newWidth-selectWidth-70)); } else if (type === "istype") { typeField.typedInput("width",(newWidth-selectWidth-70)); } else { @@ -214,6 +218,7 @@ var btwnValueField = $('',{class:"node-input-rule-btwn-value",type:"text",style:"margin-left: 5px;"}).appendTo(row).typedInput({default:'num',types:['msg','flow','global','str','num','jsonata','env',previousValueType]}); var btwnAndLabel = $('',{class:"node-input-rule-btwn-label"}).text(" "+andLabel+" ").appendTo(row3); var btwnValue2Field = $('',{class:"node-input-rule-btwn-value2",type:"text",style:"margin-left:2px;"}).appendTo(row3).typedInput({default:'num',types:['msg','flow','global','str','num','jsonata','env',previousValueType]}); + var keyValueField = $('',{class:"node-input-rule-key-value",type:"text",style:"margin-left: 5px;"}).appendTo(row).typedInput({default:'str',types:['str','msg','flow','global','env']}); var typeValueField = $('',{class:"node-input-rule-type-value",type:"text",style:"margin-left: 5px;"}).appendTo(row) .typedInput({default:'string',types:[ {value:"string",label:"string",hasValue:false}, @@ -236,6 +241,7 @@ if ((type === "btwn") || (type === "index")) { valueField.typedInput('hide'); expValueField.typedInput('hide'); + keyValueField.typedInput('hide'); numValueField.typedInput('hide'); typeValueField.typedInput('hide'); btwnValueField.typedInput('show'); @@ -243,19 +249,30 @@ btwnValueField.typedInput('hide'); btwnValue2Field.typedInput('hide'); expValueField.typedInput('hide'); + keyValueField.typedInput('hide'); numValueField.typedInput('show'); typeValueField.typedInput('hide'); valueField.typedInput('hide'); + } else if (type === "exists") { + btwnValueField.typedInput('hide'); + btwnValue2Field.typedInput('hide'); + expValueField.typedInput('hide'); + keyValueField.typedInput('show'); + numValueField.typedInput('hide'); + typeValueField.typedInput('hide'); + valueField.typedInput('hide'); } else if (type === "jsonata_exp") { btwnValueField.typedInput('hide'); btwnValue2Field.typedInput('hide'); expValueField.typedInput('show'); + keyValueField.typedInput('hide'); numValueField.typedInput('hide'); typeValueField.typedInput('hide'); valueField.typedInput('hide'); } else { btwnValueField.typedInput('hide'); expValueField.typedInput('hide'); + keyValueField.typedInput('hide'); numValueField.typedInput('hide'); typeValueField.typedInput('hide'); valueField.typedInput('hide'); @@ -297,7 +314,10 @@ } else if (rule.t === "istype") { typeValueField.typedInput('value',rule.vt); typeValueField.typedInput('type',rule.vt); - } else if (rule.t === "jsonata_exp") { + } else if (rule.t === "exists") { + keyValueField.typedInput('value',rule.v); + keyValueField.typedInput('type',rule.vt); + }else if (rule.t === "jsonata_exp") { expValueField.typedInput('value',rule.v); expValueField.typedInput('type',rule.vt||'jsonata'); } else if (typeof rule.v != "undefined") { @@ -372,12 +392,16 @@ } else if (type === "istype") { r.v = rule.find(".node-input-rule-type-value").typedInput('type'); r.vt = rule.find(".node-input-rule-type-value").typedInput('type'); + } else if (type === "exists") { + r.v = rule.find(".node-input-rule-key-value").typedInput('value'); + r.vt = rule.find(".node-input-rule-key-value").typedInput('type'); } else if (type === "jsonata_exp") { r.v = rule.find(".node-input-rule-exp-value").typedInput('value'); r.vt = rule.find(".node-input-rule-exp-value").typedInput('type'); } else { r.v = rule.find(".node-input-rule-value").typedInput('value'); r.vt = rule.find(".node-input-rule-value").typedInput('type'); + console.log("DONG",r) } if (type === "regex") { r.case = rule.find(".node-input-rule-case").prop("checked"); diff --git a/packages/node_modules/@node-red/nodes/core/logic/10-switch.js b/packages/node_modules/@node-red/nodes/core/logic/10-switch.js index f8e92a7a3..ef999ce3c 100644 --- a/packages/node_modules/@node-red/nodes/core/logic/10-switch.js +++ b/packages/node_modules/@node-red/nodes/core/logic/10-switch.js @@ -47,7 +47,6 @@ module.exports = function(RED) { } return false; }, - 'istype': function(a, b) { if (b === "array") { return Array.isArray(a); } else if (b === "buffer") { return Buffer.isBuffer(a); } @@ -72,6 +71,9 @@ module.exports = function(RED) { var index = parts.index; return ((min <= index) && (index <= max)); }, + 'exists': function(a, b) { + if (typeof b !== "object" ) { return a.hasOwnProperty(b+""); } + }, 'jsonata_exp': function(a, b) { return (b === true); }, 'else': function(a) { return a === true; } }; diff --git a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json index 76e1cf2f8..1bf5f86d6 100755 --- a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json @@ -621,7 +621,8 @@ "tail":"tail", "index":"index between", "exp":"JSONata exp", - "else":"otherwise" + "else":"otherwise", + "exists":"has key" }, "errors": { "invalid-expr": "Invalid JSONata expression: __error__", diff --git a/test/nodes/core/logic/10-switch_spec.js b/test/nodes/core/logic/10-switch_spec.js index 899e6edef..a1b1424ca 100644 --- a/test/nodes/core/logic/10-switch_spec.js +++ b/test/nodes/core/logic/10-switch_spec.js @@ -254,6 +254,16 @@ describe('switch Node', function() { genericSwitchTest("gte", 3, true, true, 3, done); }); + it('should match if a payload has a required property', function(done) { + genericSwitchTest("exists", "a", true, true, {a:1}, done); + }); + it('should not match if a payload does not have a required property', function(done) { + genericSwitchTest("exists", "a", true, false, {b:1}, done); + }); + it('should not match if the key is not a string', function(done) { + genericSwitchTest("exists", 1, true, false, {a:1}, done); + }); + it('should check if payload is between given values', function(done) { twoFieldSwitchTest("btwn", "3", "5", true, true, 4, done); }); @@ -519,7 +529,6 @@ describe('switch Node', function() { singularSwitchTest("nempty", true, false, 0, done); }); - it('should check input against a previous value', function(done) { var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{ "t": "gt", "v": "", "vt": "prev" }],checkall:true,outputs:1,wires:[["helperNode1"]]}, {id:"helperNode1", type:"helper", wires:[]}];