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

Let change node set type if total match

remove unnecessary 2 step move when not required.
add test for moving sub property up to main property
This commit is contained in:
Dave Conway-Jones 2016-10-10 11:10:36 +01:00
parent 3a8820397b
commit 4d19f881e9
3 changed files with 34 additions and 12 deletions

View File

@ -35,9 +35,11 @@
<li><b>Set</b> - set a property. The value can be a variety of different types, or
can be taken from an existing message or context property.</li>
<li><b>Change</b> - search &amp; replace parts of the property. If regular expressions
are enabled, the <b>replace with</b> property can include capture groups, for example <code>$1</code></li>
are enabled, the <b>replace with</b> property can include capture groups, for
example <code>$1</code>. Replace will only change the <b>type</b> if there
is a complete match.</li>
<li><b>Delete</b> - delete a property.</li>
<li><b>Move</b> - move or rename a property. Equivalent to set and delete in one operation.
<li><b>Move</b> - move or rename a property.
</ul>
</script>

View File

@ -152,7 +152,7 @@ module.exports = function(RED) {
} else if (rule.t === 'change') {
current = RED.util.getMessageProperty(msg,property);
if (typeof current === 'string') {
if ((fromType === 'num' || fromType === 'bool') && current === fromValue) {
if ((fromType === 'num' || fromType === 'bool' || fromType === 'str') && current === fromValue) {
// str representation of exact from number/boolean
// only replace if they match exactly
RED.util.setMessageProperty(msg,property,value);
@ -170,7 +170,8 @@ module.exports = function(RED) {
}
}
}
} else {
}
else {
var target;
if (rule.pt === 'flow') {
target = node.context().flow;
@ -185,7 +186,7 @@ module.exports = function(RED) {
} else if (rule.t === 'change') {
current = target.get(msg,property);
if (typeof current === 'string') {
if ((fromType === 'num' || fromType === 'bool') && current === fromValue) {
if ((fromType === 'num' || fromType === 'bool' || fromType === 'str') && current === fromValue) {
// str representation of exact from number/boolean
// only replace if they match exactly
target.set(property,value);
@ -214,11 +215,11 @@ module.exports = function(RED) {
for (var i=0; i<this.rules.length; i++) {
if (this.rules[i].t === "move") {
var r = this.rules[i];
if (r.to.indexOf(r.pt) !== -1) {
if ((r.tot !== r.pt) || (r.p.indexOf(r.to) !== -1)) {
msg = applyRule(msg,{t:"set", p:r.to, pt:r.tot, to:r.p, tot:r.pt});
applyRule(msg,{t:"delete", p:r.p, pt:r.pt});
}
else { // 2 step move if we are moving to a child
else { // 2 step move if we are moving from a child
msg = applyRule(msg,{t:"set", p:"_temp_move", pt:r.tot, to:r.p, tot:r.pt});
applyRule(msg,{t:"delete", p:r.p, pt:r.pt});
msg = applyRule(msg,{t:"set", p:r.to, pt:r.tot, to:"_temp_move", tot:r.pt});

View File

@ -689,6 +689,25 @@ describe('change Node', function() {
changeNode1.receive({payload:"bar"});
});
});
it('moves the value of a message sub-property object to a property', function(done) {
var flow = [{"id":"changeNode1","type":"change","rules":[{"t":"move","p":"payload.foo","pt":"msg","to":"payload","tot":"msg"}],"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.have.property('payload');
msg.payload.should.equal("bar");
(typeof msg.payload).should.equal("string");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:{foo:"bar"}});
});
});
});
describe('- multiple rules', function() {