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
pull/946/merge
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 <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 &amp; replace parts of the property. If regular expressions <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>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>
@ -135,7 +137,7 @@
var selectField = $('<select/>',{class:"node-input-rule-type",style:"width:110px; margin-right:10px;"}).appendTo(row1); var selectField = $('<select/>',{class:"node-input-rule-type",style:"width:110px; margin-right:10px;"}).appendTo(row1);
var selectOptions = [{v:"set",l:set},{v:"change",l:change},{v:"delete",l:del},{v:"move",l:move}]; var selectOptions = [{v:"set",l:set},{v:"change",l:change},{v:"delete",l:del},{v:"move",l:move}];
for (var i=0;i<4;i++) { for (var i=0; i<4; i++) {
selectField.append($("<option></option>").val(selectOptions[i].v).text(selectOptions[i].l)); selectField.append($("<option></option>").val(selectOptions[i].v).text(selectOptions[i].l));
} }
@ -241,7 +243,7 @@
this.rules = [rule]; this.rules = [rule];
} }
for (var i=0;i<this.rules.length;i++) { for (var i=0; i<this.rules.length; i++) {
var rule = this.rules[i]; var rule = this.rules[i];
$("#node-input-rule-container").editableList('addItem',rule); $("#node-input-rule-container").editableList('addItem',rule);
} }
@ -277,7 +279,7 @@
oneditresize: function(size) { oneditresize: function(size) {
var rows = $("#dialog-form>div:not(.node-input-rule-container-row)"); var rows = $("#dialog-form>div:not(.node-input-rule-container-row)");
var height = size.height; var height = size.height;
for (var i=0;i<rows.size();i++) { for (var i=0; i<rows.size(); i++) {
height -= $(rows[i]).outerHeight(true); height -= $(rows[i]).outerHeight(true);
} }
var editorRow = $("#dialog-form>div.node-input-rule-container-row"); var editorRow = $("#dialog-form>div.node-input-rule-container-row");

View File

@ -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);
@ -211,14 +212,14 @@ module.exports = function(RED) {
if (valid) { if (valid) {
var node = this; var node = this;
this.on('input', function(msg) { this.on('input', function(msg) {
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});

View File

@ -47,7 +47,7 @@ describe('change Node', function() {
}); });
}); });
it('should load defaults if set to change', function(done) { it('should load defaults if set to change', function(done) {
var flow = [{ id: "c1", type: "change", name:"change1", action:"change" }]; var flow = [{ id: "c1", type: "change", name:"change1", action:"change" }];
helper.load(changeNode, flow, function() { helper.load(changeNode, flow, function() {
//console.log(helper.getNode("c1")); //console.log(helper.getNode("c1"));
helper.getNode("c1").should.have.property("name", "change1"); helper.getNode("c1").should.have.property("name", "change1");
@ -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() {