mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Allow RED.settings.get/set to use full property desc
This commit is contained in:
@@ -722,6 +722,62 @@ RED.utils = (function() {
|
||||
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 = normalisePropertyExpression(prop);
|
||||
var depth = 0;
|
||||
var length = msgPropParts.length;
|
||||
var obj = msg;
|
||||
var key;
|
||||
for (var i=0;i<length-1;i++) {
|
||||
key = msgPropParts[i];
|
||||
if (typeof key === 'string' || (typeof key === 'number' && !Array.isArray(obj))) {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
obj = obj[key];
|
||||
} else if (createMissing) {
|
||||
if (typeof msgPropParts[i+1] === 'string') {
|
||||
obj[key] = {};
|
||||
} else {
|
||||
obj[key] = [];
|
||||
}
|
||||
obj = obj[key];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else if (typeof key === 'number') {
|
||||
// obj is an array
|
||||
if (obj[key] === undefined) {
|
||||
if (createMissing) {
|
||||
if (typeof msgPropParts[i+1] === 'string') {
|
||||
obj[key] = {};
|
||||
} else {
|
||||
obj[key] = [];
|
||||
}
|
||||
obj = obj[key];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
obj = obj[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
key = msgPropParts[length-1];
|
||||
if (typeof value === "undefined") {
|
||||
if (typeof key === 'number' && Array.isArray(obj)) {
|
||||
obj.splice(key,1);
|
||||
} else {
|
||||
delete obj[key]
|
||||
}
|
||||
} else {
|
||||
obj[key] = value;
|
||||
}
|
||||
}
|
||||
function separateIconPath(icon) {
|
||||
var result = {module: "", file: ""};
|
||||
if (icon) {
|
||||
@@ -957,6 +1013,7 @@ RED.utils = (function() {
|
||||
return {
|
||||
createObjectElement: buildMessageElement,
|
||||
getMessageProperty: getMessageProperty,
|
||||
setMessageProperty: setMessageProperty,
|
||||
normalisePropertyExpression: normalisePropertyExpression,
|
||||
validatePropertyExpression: validatePropertyExpression,
|
||||
separateIconPath: separateIconPath,
|
||||
|
Reference in New Issue
Block a user