mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Merge pull request #433 from anna2130/change-node-multi-level-properties
Change node: Multi-level properties
This commit is contained in:
commit
bec4e429f9
@ -36,7 +36,7 @@
|
||||
<div class="form-row" id="node-reg-row">
|
||||
<label> </label>
|
||||
<input type="checkbox" id="node-input-reg" style="display: inline-block; width: auto; vertical-align: top;">
|
||||
<label for="node-input-reg" style="width: 70%;">Use regular expressions ?</label>
|
||||
<label for="node-input-reg" style="width: 70%;">Use regular expressions</label>
|
||||
</div>
|
||||
<div class="form-tips" id="node-tip"></div>
|
||||
<br/>
|
||||
@ -47,10 +47,10 @@
|
||||
</script>
|
||||
|
||||
<script type="text/x-red" data-help-name="change">
|
||||
<p>A simple function node to change, replace, add or delete properties of a message.</p>
|
||||
<p>A simple function node to set, replace or delete properties of a message.</p>
|
||||
<p>When a message arrives, the selected property is modified by the defined rules.
|
||||
The message is then sent to the output.</p>
|
||||
<p><b>Note:</b> Replace only operates on <b>strings</b>. Anything else will be passed straight through.</p>
|
||||
<p><b>Note:</b> Set and replace only operate using <b>strings</b>. Anything else will be passed straight through.</p>
|
||||
</script>
|
||||
|
||||
<script type="text/javascript">
|
||||
@ -82,8 +82,10 @@
|
||||
if (this.name) {
|
||||
return this.name;
|
||||
}
|
||||
if (this.action == "replace") {
|
||||
if (this.action === "replace") {
|
||||
return "set msg."+this.property;
|
||||
} else if (this.action === "change") {
|
||||
return "replace msg."+this.property;
|
||||
} else {
|
||||
return this.action+" msg."+this.property
|
||||
}
|
||||
|
@ -20,55 +20,49 @@ module.exports = function(RED) {
|
||||
RED.nodes.createNode(this, n);
|
||||
this.action = n.action;
|
||||
this.property = n.property || "";
|
||||
this.from = n.from || " ";
|
||||
this.to = n.to || " ";
|
||||
this.from = n.from || "";
|
||||
this.to = n.to || "";
|
||||
this.reg = (n.reg === null || n.reg);
|
||||
var node = this;
|
||||
if (node.reg === false) {
|
||||
this.from = this.from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
||||
}
|
||||
var makeNew = function( stem, path, value ) {
|
||||
var lastPart = (arguments.length === 3) ? path.pop() : false;
|
||||
for (var i = 0; i < path.length; i++) {
|
||||
stem = stem[path[i]] = stem[path[i]] || {};
|
||||
}
|
||||
if (lastPart) { stem = stem[lastPart] = value; }
|
||||
return stem;
|
||||
};
|
||||
|
||||
this.on('input', function (msg) {
|
||||
if (node.action == "change") {
|
||||
var propertyParts;
|
||||
var depth = 0;
|
||||
|
||||
if (node.action === "change") {
|
||||
try {
|
||||
node.re = new RegExp(this.from, "g");
|
||||
} catch (e) {
|
||||
node.error(e.message);
|
||||
}
|
||||
if (typeof msg[node.property] === "string") {
|
||||
msg[node.property] = (msg[node.property]).replace(node.re, node.to);
|
||||
}
|
||||
}
|
||||
//else if (node.action == "replace") {
|
||||
//if (node.to.indexOf("msg.") == 0) {
|
||||
//msg[node.property] = eval(node.to);
|
||||
//}
|
||||
//else {
|
||||
//msg[node.property] = node.to;
|
||||
//}
|
||||
//}
|
||||
else if (node.action == "replace") {
|
||||
if (node.to.indexOf("msg.") === 0) {
|
||||
makeNew( msg, node.property.split("."), eval(node.to) );
|
||||
}
|
||||
else {
|
||||
makeNew( msg, node.property.split("."), node.to );
|
||||
}
|
||||
//makeNew( msg, node.property.split("."), node.to );
|
||||
}
|
||||
else if (node.action == "delete") {
|
||||
delete(msg[node.property]);
|
||||
}
|
||||
|
||||
propertyParts = node.property.split(".");
|
||||
try {
|
||||
propertyParts.reduce(function (obj, i) {
|
||||
if (++depth === propertyParts.length) {
|
||||
if (node.action === "change") {
|
||||
if (typeof obj[i] === "string") {
|
||||
obj[i] = obj[i].replace(node.re, node.to);
|
||||
}
|
||||
} else if (node.action === "replace") {
|
||||
obj[i] = node.to;
|
||||
} else if (node.action === "delete") {
|
||||
delete(obj[i]);
|
||||
}
|
||||
} else {
|
||||
if (!obj[i]) {
|
||||
obj[i] = {};
|
||||
}
|
||||
return obj[i];
|
||||
}
|
||||
}, msg);
|
||||
} catch (err) {}
|
||||
node.send(msg);
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("change", ChangeNode);
|
||||
}
|
||||
};
|
||||
|
@ -57,6 +57,42 @@ describe('ChangeNode', function() {
|
||||
});
|
||||
});
|
||||
|
||||
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('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:[]}];
|
||||
@ -75,6 +111,60 @@ describe('ChangeNode', function() {
|
||||
});
|
||||
});
|
||||
|
||||
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:[]}];
|
||||
@ -112,9 +202,7 @@ describe('ChangeNode', function() {
|
||||
});
|
||||
|
||||
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() {
|
||||
@ -154,24 +242,79 @@ describe('ChangeNode', function() {
|
||||
});
|
||||
});
|
||||
|
||||
// TODO confirm the behaviour of the change node later,apparently calling eval such that makeNew( msg, node.property.split("."), eval(node.to) ); is incorrect
|
||||
// it('changes the property name of the message object', 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 {
|
||||
// msg.otherProp.should.equal(10);
|
||||
// done();
|
||||
// } catch(err) {
|
||||
// done(err);
|
||||
// }
|
||||
// });
|
||||
// changeNode1.receive({payload:"changeMe"});
|
||||
// });
|
||||
// });
|
||||
it('deletes the value of a multi-level message property', function(done) {
|
||||
var flow = [{"id":"changeNode1","type":"change","action":"delete","property":"foo.bar","from":"","to":"","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.should.not.have.property('foo.bar');
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
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) {
|
||||
var flow = [{"id":"changeNode1","type":"change","action":"delete","property":"foo","from":"","to":"","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.should.not.have.property('foo');
|
||||
msg.payload.should.equal('payload');
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
changeNode1.receive({payload:"payload"});
|
||||
});
|
||||
});
|
||||
|
||||
it('sends unaltered message if a deleted multi-level 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"]]},
|
||||
{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.should.not.have.property('foo.bar');
|
||||
msg.payload.should.equal('payload');
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
changeNode1.receive({payload:"payload"});
|
||||
});
|
||||
});
|
||||
|
||||
it('does not change other properties', 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 {
|
||||
msg.payload.should.not.equal(10);
|
||||
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"]]},
|
||||
@ -187,7 +330,7 @@ describe('ChangeNode', function() {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
changeNode1.receive({"pay.load":"changeMe"});
|
||||
changeNode1.receive({pay:{load:"changeMe"}});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user