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

Add RED.util.get/setObjectProperty to avoid stripping msg.

This commit is contained in:
Nick O'Leary 2018-07-25 09:27:27 +01:00
parent a2bdeedb09
commit 315a9ceba3
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 31 additions and 6 deletions

View File

@ -235,10 +235,13 @@ function normalisePropertyExpression(str) {
}
function getMessageProperty(msg,expr) {
var result = null;
if (expr.indexOf('msg.')===0) {
expr = expr.substring(4);
}
return getObjectProperty(msg,expr);
}
function getObjectProperty(msg,expr) {
var result = null;
var msgPropParts = normalisePropertyExpression(expr);
var m;
msgPropParts.reduce(function(obj, key) {
@ -249,12 +252,15 @@ function getMessageProperty(msg,expr) {
}
function setMessageProperty(msg,prop,value,createMissing) {
if (typeof createMissing === 'undefined') {
createMissing = (typeof value !== 'undefined');
}
if (prop.indexOf('msg.')===0) {
prop = prop.substring(4);
}
return setObjectProperty(msg,prop,value,createMissing);
}
function setObjectProperty(msg,prop,value,createMissing) {
if (typeof createMissing === 'undefined') {
createMissing = (typeof value !== 'undefined');
}
var msgPropParts = normalisePropertyExpression(prop);
var depth = 0;
var length = msgPropParts.length;
@ -584,6 +590,8 @@ module.exports = {
generateId: generateId,
getMessageProperty: getMessageProperty,
setMessageProperty: setMessageProperty,
getObjectProperty: getObjectProperty,
setObjectProperty: setObjectProperty,
evaluateNodeProperty: evaluateNodeProperty,
normalisePropertyExpression: normalisePropertyExpression,
normaliseNodeTypeName: normaliseNodeTypeName,

View File

@ -141,7 +141,15 @@ describe("red/util", function() {
cloned.res.should.equal(msg.res);
});
});
describe('getObjectProperty', function() {
it('gets a property beginning with "msg."', function() {
// getMessageProperty strips off `msg.` prefixes.
// getObjectProperty does not
var obj = { msg: { a: "foo"}, a: "bar"};
var v = util.getObjectProperty(obj,"msg.a");
v.should.eql("foo");
})
});
describe('getMessageProperty', function() {
it('retrieves a simple property', function() {
var v = util.getMessageProperty({a:"foo"},"msg.a");
@ -169,7 +177,16 @@ describe("red/util", function() {
});
});
describe('setObjectProperty', function() {
it('set a property beginning with "msg."', function() {
// setMessageProperty strips off `msg.` prefixes.
// setObjectProperty does not
var obj = {};
util.setObjectProperty(obj,"msg.a","bar");
obj.should.have.property("msg");
obj.msg.should.have.property("a","bar");
})
});
describe('setMessageProperty', function() {
it('sets a property', function() {
var msg = {a:"foo"};