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

Add propertySelect support to Change node

This commit is contained in:
Nick O'Leary 2015-12-29 22:19:32 +00:00
parent 742c470d81
commit 1f848b205b
8 changed files with 330 additions and 108 deletions

View File

@ -459,7 +459,8 @@
"replace": "Replace with" "replace": "Replace with"
}, },
"errors": { "errors": {
"invalid-from": "Invalid 'from' property: __error__" "invalid-from": "Invalid 'from' property: __error__",
"invalid-json": "Invalid 'to' JSON property"
} }
}, },
"range": { "range": {

View File

@ -51,7 +51,7 @@
category: 'function', category: 'function',
defaults: { defaults: {
name: {value:""}, name: {value:""},
rules:{value:[{t:"set",p:"payload",to:""}]}, rules:{value:[{t:"set",p:"payload",pt:"msg",to:"",tot:"str"}]},
// legacy // legacy
action: {value:""}, action: {value:""},
property: {value:""}, property: {value:""},
@ -77,11 +77,11 @@
} else { } else {
if (this.rules.length == 1) { if (this.rules.length == 1) {
if (this.rules[0].t === "set") { if (this.rules[0].t === "set") {
return this._("change.label.set",{property:"msg."+this.rules[0].p}); return this._("change.label.set",{property:(this.rules[0].pt||"msg")+"."+this.rules[0].p});
} else if (this.rules[0].t === "change") { } else if (this.rules[0].t === "change") {
return this._("change.label.change",{property:"msg."+this.rules[0].p}); return this._("change.label.change",{property:(this.rules[0].pt||"msg")+"."+this.rules[0].p});
} else { } else {
return this._("change.label.delete",{property:"msg."+this.rules[0].p}); return this._("change.label.delete",{property:(this.rules[0].pt||"msg")+"."+this.rules[0].p});
} }
} else { } else {
return this._("change.label.changeCount",{count:this.rules.length}); return this._("change.label.changeCount",{count:this.rules.length});
@ -99,14 +99,27 @@
var search = this._("change.action.search"); var search = this._("change.action.search");
var replace = this._("change.action.replace"); var replace = this._("change.action.replace");
var regex = this._("change.label.regex"); var regex = this._("change.label.regex");
if (this.reg === null) { $("#node-input-reg").prop('checked', true); }
$("#node-input-action").change();
function resizeRule(rule,width) { function resizeRule(rule,width) {
rule.find('input[type="text"]').width(width-220); rule.find('input[type="text"]:not(.red-ui-propertySelect)').width(width-180);
rule.find('.red-ui-propertySelect').propertySelect("width",width-180);
} }
function generateRule(rule) { function generateRule(rule) {
if (rule.t === "change" && rule.re) {
rule.fromt = 're';
delete rule.re;
}
if (rule.t === "set" && !rule.tot) {
if (rule.to.indexOf("msg.") === 0 && !rule.tot) {
rule.to = rule.to.substring(4);
rule.tot = "msg";
} else {
rule.tot = "str";
}
}
var container = $('<li/>',{style:"background: #fff; margin:0; padding:8px 0px; border-bottom: 1px solid #ccc;"}); var container = $('<li/>',{style:"background: #fff; margin:0; padding:8px 0px; border-bottom: 1px solid #ccc;"});
var row1 = $('<div/>').appendTo(container); var row1 = $('<div/>').appendTo(container);
@ -114,37 +127,28 @@
var row2 = $('<div/>',{style:"margin-top:8px;"}).appendTo(container); var row2 = $('<div/>',{style:"margin-top:8px;"}).appendTo(container);
var row3 = $('<div/>',{style:"margin-top:8px;"}).appendTo(container); var row3 = $('<div/>',{style:"margin-top:8px;"}).appendTo(container);
var selectField = $('<select/>',{class:"node-input-rule-type",style:"width: 100px"}).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}]; var selectOptions = [{v:"set",l:set},{v:"change",l:change},{v:"delete",l:del}];
for (var i=0;i<3;i++) { for (var i=0;i<3;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));
} }
$('<div/>',{style:"display:inline-block; width: 50px; text-align: right;"}).text("msg.").appendTo(row1); var propertyName = $('<input/>',{style:"width: 250px",class:"node-input-rule-property-name",type:"text"}).appendTo(row1).propertySelect({options:['msg','flow','global']});
var propertyName = $('<input/>',{style:"width: 220px",class:"node-input-rule-property-name",type:"text"}).appendTo(row1);
var finalspan = $('<span/>',{style:"float: right; margin-right: 10px;"}).appendTo(row1); var finalspan = $('<span/>',{style:"float: right; margin-right: 10px;"}).appendTo(row1);
var deleteButton = $('<a/>',{href:"#",class:"editor-button editor-button-small", style:"margin-top: 7px; margin-left: 5px;"}).appendTo(finalspan); var deleteButton = $('<a/>',{href:"#",class:"editor-button editor-button-small", style:"margin-top: 7px; margin-left: 5px;"}).appendTo(finalspan);
$('<i/>',{class:"fa fa-remove"}).appendTo(deleteButton); $('<i/>',{class:"fa fa-remove"}).appendTo(deleteButton);
$('<div/>',{style:"display: inline-block;text-align:right; width:150px;padding-right: 10px; box-sizing: border-box;"}).text(to).appendTo(row2); $('<div/>',{style:"display: inline-block;text-align:right; width:120px;padding-right: 10px; box-sizing: border-box;"}).text(to).appendTo(row2);
var propertyValue = $('<input/>',{style:"width: 220px",class:"node-input-rule-property-value",type:"text"}).appendTo(row2); var propertyValue = $('<input/>',{style:"width: 250px",class:"node-input-rule-property-value",type:"text"}).appendTo(row2).propertySelect({default:'str',options:['msg','flow','global','str','num','json']});
var row3_1 = $('<div/>').appendTo(row3); var row3_1 = $('<div/>').appendTo(row3);
$('<div/>',{style:"display: inline-block;text-align:right; width:150px;padding-right: 10px; box-sizing: border-box;"}).text(search).appendTo(row3_1); $('<div/>',{style:"display: inline-block;text-align:right; width:120px;padding-right: 10px; box-sizing: border-box;"}).text(search).appendTo(row3_1);
var fromValue = $('<input/>',{style:"width: 220px",class:"node-input-rule-property-search-value",type:"text"}).appendTo(row3_1); var fromValue = $('<input/>',{style:"width: 250px",class:"node-input-rule-property-search-value",type:"text"}).appendTo(row3_1).propertySelect({default:'str',options:['msg','flow','global','str','re']});
var row3_2 = $('<div/>',{style:"margin-top:8px;"}).appendTo(row3); var row3_2 = $('<div/>',{style:"margin-top:8px;"}).appendTo(row3);
$('<div/>',{style:"display: inline-block;text-align:right; width:150px;padding-right: 10px; box-sizing: border-box;"}).text(replace).appendTo(row3_2); $('<div/>',{style:"display: inline-block;text-align:right; width:120px;padding-right: 10px; box-sizing: border-box;"}).text(replace).appendTo(row3_2);
var toValue = $('<input/>',{style:"width: 220px",class:"node-input-rule-property-replace-value",type:"text"}).appendTo(row3_2); var toValue = $('<input/>',{style:"width: 250px",class:"node-input-rule-property-replace-value",type:"text"}).appendTo(row3_2).propertySelect({default:'str',options:['msg','flow','global','str','num','json']});
var row3_3 = $('<div/>',{style:"margin-top:8px;"}).appendTo(row3);
var id = "node-input-rule-property-regex-"+Math.floor(Math.random()*10000);
var useRegExp = $('<input/>',{id:id,class:"node-input-rule-property-re",type:"checkbox", style:"margin-left: 150px; margin-right: 10px; display: inline-block; width: auto; vertical-align: top;"}).appendTo(row3_3);
$('<label/>',{for:id,style:"width: auto;"}).text(regex).appendTo(row3_3);
selectField.change(function() { selectField.change(function() {
var width = $("#node-input-rule-container").width(); var width = $("#node-input-rule-container").width();
@ -170,10 +174,13 @@
selectField.find("option").filter(function() {return $(this).val() == rule.t;}).attr('selected',true); selectField.find("option").filter(function() {return $(this).val() == rule.t;}).attr('selected',true);
propertyName.val(rule.p); propertyName.val(rule.p);
propertyName.propertySelect('type',rule.pt)
propertyValue.val(rule.to); propertyValue.val(rule.to);
propertyValue.propertySelect('type',rule.tot)
fromValue.val(rule.from); fromValue.val(rule.from);
fromValue.propertySelect('type',rule.fromt)
toValue.val(rule.to); toValue.val(rule.to);
useRegExp.prop('checked', rule.re); toValue.propertySelect('type',rule.tot)
selectField.change(); selectField.change();
$("#node-input-rule-container").append(container); $("#node-input-rule-container").append(container);
@ -185,7 +192,8 @@
if (!this.rules) { if (!this.rules) {
var rule = { var rule = {
t:(this.action=="replace"?"set":this.action), t:(this.action=="replace"?"set":this.action),
p:this.property p:this.property,
pt:"msg"
} }
if (rule.t === "set") { if (rule.t === "set") {
@ -250,13 +258,17 @@
var type = rule.find(".node-input-rule-type option:selected").val(); var type = rule.find(".node-input-rule-type option:selected").val();
var r = { var r = {
t:type, t:type,
p:rule.find(".node-input-rule-property-name").val() p:rule.find(".node-input-rule-property-name").val(),
pt:rule.find(".node-input-rule-property-name").propertySelect('type')
}; };
if (type === "set") { if (type === "set") {
r.to = rule.find(".node-input-rule-property-value").val(); r.to = rule.find(".node-input-rule-property-value").val();
r.tot = rule.find(".node-input-rule-property-value").propertySelect('type');
} else if (type === "change") { } else if (type === "change") {
r.from = rule.find(".node-input-rule-property-search-value").val(); r.from = rule.find(".node-input-rule-property-search-value").val();
r.fromt = rule.find(".node-input-rule-property-search-value").propertySelect('type');
r.to = rule.find(".node-input-rule-property-replace-value").val(); r.to = rule.find(".node-input-rule-property-replace-value").val();
r.tot = rule.find(".node-input-rule-property-replace-value").propertySelect('type');
r.re = rule.find(".node-input-rule-property-re").prop('checked'); r.re = rule.find(".node-input-rule-property-re").prop('checked');
} }
node.rules.push(r); node.rules.push(r);

View File

@ -44,8 +44,28 @@ module.exports = function(RED) {
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];
// Migrate to type-aware rules
if (!rule.pt) {
rule.pt = "msg";
}
if (rule.t === "change" && rule.re) {
rule.fromt = 're';
delete rule.re;
}
if (rule.t === "set" && !rule.tot) {
if (rule.to.indexOf("msg.") === 0 && !rule.tot) {
rule.to = rule.to.substring(4);
rule.tot = "msg";
}
}
if (!rule.tot) {
rule.tot = "str";
}
if (!rule.fromt) {
rule.fromt = "str";
}
if (rule.t === "change") { if (rule.t === "change") {
if (rule.re === false) { if (rule.fromt !== 're') {
rule.from = rule.from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); rule.from = rule.from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
} }
try { try {
@ -55,57 +75,66 @@ module.exports = function(RED) {
this.error(RED._("change.errors.invalid-from",{error:e.message})); this.error(RED._("change.errors.invalid-from",{error:e.message}));
} }
} }
if (rule.tot === 'num') {
rule.to = Number(rule.to);
} else if (rule.tot === 'json') {
try {
rule.to = JSON.parse(rule.to);
} catch(e2) {
valid = false;
this.error(RED._("change.errors.invalid-json"));
}
}
} }
function applyRule(msg,rule) { function applyRule(msg,rule) {
var propertyParts;
var depth = 0;
propertyParts = rule.p.split(".");
try { try {
propertyParts.reduce(function(obj, i) { var property = rule.p;
var to = rule.to; var value = rule.to;
// Set msg from property to another msg property if (rule.tot === "msg") {
if (rule.t === "set" && rule.to.indexOf("msg.") === 0) { value = RED.util.getMessageProperty(msg,rule.to);
var parts = to.substring(4); } else if (rule.tot === 'flow') {
var msgPropParts = parts.split("."); value = node.context().flow.get(rule.to);
try { } else if (rule.tot === 'global') {
msgPropParts.reduce(function(ob, j) { value = node.context().global.get(rule.to);
to = (typeof ob[j] !== "undefined" ? ob[j] : undefined); }
return to; if (rule.pt === 'msg') {
}, msg); if (rule.t === 'delete') {
} catch (err) {} RED.util.setMessageProperty(msg,property,undefined);
} else if (rule.t === 'set') {
RED.util.setMessageProperty(msg,property,value);
} else if (rule.t === 'change') {
var current = RED.util.getMessageProperty(msg,property);
if (typeof current === 'string') {
current = current.replace(rule.from,value);
RED.util.setMessageProperty(msg,property,current);
}
}
} else {
var target;
if (rule.pt === 'flow') {
target = node.context().flow;
} else if (rule.pt === 'global') {
target = node.context().global;
}
if (target) {
if (rule.t === 'delete') {
target.set(property,undefined);
} else if (rule.t === 'set') {
target.set(property,value);
} else if (rule.t === 'change') {
var current = target.get(msg,property);
if (typeof current === 'string') {
current = current.replace(rule.from,value);
target.set(property,current);
}
}
} }
if (++depth === propertyParts.length) { }
if (rule.t === "change") { } catch(err) {console.log(err.stack)}
if (typeof obj[i] === "string") {
obj[i] = obj[i].replace(rule.from, rule.to);
}
} else if (rule.t === "set") {
if (typeof to === "undefined") {
delete(obj[i]);
} else {
obj[i] = to;
}
} else if (rule.t === "delete") {
delete(obj[i]);
}
} else {
// 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] = {};
}
return obj[i];
}
}, msg);
} catch (err) {}
return msg; return msg;
} }
if (valid) { if (valid) {
var node = this; var node = this;
this.on('input', function(msg) { this.on('input', function(msg) {

View File

@ -42,6 +42,7 @@ var nodeApp;
var server; var server;
var errorHandler = function(err,req,res,next) { var errorHandler = function(err,req,res,next) {
console.log("HERE I AM");
if (err.message === "request entity too large") { if (err.message === "request entity too large") {
log.error(err); log.error(err);
} else { } else {
@ -87,7 +88,7 @@ function init(_server,runtime) {
adminApp.use(bodyParser.json({limit:maxApiRequestSize})); adminApp.use(bodyParser.json({limit:maxApiRequestSize}));
adminApp.use(bodyParser.urlencoded({limit:maxApiRequestSize,extended:true})); adminApp.use(bodyParser.urlencoded({limit:maxApiRequestSize,extended:true}));
adminApp.get("/auth/login",auth.login); adminApp.get("/auth/login",auth.login,errorHandler);
if (settings.adminAuth) { if (settings.adminAuth) {
//TODO: all passport references ought to be in ./auth //TODO: all passport references ought to be in ./auth
@ -98,43 +99,43 @@ function init(_server,runtime) {
auth.getToken, auth.getToken,
auth.errorHandler auth.errorHandler
); );
adminApp.post("/auth/revoke",needsPermission(""),auth.revoke); adminApp.post("/auth/revoke",needsPermission(""),auth.revoke,errorHandler);
} }
// Flows // Flows
adminApp.get("/flows",needsPermission("flows.read"),flows.get); adminApp.get("/flows",needsPermission("flows.read"),flows.get,errorHandler);
adminApp.post("/flows",needsPermission("flows.write"),flows.post); adminApp.post("/flows",needsPermission("flows.write"),flows.post,errorHandler);
adminApp.get("/flow/:id",needsPermission("flows.read"),flow.get); adminApp.get("/flow/:id",needsPermission("flows.read"),flow.get,errorHandler);
adminApp.post("/flow",needsPermission("flows.write"),flow.post); adminApp.post("/flow",needsPermission("flows.write"),flow.post,errorHandler);
adminApp.delete("/flow/:id",needsPermission("flows.write"),flow.delete); adminApp.delete("/flow/:id",needsPermission("flows.write"),flow.delete,errorHandler);
adminApp.put("/flow/:id",needsPermission("flows.write"),flow.put); adminApp.put("/flow/:id",needsPermission("flows.write"),flow.put,errorHandler);
// Nodes // Nodes
adminApp.get("/nodes",needsPermission("nodes.read"),nodes.getAll); adminApp.get("/nodes",needsPermission("nodes.read"),nodes.getAll,errorHandler);
adminApp.post("/nodes",needsPermission("nodes.write"),nodes.post); adminApp.post("/nodes",needsPermission("nodes.write"),nodes.post,errorHandler);
adminApp.get("/nodes/:mod",needsPermission("nodes.read"),nodes.getModule); adminApp.get("/nodes/:mod",needsPermission("nodes.read"),nodes.getModule,errorHandler);
adminApp.put("/nodes/:mod",needsPermission("nodes.write"),nodes.putModule); adminApp.put("/nodes/:mod",needsPermission("nodes.write"),nodes.putModule,errorHandler);
adminApp.delete("/nodes/:mod",needsPermission("nodes.write"),nodes.delete); adminApp.delete("/nodes/:mod",needsPermission("nodes.write"),nodes.delete,errorHandler);
adminApp.get("/nodes/:mod/:set",needsPermission("nodes.read"),nodes.getSet); adminApp.get("/nodes/:mod/:set",needsPermission("nodes.read"),nodes.getSet,errorHandler);
adminApp.put("/nodes/:mod/:set",needsPermission("nodes.write"),nodes.putSet); adminApp.put("/nodes/:mod/:set",needsPermission("nodes.write"),nodes.putSet,errorHandler);
adminApp.get('/credentials/:type/:id', needsPermission("credentials.read"),credentials.get); adminApp.get('/credentials/:type/:id', needsPermission("credentials.read"),credentials.get,errorHandler);
adminApp.get(/locales\/(.+)\/?$/,locales.get); adminApp.get(/locales\/(.+)\/?$/,locales.get,errorHandler);
// Library // Library
adminApp.post(new RegExp("/library/flows\/(.*)"),needsPermission("library.write"),library.post); adminApp.post(new RegExp("/library/flows\/(.*)"),needsPermission("library.write"),library.post,errorHandler);
adminApp.get("/library/flows",needsPermission("library.read"),library.getAll); adminApp.get("/library/flows",needsPermission("library.read"),library.getAll,errorHandler);
adminApp.get(new RegExp("/library/flows\/(.*)"),needsPermission("library.read"),library.get); adminApp.get(new RegExp("/library/flows\/(.*)"),needsPermission("library.read"),library.get,errorHandler);
// Settings // Settings
adminApp.get("/settings",needsPermission("settings.read"),info.settings); adminApp.get("/settings",needsPermission("settings.read"),info.settings,errorHandler);
// Error Handler // Error Handler
adminApp.use(errorHandler); //adminApp.use(errorHandler);
} }
} }
function start() { function start() {

View File

@ -103,10 +103,58 @@ function compareObjects(obj1,obj2) {
return true; return true;
} }
function getMessageProperty(msg,expr) {
var result = null;
if (expr.indexOf('msg.')===0) {
expr = expr.substring(4);
}
var msgPropParts = expr.split(".");
msgPropParts.reduce(function(obj, i) {
result = (typeof obj[i] !== "undefined" ? obj[i] : undefined);
return result;
}, msg);
return result;
}
function setMessageProperty(msg,prop,value,createMissing) {
if (typeof createMissing === 'undefined') {
createMissing = (typeof value !== 'undefined');
}
if (prop.indexOf('msg.')===0) {
prop = prop.substring(4);
}
var msgPropParts = prop.split(".");
var depth = 0;
msgPropParts.reduce(function(obj, i) {
if (obj === null) {
return null;
}
depth++;
if (depth === msgPropParts.length) {
if (typeof value === "undefined") {
delete obj[i];
} else {
obj[i] = value;
}
} else {
if (!obj[i]) {
if (createMissing) {
obj[i] = {};
} else {
return null;
}
}
return obj[i];
}
}, msg);
}
module.exports = { module.exports = {
ensureString: ensureString, ensureString: ensureString,
ensureBuffer: ensureBuffer, ensureBuffer: ensureBuffer,
cloneMessage: cloneMessage, cloneMessage: cloneMessage,
compareObjects: compareObjects, compareObjects: compareObjects,
generateId: generateId generateId: generateId,
getMessageProperty: getMessageProperty,
setMessageProperty: setMessageProperty
}; };

View File

@ -34,7 +34,7 @@ describe('change Node', function() {
var flow = [{ id: "c1", type: "change", name:"change1" }]; var flow = [{ id: "c1", type: "change", name:"change1" }];
helper.load(changeNode, flow, function() { helper.load(changeNode, flow, function() {
helper.getNode("c1").should.have.property("name", "change1"); helper.getNode("c1").should.have.property("name", "change1");
helper.getNode("c1").should.have.property("rules", [{t:undefined,p:''}]); helper.getNode("c1").should.have.property("rules", [{fromt:'str',pt:'msg',tot:'str',t:undefined,p:''}]);
done(); done();
}); });
}); });
@ -42,7 +42,7 @@ describe('change Node', function() {
var flow = [{ id: "c1", type: "change", name:"change1", action:"replace" }]; var flow = [{ id: "c1", type: "change", name:"change1", action:"replace" }];
helper.load(changeNode, flow, function() { helper.load(changeNode, flow, function() {
helper.getNode("c1").should.have.property("name", "change1"); helper.getNode("c1").should.have.property("name", "change1");
helper.getNode("c1").should.have.property("rules", [ { p: '', t: 'set', to: '' } ]); helper.getNode("c1").should.have.property("rules", [ {fromt: 'str', p: '', pt: 'msg', t: 'set', to: '', tot: 'str'} ]);
done(); done();
}); });
}); });
@ -51,12 +51,12 @@ describe('change Node', function() {
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");
helper.getNode("c1").should.have.property("rules", [ { from: /(?:)/g, p: '', re: undefined, t: 'change', to: '' } ]); helper.getNode("c1").should.have.property("rules", [ { from: /(?:)/g,fromt: 'str', p: '',pt: 'msg', re: undefined, t: 'change', to: '',tot: 'str' } ]);
done(); done();
}); });
}); });
describe('#replace' , function() { describe('#set' , function() {
it('sets the value of the message property', function(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"]]}, var flow = [{"id":"changeNode1","type":"change","action":"replace","property":"payload","from":"","to":"changed","reg":false,"name":"changeNode","wires":[["helperNode1"]]},
@ -64,6 +64,7 @@ describe('change Node', function() {
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");
var rule = helper.getNode("changeNode1").rules[0];
helperNode1.on("input", function(msg) { helperNode1.on("input", function(msg) {
try { try {
msg.payload.should.equal("changed"); msg.payload.should.equal("changed");
@ -118,6 +119,9 @@ describe('change Node', function() {
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");
var rule = helper.getNode("changeNode1").rules[0];
rule.t.should.eql('set');
rule.tot.should.eql('msg');
helperNode1.on("input", function(msg) { helperNode1.on("input", function(msg) {
try { try {
msg.foo.should.equal("bar"); msg.foo.should.equal("bar");
@ -237,6 +241,44 @@ describe('change Node', function() {
changeNode1.receive({pay:{load:"changeMe"}}); changeNode1.receive({pay:{load:"changeMe"}});
}); });
}); });
it('changes the value to a number', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":"123","tot":"num"}],"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.eql(123);
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:""});
});
});
it('changes the value to a js object', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"set","p":"payload","to":'{"a":123}',"tot":"json"}],"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.eql({a:123});
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:""});
});
});
}); });
describe('#change', function() { describe('#change', function() {
it('changes the value of the message property', function(done) { it('changes the value of the message property', function(done) {
@ -347,7 +389,7 @@ describe('change Node', function() {
}); });
}); });
it('Reports invalid regex', function(done) { it('reports invalid regex', function(done) {
var sinon = require('sinon'); var sinon = require('sinon');
var flow = [{"id":"changeNode1","type":"change","action":"change","property":"payload","from":"\\+**+","to":"NUMBER","reg":true,"name":"changeNode","wires":[["helperNode1"]]}, var flow = [{"id":"changeNode1","type":"change","action":"change","property":"payload","from":"\\+**+","to":"NUMBER","reg":true,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];
@ -363,6 +405,43 @@ describe('change Node', function() {
}); });
}); });
it('supports regex groups - new rule format', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"change","p":"payload","from":"(Hello)","to":"$1-$1-$1","fromt":"re","tot":"str"}],"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('changes the value - new rule format', function(done) {
var flow = [{"id":"changeNode1","type":"change",rules:[{"t":"change","p":"payload","from":"ABC","to":"123","fromt":"str","tot":"str"}],"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("abc123abc");
done();
} catch(err) {
done(err);
}
});
changeNode1.receive({payload:"abcABCabc"});
});
});
}); });
describe("#delete", function() { describe("#delete", function() {

View File

@ -434,7 +434,8 @@ describe('flows/util', function() {
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig); var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
diffResult.added.should.have.length(0); diffResult.added.should.have.length(0);
diffResult.changed.sort().should.eql(['2', 'sf1']); diffResult.changed.sort().should.eql(['2', 'sf1']);
diffResult.removed.should.have.length(0); diffResult.removed.should.have.length(1);
diffResult.removed.sort().should.eql(['sf1-2']);
diffResult.rewired.should.have.length(0); diffResult.rewired.should.have.length(0);
diffResult.linked.sort().should.eql(["1","3"]); diffResult.linked.sort().should.eql(["1","3"]);

View File

@ -110,6 +110,57 @@ describe("red/util", function() {
cloned.req.should.equal(msg.req); cloned.req.should.equal(msg.req);
cloned.res.should.equal(msg.res); cloned.res.should.equal(msg.res);
}); });
}); });
describe('getMessageProperty', function() {
it('retrieves a simple property', function() {
var v = util.getMessageProperty({a:"foo"},"msg.a");
v.should.eql("foo");
var v2 = util.getMessageProperty({a:"foo"},"a");
v2.should.eql("foo");
});
it('should return undefined if property does not exist', function() {
var v = util.getMessageProperty({a:"foo"},"msg.b");
should.not.exist(v);
});
it('should throw error if property parent does not exist', function() {
/*jshint immed: false */
(function() {
util.getMessageProperty({a:"foo"},"msg.a.b.c");
}).should.throw();
})
});
describe('setMessageProperty', function() {
it('sets a property', function() {
var msg = {a:"foo"};
util.setMessageProperty(msg,"msg.a","bar");
msg.a.should.eql('bar');
});
it('sets a deep level property', function() {
var msg = {a:{b:{c:"foo"}}};
util.setMessageProperty(msg,"msg.a.b.c","bar");
msg.a.b.c.should.eql('bar');
});
it('creates missing parent properties by default', function() {
var msg = {a:{}};
util.setMessageProperty(msg,"msg.a.b.c","bar");
msg.a.b.c.should.eql('bar');
})
it('does not create missing parent properties', function() {
var msg = {a:{}};
util.setMessageProperty(msg,"msg.a.b.c","bar",false);
should.not.exist(msg.a.b);
})
it('deletes property if value is undefined', function() {
var msg = {a:{b:{c:"foo"}}};
util.setMessageProperty(msg,"msg.a.b.c",undefined);
should.not.exist(msg.a.b.c);
})
it('does not create missing parent properties if value is undefined', function() {
var msg = {a:{}};
util.setMessageProperty(msg,"msg.a.b.c",undefined);
should.not.exist(msg.a.b);
})
})
}); });