1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add "has key" rule to switch node + tests

This commit is contained in:
Dave Conway-Jones 2019-07-08 15:54:31 +01:00
parent 944f3bd329
commit 238bcb8698
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
4 changed files with 40 additions and 4 deletions

View File

@ -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 = $('<input/>',{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 = $('<span/>',{class:"node-input-rule-btwn-label"}).text(" "+andLabel+" ").appendTo(row3);
var btwnValue2Field = $('<input/>',{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 = $('<input/>',{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 = $('<input/>',{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");

View File

@ -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; }
};

View File

@ -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__",

View File

@ -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:[]}];