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:
parent
3a8820397b
commit
4d19f881e9
@ -35,9 +35,11 @@
|
|||||||
<li><b>Set</b> - set a property. The value can be a variety of different types, or
|
<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>
|
can be taken from an existing message or context property.</li>
|
||||||
<li><b>Change</b> - search & replace parts of the property. If regular expressions
|
<li><b>Change</b> - search & 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>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>
|
</ul>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@ -152,7 +152,7 @@ module.exports = function(RED) {
|
|||||||
} else if (rule.t === 'change') {
|
} else if (rule.t === 'change') {
|
||||||
current = RED.util.getMessageProperty(msg,property);
|
current = RED.util.getMessageProperty(msg,property);
|
||||||
if (typeof current === 'string') {
|
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
|
// str representation of exact from number/boolean
|
||||||
// only replace if they match exactly
|
// only replace if they match exactly
|
||||||
RED.util.setMessageProperty(msg,property,value);
|
RED.util.setMessageProperty(msg,property,value);
|
||||||
@ -170,7 +170,8 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
var target;
|
var target;
|
||||||
if (rule.pt === 'flow') {
|
if (rule.pt === 'flow') {
|
||||||
target = node.context().flow;
|
target = node.context().flow;
|
||||||
@ -185,7 +186,7 @@ module.exports = function(RED) {
|
|||||||
} else if (rule.t === 'change') {
|
} else if (rule.t === 'change') {
|
||||||
current = target.get(msg,property);
|
current = target.get(msg,property);
|
||||||
if (typeof current === 'string') {
|
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
|
// str representation of exact from number/boolean
|
||||||
// only replace if they match exactly
|
// only replace if they match exactly
|
||||||
target.set(property,value);
|
target.set(property,value);
|
||||||
@ -214,11 +215,11 @@ module.exports = function(RED) {
|
|||||||
for (var i=0; i<this.rules.length; i++) {
|
for (var i=0; i<this.rules.length; i++) {
|
||||||
if (this.rules[i].t === "move") {
|
if (this.rules[i].t === "move") {
|
||||||
var r = this.rules[i];
|
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});
|
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});
|
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});
|
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});
|
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});
|
msg = applyRule(msg,{t:"set", p:r.to, pt:r.tot, to:"_temp_move", tot:r.pt});
|
||||||
|
@ -689,6 +689,25 @@ describe('change Node', function() {
|
|||||||
changeNode1.receive({payload:"bar"});
|
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() {
|
describe('- multiple rules', function() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user