mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Add isEmpty check to switch node
This commit is contained in:
parent
28402b0894
commit
e94708606d
@ -577,6 +577,8 @@
|
|||||||
"null":"is null",
|
"null":"is null",
|
||||||
"nnull":"is not null",
|
"nnull":"is not null",
|
||||||
"istype":"is of type",
|
"istype":"is of type",
|
||||||
|
"empty":"is empty",
|
||||||
|
"nempty":"is not empty",
|
||||||
"head":"head",
|
"head":"head",
|
||||||
"tail":"tail",
|
"tail":"tail",
|
||||||
"index":"index between",
|
"index":"index between",
|
||||||
|
@ -60,6 +60,12 @@
|
|||||||
<li>An <b>Otherwise</b> rule can be used to match if none of the preceeding
|
<li>An <b>Otherwise</b> rule can be used to match if none of the preceeding
|
||||||
rules have matched.</li>
|
rules have matched.</li>
|
||||||
</ol>
|
</ol>
|
||||||
|
<h4>Notes</h4>
|
||||||
|
<p>The <code>is true/false</code> and <code>is null</code> rules perform strict
|
||||||
|
comparisons against those types. They do not convert between types.</p>
|
||||||
|
<p>The <code>is empty</code> rule passes for Strings, Arrays and Buffers that have
|
||||||
|
a length of 0, or Objects that have no properties. It does not pass for <code>null</code>
|
||||||
|
or <code>undefined</code> values.</p>
|
||||||
<h4>Handling message sequences</h4>
|
<h4>Handling message sequences</h4>
|
||||||
<p>By default, the node does not modify the <code>msg.parts</code> property of messages
|
<p>By default, the node does not modify the <code>msg.parts</code> property of messages
|
||||||
that are part of a sequence.</p>
|
that are part of a sequence.</p>
|
||||||
@ -86,6 +92,8 @@
|
|||||||
{v:"null",t:"switch.rules.null",kind:'V'},
|
{v:"null",t:"switch.rules.null",kind:'V'},
|
||||||
{v:"nnull",t:"switch.rules.nnull",kind:'V'},
|
{v:"nnull",t:"switch.rules.nnull",kind:'V'},
|
||||||
{v:"istype",t:"switch.rules.istype",kind:'V'},
|
{v:"istype",t:"switch.rules.istype",kind:'V'},
|
||||||
|
{v:"empty",t:"switch.rules.empty",kind:'V'},
|
||||||
|
{v:"nempty",t:"switch.rules.nempty",kind:'V'},
|
||||||
{v:"head",t:"switch.rules.head",kind:'S'},
|
{v:"head",t:"switch.rules.head",kind:'S'},
|
||||||
{v:"index",t:"switch.rules.index",kind:'S'},
|
{v:"index",t:"switch.rules.index",kind:'S'},
|
||||||
{v:"tail",t:"switch.rules.tail",kind:'S'},
|
{v:"tail",t:"switch.rules.tail",kind:'S'},
|
||||||
|
@ -31,6 +31,23 @@ module.exports = function(RED) {
|
|||||||
'false': function(a) { return a === false; },
|
'false': function(a) { return a === false; },
|
||||||
'null': function(a) { return (typeof a == "undefined" || a === null); },
|
'null': function(a) { return (typeof a == "undefined" || a === null); },
|
||||||
'nnull': function(a) { return (typeof a != "undefined" && a !== null); },
|
'nnull': function(a) { return (typeof a != "undefined" && a !== null); },
|
||||||
|
'empty': function(a) {
|
||||||
|
if (typeof a === 'string' || Array.isArray(a) || Buffer.isBuffer(a)) {
|
||||||
|
return a.length === 0;
|
||||||
|
} else if (typeof a === 'object') {
|
||||||
|
return Object.keys(a).length === 0;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
'nempty': function(a) {
|
||||||
|
if (typeof a === 'string' || Array.isArray(a) || Buffer.isBuffer(a)) {
|
||||||
|
return a.length !== 0;
|
||||||
|
} else if (typeof a === 'object') {
|
||||||
|
return Object.keys(a).length !== 0;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
},
|
||||||
|
|
||||||
'istype': function(a, b) {
|
'istype': function(a, b) {
|
||||||
if (b === "array") { return Array.isArray(a); }
|
if (b === "array") { return Array.isArray(a); }
|
||||||
else if (b === "buffer") { return Buffer.isBuffer(a); }
|
else if (b === "buffer") { return Buffer.isBuffer(a); }
|
||||||
|
@ -125,7 +125,7 @@ describe('switch Node', function() {
|
|||||||
helper.load(switchNode, flow, function() {
|
helper.load(switchNode, flow, function() {
|
||||||
var switchNode1 = helper.getNode("switchNode1");
|
var switchNode1 = helper.getNode("switchNode1");
|
||||||
var helperNode1 = helper.getNode("helperNode1");
|
var helperNode1 = helper.getNode("helperNode1");
|
||||||
var sid = undefined;
|
var sid;
|
||||||
var count = 0;
|
var count = 0;
|
||||||
if (modifier !== undefined) {
|
if (modifier !== undefined) {
|
||||||
modifier(switchNode1);
|
modifier(switchNode1);
|
||||||
@ -333,6 +333,75 @@ describe('switch Node', function() {
|
|||||||
singularSwitchTest(false, true, false, true, done);
|
singularSwitchTest(false, true, false, true, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should check if payload is empty (string)', function(done) {
|
||||||
|
singularSwitchTest("empty", true, true, "", done);
|
||||||
|
});
|
||||||
|
it('should check if payload is empty (array)', function(done) {
|
||||||
|
singularSwitchTest("empty", true, true, [], done);
|
||||||
|
});
|
||||||
|
it('should check if payload is empty (buffer)', function(done) {
|
||||||
|
singularSwitchTest("empty", true, true, Buffer.alloc(0), done);
|
||||||
|
});
|
||||||
|
it('should check if payload is empty (object)', function(done) {
|
||||||
|
singularSwitchTest("empty", true, true, {}, done);
|
||||||
|
});
|
||||||
|
it('should check if payload is empty (non-empty string)', function(done) {
|
||||||
|
singularSwitchTest("empty", true, false, "1", done);
|
||||||
|
});
|
||||||
|
it('should check if payload is empty (non-empty array)', function(done) {
|
||||||
|
singularSwitchTest("empty", true, false, [1], done);
|
||||||
|
});
|
||||||
|
it('should check if payload is empty (non-empty buffer)', function(done) {
|
||||||
|
singularSwitchTest("empty", true, false, Buffer.alloc(1), done);
|
||||||
|
});
|
||||||
|
it('should check if payload is empty (non-empty object)', function(done) {
|
||||||
|
singularSwitchTest("empty", true, false, {a:1}, done);
|
||||||
|
});
|
||||||
|
it('should check if payload is empty (null)', function(done) {
|
||||||
|
singularSwitchTest("empty", true, false, null, done);
|
||||||
|
});
|
||||||
|
it('should check if payload is empty (undefined)', function(done) {
|
||||||
|
singularSwitchTest("empty", true, false, undefined, done);
|
||||||
|
});
|
||||||
|
it('should check if payload is empty (0)', function(done) {
|
||||||
|
singularSwitchTest("empty", true, false, null, done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should check if payload is not empty (string)', function(done) {
|
||||||
|
singularSwitchTest("nempty", true, !true, "", done);
|
||||||
|
});
|
||||||
|
it('should check if payload is not empty (array)', function(done) {
|
||||||
|
singularSwitchTest("nempty", true, !true, [], done);
|
||||||
|
});
|
||||||
|
it('should check if payload is not empty (buffer)', function(done) {
|
||||||
|
singularSwitchTest("nempty", true, !true, Buffer.alloc(0), done);
|
||||||
|
});
|
||||||
|
it('should check if payload is not empty (object)', function(done) {
|
||||||
|
singularSwitchTest("nempty", true, !true, {}, done);
|
||||||
|
});
|
||||||
|
it('should check if payload is not empty (non-empty string)', function(done) {
|
||||||
|
singularSwitchTest("nempty", true, !false, "1", done);
|
||||||
|
});
|
||||||
|
it('should check if payload is not empty (non-empty array)', function(done) {
|
||||||
|
singularSwitchTest("nempty", true, !false, [1], done);
|
||||||
|
});
|
||||||
|
it('should check if payload is not empty (non-empty buffer)', function(done) {
|
||||||
|
singularSwitchTest("nempty", true, !false, Buffer.alloc(1), done);
|
||||||
|
});
|
||||||
|
it('should check if payload is not empty (non-empty object)', function(done) {
|
||||||
|
singularSwitchTest("nempty", true, !false, {a:1}, done);
|
||||||
|
});
|
||||||
|
it('should check if payload is not empty (null)', function(done) {
|
||||||
|
singularSwitchTest("nempty", true, false, null, done);
|
||||||
|
});
|
||||||
|
it('should check if payload is not empty (undefined)', function(done) {
|
||||||
|
singularSwitchTest("nempty", true, false, undefined, done);
|
||||||
|
});
|
||||||
|
it('should check if payload is not empty (0)', function(done) {
|
||||||
|
singularSwitchTest("nempty", true, false, null, done);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
it('should check input against a previous value', function(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"]]},
|
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:[]}];
|
{id:"helperNode1", type:"helper", wires:[]}];
|
||||||
@ -666,9 +735,11 @@ describe('switch Node', function() {
|
|||||||
var vals = new Array(port_count);
|
var vals = new Array(port_count);
|
||||||
var recv_count = 0;
|
var recv_count = 0;
|
||||||
for (var id in outs) {
|
for (var id in outs) {
|
||||||
var out = outs[id];
|
if (outs.hasOwnProperty(id)) {
|
||||||
vals[out.port] = out.vals;
|
var out = outs[id];
|
||||||
recv_count += out.vals.length;
|
vals[out.port] = out.vals;
|
||||||
|
recv_count += out.vals.length;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
var count = 0;
|
var count = 0;
|
||||||
function check_msg(msg, ix, vf) {
|
function check_msg(msg, ix, vf) {
|
||||||
@ -703,8 +774,8 @@ describe('switch Node', function() {
|
|||||||
}
|
}
|
||||||
var index = parts.index;
|
var index = parts.index;
|
||||||
var eindex = counts[ix];
|
var eindex = counts[ix];
|
||||||
var eval = evals[eindex];
|
var value = evals[eindex];
|
||||||
payload.should.equal(eval);
|
payload.should.equal(value);
|
||||||
counts[ix]++;
|
counts[ix]++;
|
||||||
count++;
|
count++;
|
||||||
if (count === recv_count) {
|
if (count === recv_count) {
|
||||||
@ -716,18 +787,22 @@ describe('switch Node', function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (var id in outs) {
|
for (var id in outs) {
|
||||||
(function() {
|
if (outs.hasOwnProperty(id)) {
|
||||||
var node = helper.getNode(id);
|
(function() {
|
||||||
var port = outs[id].port;
|
var node = helper.getNode(id);
|
||||||
var vf = outs[id].vf;
|
var port = outs[id].port;
|
||||||
node.on("input", function(msg) {
|
var vf = outs[id].vf;
|
||||||
check_msg(msg, port, vf);
|
node.on("input", function(msg) {
|
||||||
});
|
check_msg(msg, port, vf);
|
||||||
})();
|
});
|
||||||
|
})();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for(var i in seq_in) {
|
for(var i in seq_in) {
|
||||||
n1.receive({payload:seq_in[i], xindex:i,
|
if (seq_in.hasOwnProperty(i)) {
|
||||||
parts:{index:i, count:seq_in.length, id:222}});
|
n1.receive({payload:seq_in[i], xindex:i,
|
||||||
|
parts:{index:i, count:seq_in.length, id:222}});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user