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

Change node can set msg property to another msg property. Closes #456

This commit is contained in:
Anna Thomas 2014-10-28 11:02:46 +00:00
parent bd75c1c753
commit ce6513e7f7
3 changed files with 403 additions and 283 deletions

View File

@ -61,7 +61,9 @@
action: {value:"replace",required:true}, action: {value:"replace",required:true},
property: {value:"payload",required:true}, property: {value:"payload",required:true},
from: {value:"",validate: function(v) { from: {value:"",validate: function(v) {
if (this.action == "change" && this.reg) { if (this.action === "change" && !this.from) {
return false;
} else if (this.action === "change" && this.reg) {
try { try {
var re = new RegExp(this.from, "g"); var re = new RegExp(this.from, "g");
return true; return true;

View File

@ -16,6 +16,7 @@
module.exports = function(RED) { module.exports = function(RED) {
"use strict"; "use strict";
function ChangeNode(n) { function ChangeNode(n) {
RED.nodes.createNode(this, n); RED.nodes.createNode(this, n);
this.action = n.action; this.action = n.action;
@ -28,7 +29,7 @@ module.exports = function(RED) {
this.from = this.from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); this.from = this.from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
} }
this.on('input', function (msg) { this.on('input', function(msg) {
var propertyParts; var propertyParts;
var depth = 0; var depth = 0;
@ -42,19 +43,40 @@ module.exports = function(RED) {
propertyParts = node.property.split("."); propertyParts = node.property.split(".");
try { try {
propertyParts.reduce(function (obj, i) { propertyParts.reduce(function(obj, i) {
var to = node.to;
// Set msg from property to another msg property
if (node.action === "replace" && node.to.indexOf("msg.") === 0) {
var parts = to.substring(4);
var msgPropParts = parts.split(".");
try {
msgPropParts.reduce(function(ob, j) {
to = (typeof ob[j] !== "undefined" ? ob[j] : undefined);
return to;
}, msg);
} catch (err) {}
}
if (++depth === propertyParts.length) { if (++depth === propertyParts.length) {
if (node.action === "change") { if (node.action === "change") {
if (typeof obj[i] === "string") { if (typeof obj[i] === "string") {
obj[i] = obj[i].replace(node.re, node.to); obj[i] = obj[i].replace(node.re, node.to);
} }
} else if (node.action === "replace") { } else if (node.action === "replace") {
obj[i] = node.to; if (typeof to === "undefined") {
delete(obj[i]);
} else {
obj[i] = to;
}
} else if (node.action === "delete") { } else if (node.action === "delete") {
delete(obj[i]); delete(obj[i]);
} }
} else { } else {
if (!obj[i]) { // to property doesn't exist, don't create empty object
if (typeof to === "undefined") {
return;
// setting a non-existent multilevel object, create empty parent
} else if (!obj[i]) {
obj[i] = {}; obj[i] = {};
} }
return obj[i]; return obj[i];

View File

@ -30,307 +30,403 @@ describe('ChangeNode', function() {
helper.stopServer(done); helper.stopServer(done);
}); });
it('should be loaded', function(done) { describe('#replace' , function() {
var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"payload","from":"","to":"","reg":false,"name":"changeNode","wires":[[]]}]; it('should be loaded', function(done) {
helper.load(changeNode, flow, function() { var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"payload","from":"","to":"","reg":false,"name":"changeNode","wires":[[]]}];
var changeNode1 = helper.getNode("changeNode1"); helper.load(changeNode, flow, function() {
changeNode1.should.have.property('name', 'changeNode'); var changeNode1 = helper.getNode("changeNode1");
done(); changeNode1.should.have.property('name', 'changeNode');
}); done();
});
it('sets the value of the message property', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"payload","from":"","to":"changed","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.payload.should.equal("changed");
done();
} catch(err) {
done(err);
}
}); });
changeNode1.receive({payload:"changeMe"});
}); });
});
it('sets the value of an already set multi-level message property', function(done) { it('sets the value of the message property', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"foo.bar","from":"","to":"bar","reg":false,"name":"changeNode","wires":[["helperNode1"]]}, var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"payload","from":"","to":"changed","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() { helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1"); var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1"); var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) { helperNode1.on("input", function(msg) {
try {
msg.foo.bar.should.equal("bar");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({foo:{bar:"foo"}});
});
});
it('sets the value of an empty multi-level message property', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"foo.bar","from":"","to":"bar","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.foo.bar.should.equal("bar");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({});
});
});
it('changes the value of the message property', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"change","property":"payload","from":"Hello","to":"Goodbye","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.payload.should.equal("Goodbye World!");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:"Hello World!"});
});
});
it('changes the value of a multi-level message property', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"change","property":"foo.bar","from":"Hello","to":"Goodbye","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.foo.bar.should.equal("Goodbye World!");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({foo:{bar:"Hello World!"}});
});
});
it('sends unaltered message if the changed message property does not exist', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"change","property":"foo","from":"Hello","to":"Goodbye","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.payload.should.equal("Hello World!");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:"Hello World!"});
});
});
it('sends unaltered message if a changed multi-level message property does not exist', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"change","property":"foo.bar","from":"Hello","to":"Goodbye","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.payload.should.equal("Hello World!");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:"Hello World!"});
});
});
it('changes the value of the message property based on a regex', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"change","property":"payload","from":"\\d+","to":"NUMBER","reg":true,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.payload.should.equal("Replace all numbers NUMBER and NUMBER");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:"Replace all numbers 12 and 14"});
});
});
it('supports regex groups', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"change","property":"payload","from":"(Hello)","to":"$1-$1-$1","reg":true,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.payload.should.equal("Hello-Hello-Hello World");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:"Hello World"});
});
});
it('Reports invalid regex', function(done) {
var sinon = require('sinon');
var flow = [{"id":"changeNode1","type":"change","action":"change","property":"payload","from":"\\+**+","to":"NUMBER","reg":true,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
sinon.stub(changeNode1, 'error', function(error) {
if(error.indexOf("regular expression" > -1)) {
done();
} else {
try { try {
should.fail(null, null, "An error should be reported for an invalid regex"); msg.payload.should.equal("changed");
} catch (err) { done();
} catch(err) {
done(err); done(err);
} }
} });
}); changeNode1.receive({payload:"changeMe"});
changeNode1.receive({payload:"This is irrelevant"}); });
});
it('sets the value of an already set multi-level message property', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"foo.bar","from":"","to":"bar","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.foo.bar.should.equal("bar");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({foo:{bar:"foo"}});
});
});
it('sets the value of an empty multi-level message property', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"foo.bar","from":"","to":"bar","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.foo.bar.should.equal("bar");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({});
});
});
it('sets the value of a message property to another message property', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"foo","from":"","to":"msg.fred","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.foo.should.equal("bar");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({fred:"bar"});
});
});
it('sets the value of a multi-level message property to another multi-level message property', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"foo.bar","from":"","to":"msg.fred.red","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.foo.bar.should.equal("bar");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({fred:{red:"bar"}});
});
});
it('doesn\'t set the value of a message property when the \'to\' message property does not exist', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"foo.bar","from":"","to":"msg.fred.red","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
should.not.exist(msg.foo);
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({});
});
});
it('overrides the value of a message property when the \'to\' message property does not exist', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"payload","from":"","to":"msg.foo","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
should.not.exist(msg.payload);
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:"Hello"});
});
});
it('sets the message property to null when the \'to\' message property equals null', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"payload","from":"","to":"msg.foo","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
(msg.payload === null).should.be.true;
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:"Hello", foo:null});
});
});
it('does not set other properties using = inside to property', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"payload","from":"","to":"msg.otherProp=10","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
should.not.exist(msg.payload);
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:"changeMe"});
});
});
it('splits dot delimited properties into objects', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"pay.load","from":"","to":"10","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.pay.load.should.equal("10");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({pay:{load:"changeMe"}});
});
}); });
}); });
it('deletes the value of the message property', function(done) { describe('#change', function() {
var flow = [{"id":"changeNode1","type":"change","action":"delete","property":"payload","from":"","to":"","reg":false,"name":"changeNode","wires":[["helperNode1"]]}, it('changes the value of the message property', function(done) {
{id:"helperNode1", type:"helper", wires:[]}]; var flow = [{"id":"changeNode1","type":"change","action":"change","property":"payload","from":"Hello","to":"Goodbye","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
helper.load(changeNode, flow, function() { {id:"helperNode1", type:"helper", wires:[]}];
var changeNode1 = helper.getNode("changeNode1"); helper.load(changeNode, flow, function() {
var helperNode1 = helper.getNode("helperNode1"); var changeNode1 = helper.getNode("changeNode1");
helperNode1.on("input", function(msg) { var helperNode1 = helper.getNode("helperNode1");
try { helperNode1.on("input", function(msg) {
msg.should.not.have.property('payload'); try {
done(); msg.payload.should.equal("Goodbye World!");
} catch(err) { done();
done(err); } catch(err) {
} done(err);
}
});
changeNode1.receive({payload:"Hello World!"});
});
});
it('changes the value of a multi-level message property', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"change","property":"foo.bar","from":"Hello","to":"Goodbye","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.foo.bar.should.equal("Goodbye World!");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({foo:{bar:"Hello World!"}});
});
});
it('sends unaltered message if the changed message property does not exist', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"change","property":"foo","from":"Hello","to":"Goodbye","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.payload.should.equal("Hello World!");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:"Hello World!"});
});
});
it('sends unaltered message if a changed multi-level message property does not exist', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"change","property":"foo.bar","from":"Hello","to":"Goodbye","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.payload.should.equal("Hello World!");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:"Hello World!"});
});
});
it('changes the value of the message property based on a regex', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"change","property":"payload","from":"\\d+","to":"NUMBER","reg":true,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.payload.should.equal("Replace all numbers NUMBER and NUMBER");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:"Replace all numbers 12 and 14"});
});
});
it('supports regex groups', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"change","property":"payload","from":"(Hello)","to":"$1-$1-$1","reg":true,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.payload.should.equal("Hello-Hello-Hello World");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:"Hello World"});
});
});
it('Reports invalid regex', function(done) {
var sinon = require('sinon');
var flow = [{"id":"changeNode1","type":"change","action":"change","property":"payload","from":"\\+**+","to":"NUMBER","reg":true,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
sinon.stub(changeNode1, 'error', function(error) {
if(error.indexOf("regular expression" > -1)) {
done();
} else {
try {
should.fail(null, null, "An error should be reported for an invalid regex");
} catch (err) {
done(err);
}
}
});
changeNode1.receive({payload:"This is irrelevant"});
}); });
changeNode1.receive({payload:"This won't get through!"});
}); });
}); });
it('deletes the value of a multi-level message property', function(done) { describe("#delete", function() {
var flow = [{"id":"changeNode1","type":"change","action":"delete","property":"foo.bar","from":"","to":"","reg":false,"name":"changeNode","wires":[["helperNode1"]]}, it('deletes the value of the message property', function(done) {
{id:"helperNode1", type:"helper", wires:[]}]; var flow = [{"id":"changeNode1","type":"change","action":"delete","property":"payload","from":"","to":"","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
helper.load(changeNode, flow, function() { {id:"helperNode1", type:"helper", wires:[]}];
var changeNode1 = helper.getNode("changeNode1"); helper.load(changeNode, flow, function() {
var helperNode1 = helper.getNode("helperNode1"); var changeNode1 = helper.getNode("changeNode1");
helperNode1.on("input", function(msg) { var helperNode1 = helper.getNode("helperNode1");
try { helperNode1.on("input", function(msg) {
msg.should.not.have.property('foo.bar'); try {
done(); msg.should.not.have.property('payload');
} catch(err) { done();
done(err); } catch(err) {
} done(err);
}
});
changeNode1.receive({payload:"This won't get through!"});
}); });
changeNode1.receive({payload:"This won't get through!", foo:{bar:"This will be deleted!"}});
}); });
});
it('sends unaltered message if the deleted message property does not exist', function(done) { it('deletes the value of a multi-level message property', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"delete","property":"foo","from":"","to":"","reg":false,"name":"changeNode","wires":[["helperNode1"]]}, var flow = [{"id":"changeNode1","type":"change","action":"delete","property":"foo.bar","from":"","to":"","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() { helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1"); var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1"); var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) { helperNode1.on("input", function(msg) {
try { try {
msg.should.not.have.property('foo'); msg.should.not.have.property('foo.bar');
msg.payload.should.equal('payload'); done();
done(); } catch(err) {
} catch(err) { done(err);
done(err); }
} });
changeNode1.receive({payload:"This won't get through!", foo:{bar:"This will be deleted!"}});
}); });
changeNode1.receive({payload:"payload"});
}); });
});
it('sends unaltered message if a deleted multi-level message property does not exist', function(done) { it('sends unaltered message if the deleted message property does not exist', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"delete","property":"foo.bar","from":"","to":"","reg":false,"name":"changeNode","wires":[["helperNode1"]]}, var flow = [{"id":"changeNode1","type":"change","action":"delete","property":"foo","from":"","to":"","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() { helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1"); var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1"); var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) { helperNode1.on("input", function(msg) {
try { try {
msg.should.not.have.property('foo.bar'); msg.should.not.have.property('foo');
msg.payload.should.equal('payload'); msg.payload.should.equal('payload');
done(); done();
} catch(err) { } catch(err) {
done(err); done(err);
} }
});
changeNode1.receive({payload:"payload"});
}); });
changeNode1.receive({payload:"payload"});
}); });
});
it('does not change other properties', function(done) { it('sends unaltered message if a deleted multi-level message property does not exist', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"payload","from":"","to":"msg.otherProp=10","reg":false,"name":"changeNode","wires":[["helperNode1"]]}, var flow = [{"id":"changeNode1","type":"change","action":"delete","property":"foo.bar","from":"","to":"","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() { helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1"); var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1"); var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) { helperNode1.on("input", function(msg) {
try { try {
msg.payload.should.not.equal(10); msg.should.not.have.property('foo.bar');
done(); msg.payload.should.equal('payload');
} catch(err) { done();
done(err); } catch(err) {
} done(err);
}); }
changeNode1.receive({payload:"changeMe"}); });
}); changeNode1.receive({payload:"payload"});
});
it('splits dot delimited properties into objects', function(done) {
var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"pay.load","from":"","to":"10","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
helperNode1.on("input", function(msg) {
try {
msg.pay.load.should.equal("10");
done();
} catch(err) {
done(err);
}
}); });
changeNode1.receive({pay:{load:"changeMe"}});
}); });
}); });
}); });