1
0
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:
Nick O'Leary 2018-07-11 16:14:09 +01:00
parent 28402b0894
commit e94708606d
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
4 changed files with 118 additions and 16 deletions

View File

@ -577,6 +577,8 @@
"null":"is null",
"nnull":"is not null",
"istype":"is of type",
"empty":"is empty",
"nempty":"is not empty",
"head":"head",
"tail":"tail",
"index":"index between",

View File

@ -60,6 +60,12 @@
<li>An <b>Otherwise</b> rule can be used to match if none of the preceeding
rules have matched.</li>
</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>
<p>By default, the node does not modify the <code>msg.parts</code> property of messages
that are part of a sequence.</p>
@ -86,6 +92,8 @@
{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:"empty",t:"switch.rules.empty",kind:'V'},
{v:"nempty",t:"switch.rules.nempty",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'},

View File

@ -31,6 +31,23 @@ 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); },
'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) {
if (b === "array") { return Array.isArray(a); }
else if (b === "buffer") { return Buffer.isBuffer(a); }

View File

@ -125,7 +125,7 @@ describe('switch Node', function() {
helper.load(switchNode, flow, function() {
var switchNode1 = helper.getNode("switchNode1");
var helperNode1 = helper.getNode("helperNode1");
var sid = undefined;
var sid;
var count = 0;
if (modifier !== undefined) {
modifier(switchNode1);
@ -333,6 +333,75 @@ describe('switch Node', function() {
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) {
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:[]}];
@ -666,10 +735,12 @@ describe('switch Node', function() {
var vals = new Array(port_count);
var recv_count = 0;
for (var id in outs) {
if (outs.hasOwnProperty(id)) {
var out = outs[id];
vals[out.port] = out.vals;
recv_count += out.vals.length;
}
}
var count = 0;
function check_msg(msg, ix, vf) {
try {
@ -703,8 +774,8 @@ describe('switch Node', function() {
}
var index = parts.index;
var eindex = counts[ix];
var eval = evals[eindex];
payload.should.equal(eval);
var value = evals[eindex];
payload.should.equal(value);
counts[ix]++;
count++;
if (count === recv_count) {
@ -716,6 +787,7 @@ describe('switch Node', function() {
}
}
for (var id in outs) {
if (outs.hasOwnProperty(id)) {
(function() {
var node = helper.getNode(id);
var port = outs[id].port;
@ -725,10 +797,13 @@ describe('switch Node', function() {
});
})();
}
}
for(var i in seq_in) {
if (seq_in.hasOwnProperty(i)) {
n1.receive({payload:seq_in[i], xindex:i,
parts:{index:i, count:seq_in.length, id:222}});
}
}
});
}