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"
},
"errors": {
"invalid-from": "Invalid 'from' property: __error__"
"invalid-from": "Invalid 'from' property: __error__",
"invalid-json": "Invalid 'to' JSON property"
}
},
"range": {

View File

@ -51,7 +51,7 @@
category: 'function',
defaults: {
name: {value:""},
rules:{value:[{t:"set",p:"payload",to:""}]},
rules:{value:[{t:"set",p:"payload",pt:"msg",to:"",tot:"str"}]},
// legacy
action: {value:""},
property: {value:""},
@ -77,11 +77,11 @@
} else {
if (this.rules.length == 1) {
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") {
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 {
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 {
return this._("change.label.changeCount",{count:this.rules.length});
@ -99,14 +99,27 @@
var search = this._("change.action.search");
var replace = this._("change.action.replace");
var regex = this._("change.label.regex");
if (this.reg === null) { $("#node-input-reg").prop('checked', true); }
$("#node-input-action").change();
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) {
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 row1 = $('<div/>').appendTo(container);
@ -114,37 +127,28 @@
var row2 = $('<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}];
for (var i=0;i<3;i++) {
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: 220px",class:"node-input-rule-property-name",type:"text"}).appendTo(row1);
var propertyName = $('<input/>',{style:"width: 250px",class:"node-input-rule-property-name",type:"text"}).appendTo(row1).propertySelect({options:['msg','flow','global']});
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);
$('<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);
var propertyValue = $('<input/>',{style:"width: 220px",class:"node-input-rule-property-value",type:"text"}).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: 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);
$('<div/>',{style:"display: inline-block;text-align:right; width:150px;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);
$('<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: 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);
$('<div/>',{style:"display: inline-block;text-align:right; width:150px;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 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);
$('<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: 250px",class:"node-input-rule-property-replace-value",type:"text"}).appendTo(row3_2).propertySelect({default:'str',options:['msg','flow','global','str','num','json']});
selectField.change(function() {
var width = $("#node-input-rule-container").width();
@ -170,10 +174,13 @@
selectField.find("option").filter(function() {return $(this).val() == rule.t;}).attr('selected',true);
propertyName.val(rule.p);
propertyName.propertySelect('type',rule.pt)
propertyValue.val(rule.to);
propertyValue.propertySelect('type',rule.tot)
fromValue.val(rule.from);
fromValue.propertySelect('type',rule.fromt)
toValue.val(rule.to);
useRegExp.prop('checked', rule.re);
toValue.propertySelect('type',rule.tot)
selectField.change();
$("#node-input-rule-container").append(container);
@ -185,7 +192,8 @@
if (!this.rules) {
var rule = {
t:(this.action=="replace"?"set":this.action),
p:this.property
p:this.property,
pt:"msg"
}
if (rule.t === "set") {
@ -250,13 +258,17 @@
var type = rule.find(".node-input-rule-type option:selected").val();
var r = {
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") {
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") {
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.tot = rule.find(".node-input-rule-property-replace-value").propertySelect('type');
r.re = rule.find(".node-input-rule-property-re").prop('checked');
}
node.rules.push(r);

View File

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

View File

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

View File

@ -103,10 +103,58 @@ function compareObjects(obj1,obj2) {
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 = {
ensureString: ensureString,
ensureBuffer: ensureBuffer,
cloneMessage: cloneMessage,
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" }];
helper.load(changeNode, flow, function() {
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();
});
});
@ -42,7 +42,7 @@ describe('change Node', function() {
var flow = [{ id: "c1", type: "change", name:"change1", action:"replace" }];
helper.load(changeNode, flow, function() {
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();
});
});
@ -51,12 +51,12 @@ describe('change Node', function() {
helper.load(changeNode, flow, function() {
//console.log(helper.getNode("c1"));
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();
});
});
describe('#replace' , function() {
describe('#set' , function() {
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"]]},
@ -64,6 +64,7 @@ describe('change Node', function() {
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
var rule = helper.getNode("changeNode1").rules[0];
helperNode1.on("input", function(msg) {
try {
msg.payload.should.equal("changed");
@ -118,6 +119,9 @@ describe('change Node', function() {
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
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) {
try {
msg.foo.should.equal("bar");
@ -237,6 +241,44 @@ describe('change Node', function() {
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() {
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 flow = [{"id":"changeNode1","type":"change","action":"change","property":"payload","from":"\\+**+","to":"NUMBER","reg":true,"name":"changeNode","wires":[["helperNode1"]]},
{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() {

View File

@ -434,7 +434,8 @@ describe('flows/util', function() {
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
diffResult.added.should.have.length(0);
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.linked.sort().should.eql(["1","3"]);

View File

@ -110,6 +110,57 @@ describe("red/util", function() {
cloned.req.should.equal(msg.req);
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);
})
})
});