diff --git a/nodes/core/locales/en-US/messages.json b/nodes/core/locales/en-US/messages.json
index 62d2ea46a..c93270e95 100644
--- a/nodes/core/locales/en-US/messages.json
+++ b/nodes/core/locales/en-US/messages.json
@@ -568,6 +568,7 @@
"false":"is false",
"null":"is null",
"nnull":"is not null",
+ "istype":"is of type",
"head":"head",
"tail":"tail",
"index":"index between",
diff --git a/nodes/core/logic/10-switch.html b/nodes/core/logic/10-switch.html
index 10491e451..ffa5a48e5 100644
--- a/nodes/core/logic/10-switch.html
+++ b/nodes/core/logic/10-switch.html
@@ -85,6 +85,7 @@
{v:"false",t:"switch.rules.false",kind:'V'},
{v:"null",t:"switch.rules.null",kind:'V'},
{v:"nnull",t:"switch.rules.nnull",kind:'V'},
+ {v:"istype",t:"switch.rules.istype",kind:'V'},
{v:"head",t:"switch.rules.head",kind:'S'},
{v:"index",t:"switch.rules.index",kind:'S'},
{v:"tail",t:"switch.rules.tail",kind:'S'},
@@ -160,6 +161,7 @@
var selectField = rule.find("select");
var type = selectField.val()||"";
var valueField = rule.find(".node-input-rule-value");
+ 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 btwnField1 = rule.find(".node-input-rule-btwn-value");
@@ -180,6 +182,8 @@
numField.typedInput("width",(newWidth-selectWidth-70));
} else if (type === "jsonata_exp") {
expField.typedInput("width",(newWidth-selectWidth-70));
+ } else if (type === "istype") {
+ typeField.typedInput("width",(newWidth-selectWidth-70));
} else {
if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
// valueField.hide();
@@ -232,6 +236,18 @@
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',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',previousValueType]});
+ 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},
+ {value:"number",label:"number",hasValue:false},
+ {value:"boolean",label:"boolean",hasValue:false},
+ {value:"array",label:"array",hasValue:false},
+ {value:"buffer",label:"buffer",hasValue:false},
+ {value:"object",label:"object",hasValue:false},
+ {value:"json",label:"JSON string",hasValue:false},
+ {value:"undefined",label:"undefined",hasValue:false},
+ {value:"null",label:"null",hasValue:false}
+ ]});
var finalspan = $('',{style:"float: right;margin-top: 6px;"}).appendTo(row);
finalspan.append(' → '+(i+1)+' ');
var caseSensitive = $('',{id:"node-input-rule-case-"+i,class:"node-input-rule-case",type:"checkbox",style:"width:auto;vertical-align:top"}).appendTo(row2);
@@ -243,26 +259,39 @@
valueField.typedInput('hide');
expValueField.typedInput('hide');
numValueField.typedInput('hide');
+ typeValueField.typedInput('hide');
btwnValueField.typedInput('show');
} else if ((type === "head") || (type === "tail")) {
btwnValueField.typedInput('hide');
btwnValue2Field.typedInput('hide');
expValueField.typedInput('hide');
numValueField.typedInput('show');
+ typeValueField.typedInput('hide');
valueField.typedInput('hide');
} else if (type === "jsonata_exp") {
btwnValueField.typedInput('hide');
btwnValue2Field.typedInput('hide');
expValueField.typedInput('show');
numValueField.typedInput('hide');
+ typeValueField.typedInput('hide');
valueField.typedInput('hide');
} else {
btwnValueField.typedInput('hide');
expValueField.typedInput('hide');
numValueField.typedInput('hide');
+ typeValueField.typedInput('hide');
+ valueField.typedInput('hide');
if (type === "true" || type === "false" || type === "null" || type === "nnull" || type === "else") {
valueField.typedInput('hide');
- } else {
+ typeValueField.typedInput('hide');
+ }
+ else
+ if (type === "istype") {
+ valueField.typedInput('hide');
+ typeValueField.typedInput('show');
+ }
+ else {
+ typeValueField.typedInput('hide');
valueField.typedInput('show');
}
}
@@ -287,6 +316,9 @@
} else if ((rule.t === "head") || (rule.t === "tail")) {
numValueField.typedInput('value',rule.v);
numValueField.typedInput('type',rule.vt||'num');
+ } else if (rule.t === "istype") {
+ typeValueField.typedInput('value',rule.vt);
+ typeValueField.typedInput('type',rule.vt);
} else if (rule.t === "jsonata_exp") {
expValueField.typedInput('value',rule.v);
expValueField.typedInput('type',rule.vt||'jsonata');
@@ -359,6 +391,9 @@
} else if ((type === "head") || (type === "tail")) {
r.v = rule.find(".node-input-rule-num-value").typedInput('value');
r.vt = rule.find(".node-input-rule-num-value").typedInput('type');
+ } 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 === "jsonata_exp") {
r.v = rule.find(".node-input-rule-exp-value").typedInput('value');
r.vt = rule.find(".node-input-rule-exp-value").typedInput('type');
diff --git a/nodes/core/logic/10-switch.js b/nodes/core/logic/10-switch.js
index 1fa9fd02c..919fe13ac 100644
--- a/nodes/core/logic/10-switch.js
+++ b/nodes/core/logic/10-switch.js
@@ -31,6 +31,16 @@ module.exports = function(RED) {
'false': function(a) { return a === false; },
'null': function(a) { return (typeof a == "undefined" || a === null); },
'nnull': function(a) { return (typeof a != "undefined" && a !== null); },
+ 'istype': function(a, b) {
+ if (b === "array") { return Array.isArray(a); }
+ else if (b === "buffer") { return Buffer.isBuffer(a); }
+ else if (b === "json") {
+ try { JSON.parse(a); return true; } // or maybe ??? a !== null; }
+ catch(e) { return false;}
+ }
+ else if (b === "null") { return a === null; }
+ else { return typeof a === b && !Array.isArray(a) && !Buffer.isBuffer(a) && a !== null; }
+ },
'head': function(a, b, c, d, parts) {
var count = Number(b);
return (parts.index < count);
@@ -292,6 +302,10 @@ module.exports = function(RED) {
node.error(RED._("switch.errors.invalid-expr",{error:err.message}));
return;
}
+ } else if (rule.vt === 'json') {
+ v1 = "json";
+ } else if (rule.vt === 'null') {
+ v1 = "null";
} else {
try {
v1 = RED.util.evaluateNodeProperty(rule.v,rule.vt,node,msg);
diff --git a/test/nodes/core/logic/10-switch_spec.js b/test/nodes/core/logic/10-switch_spec.js
index a5d057c92..abdfac83a 100644
--- a/test/nodes/core/logic/10-switch_spec.js
+++ b/test/nodes/core/logic/10-switch_spec.js
@@ -103,7 +103,7 @@ describe('switch Node', function() {
helperNode1.on("input", function(msg) {
try {
if (shouldReceive === true) {
- msg.payload.should.equal(sendPayload);
+ should.equal(msg.payload,sendPayload);
done();
} else {
should.fail(null, null, "We should never get an input!");
@@ -168,8 +168,6 @@ describe('switch Node', function() {
});
}
-
-
it('should check if payload equals given value', function(done) {
genericSwitchTest("eq", "Hello", true, true, "Hello", done);
});
@@ -258,6 +256,43 @@ describe('switch Node', function() {
genericSwitchTest("regex", "[abc]+", true, true, "abbabac", done);
});
+ it('should check if payload if of type string ', function(done) {
+ genericSwitchTest("istype", "string", true, true, "Hello", done);
+ });
+ it('should check if payload if of type number ', function(done) {
+ genericSwitchTest("istype", "number", true, true, 999, done);
+ });
+ it('should check if payload if of type number 0', function(done) {
+ genericSwitchTest("istype", "number", true, true, 0, done);
+ });
+ it('should check if payload if of type boolean true', function(done) {
+ genericSwitchTest("istype", "boolean", true, true, true, done);
+ });
+ it('should check if payload if of type boolean false', function(done) {
+ genericSwitchTest("istype", "boolean", true, true, true, done);
+ });
+ it('should check if payload if of type array ', function(done) {
+ genericSwitchTest("istype", "array", true, true, [1,2,3,"a","b"], done);
+ });
+ it('should check if payload if of type buffer ', function(done) {
+ genericSwitchTest("istype", "buffer", true, true, Buffer.from("Hello"), done);
+ });
+ it('should check if payload if of type object ', function(done) {
+ genericSwitchTest("istype", "object", true, true, {a:1,b:"b",c:true}, done);
+ });
+ it('should check if payload if of type JSON string ', function(done) {
+ genericSwitchTest("istype", "json", true, true, JSON.stringify({a:1,b:"b",c:true}), done);
+ });
+ it('should check if payload if of type JSON string (and fail if not) ', function(done) {
+ genericSwitchTest("istype", "json", true, false, "Hello", done);
+ });
+ it('should check if payload if of type null', function(done) {
+ genericSwitchTest("istype", "null", true, true, null, done);
+ });
+ it('should check if payload if of type undefined', function(done) {
+ genericSwitchTest("istype", "undefined", true, true, undefined, done);
+ });
+
it('should match regex with ignore-case flag set true', function(done) {
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"regex","v":"onetwothree","case":true}],checkall:true,outputs:1,wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
@@ -433,7 +468,6 @@ describe('switch Node', function() {
var flow = [{id:"switchNode1",type:"switch",name:"switchNode",property:"payload",rules:[{"t":"nnull"}],checkall:false,outputs:1,wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
-
helper.load(switchNode, flow, function() {
var switchNode1 = helper.getNode("switchNode1");
var helperNode1 = helper.getNode("helperNode1");
@@ -787,5 +821,4 @@ describe('switch Node', function() {
n1.receive({payload:1, parts:{index:0, count:4, id:222}});
});
});
-
});