mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Added multi-level property functionality
This commit is contained in:
parent
4f496c37be
commit
97e5c2e571
@ -36,7 +36,7 @@
|
|||||||
<div class="form-row" id="node-reg-row">
|
<div class="form-row" id="node-reg-row">
|
||||||
<label> </label>
|
<label> </label>
|
||||||
<input type="checkbox" id="node-input-reg" style="display: inline-block; width: auto; vertical-align: top;">
|
<input type="checkbox" id="node-input-reg" style="display: inline-block; width: auto; vertical-align: top;">
|
||||||
<label for="node-input-reg" style="width: 70%;">Use regular expressions ?</label>
|
<label for="node-input-reg" style="width: 70%;">Use regular expressions</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-tips" id="node-tip"></div>
|
<div class="form-tips" id="node-tip"></div>
|
||||||
<br/>
|
<br/>
|
||||||
@ -47,10 +47,10 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/x-red" data-help-name="change">
|
<script type="text/x-red" data-help-name="change">
|
||||||
<p>A simple function node to change, replace, add or delete properties of a message.</p>
|
<p>A simple function node to set, replace or delete properties of a message.</p>
|
||||||
<p>When a message arrives, the selected property is modified by the defined rules.
|
<p>When a message arrives, the selected property is modified by the defined rules.
|
||||||
The message is then sent to the output.</p>
|
The message is then sent to the output.</p>
|
||||||
<p><b>Note:</b> Replace only operates on <b>strings</b>. Anything else will be passed straight through.</p>
|
<p><b>Note:</b> Set and replace only operate using <b>strings</b>. Anything else will be passed straight through.</p>
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
@ -82,8 +82,10 @@
|
|||||||
if (this.name) {
|
if (this.name) {
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
if (this.action == "replace") {
|
if (this.action === "replace") {
|
||||||
return "set msg."+this.property;
|
return "set msg."+this.property;
|
||||||
|
} else if (this.action === "change") {
|
||||||
|
return "replace msg."+this.property;
|
||||||
} else {
|
} else {
|
||||||
return this.action+" msg."+this.property
|
return this.action+" msg."+this.property
|
||||||
}
|
}
|
||||||
|
@ -20,55 +20,49 @@ module.exports = function(RED) {
|
|||||||
RED.nodes.createNode(this, n);
|
RED.nodes.createNode(this, n);
|
||||||
this.action = n.action;
|
this.action = n.action;
|
||||||
this.property = n.property || "";
|
this.property = n.property || "";
|
||||||
this.from = n.from || " ";
|
this.from = n.from || "";
|
||||||
this.to = n.to || " ";
|
this.to = n.to || "";
|
||||||
this.reg = (n.reg === null || n.reg);
|
this.reg = (n.reg === null || n.reg);
|
||||||
var node = this;
|
var node = this;
|
||||||
if (node.reg === false) {
|
if (node.reg === false) {
|
||||||
this.from = this.from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
this.from = this.from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
||||||
}
|
}
|
||||||
var makeNew = function( stem, path, value ) {
|
|
||||||
var lastPart = (arguments.length === 3) ? path.pop() : false;
|
|
||||||
for (var i = 0; i < path.length; i++) {
|
|
||||||
stem = stem[path[i]] = stem[path[i]] || {};
|
|
||||||
}
|
|
||||||
if (lastPart) { stem = stem[lastPart] = value; }
|
|
||||||
return stem;
|
|
||||||
};
|
|
||||||
|
|
||||||
this.on('input', function (msg) {
|
this.on('input', function (msg) {
|
||||||
if (node.action == "change") {
|
var propertyParts;
|
||||||
|
var depth = 0;
|
||||||
|
|
||||||
|
if (node.action === "change") {
|
||||||
try {
|
try {
|
||||||
node.re = new RegExp(this.from, "g");
|
node.re = new RegExp(this.from, "g");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
node.error(e.message);
|
node.error(e.message);
|
||||||
}
|
}
|
||||||
if (typeof msg[node.property] === "string") {
|
|
||||||
msg[node.property] = (msg[node.property]).replace(node.re, node.to);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
propertyParts = node.property.split(".");
|
||||||
|
try {
|
||||||
|
propertyParts.reduce(function (obj, i) {
|
||||||
|
if (++depth === propertyParts.length) {
|
||||||
|
if (node.action === "change") {
|
||||||
|
if (typeof obj[i] === "string") {
|
||||||
|
obj[i] = obj[i].replace(node.re, node.to);
|
||||||
}
|
}
|
||||||
//else if (node.action == "replace") {
|
} else if (node.action === "replace") {
|
||||||
//if (node.to.indexOf("msg.") == 0) {
|
obj[i] = node.to;
|
||||||
//msg[node.property] = eval(node.to);
|
} else if (node.action === "delete") {
|
||||||
//}
|
delete(obj[i]);
|
||||||
//else {
|
|
||||||
//msg[node.property] = node.to;
|
|
||||||
//}
|
|
||||||
//}
|
|
||||||
else if (node.action == "replace") {
|
|
||||||
if (node.to.indexOf("msg.") === 0) {
|
|
||||||
makeNew( msg, node.property.split("."), eval(node.to) );
|
|
||||||
}
|
}
|
||||||
else {
|
} else {
|
||||||
makeNew( msg, node.property.split("."), node.to );
|
if (!obj[i]) {
|
||||||
|
obj[i] = {};
|
||||||
}
|
}
|
||||||
//makeNew( msg, node.property.split("."), node.to );
|
return obj[i];
|
||||||
}
|
|
||||||
else if (node.action == "delete") {
|
|
||||||
delete(msg[node.property]);
|
|
||||||
}
|
}
|
||||||
|
}, msg);
|
||||||
|
} catch (err) {}
|
||||||
node.send(msg);
|
node.send(msg);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("change", ChangeNode);
|
RED.nodes.registerType("change", ChangeNode);
|
||||||
}
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user