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

let split of objects use key to set another property (e.g. topic)

and add tests
and update messages
This commit is contained in:
Dave Conway-Jones 2017-06-22 18:42:54 +01:00
parent b91c178200
commit dc9fa81346
No known key found for this signature in database
GPG Key ID: 81B04231572A9A2D
4 changed files with 58 additions and 15 deletions

View File

@ -758,7 +758,9 @@
"action": "Action", "action": "Action",
"addnewline": "Add newline (\\n) to each payload?", "addnewline": "Add newline (\\n) to each payload?",
"createdir": "Create directory if it doesn't exist?", "createdir": "Create directory if it doesn't exist?",
"outputas": "Output as", "outputas": "Output",
"breakchunks": "Break into chunks",
"breaklines": "Break into lines",
"filelabel": "file", "filelabel": "file",
"deletelabel": "delete __file__" "deletelabel": "delete __file__"
}, },
@ -768,8 +770,10 @@
"delete": "delete file" "delete": "delete file"
}, },
"output": { "output": {
"utf8": "a utf8 string", "utf8": "a single utf8 string",
"buffer": "a Buffer" "buffer": "a single Buffer object",
"lines": "a msg per line",
"stream": "a stream of Buffers"
}, },
"status": { "status": {
"wrotefile": "wrote to file: __file__", "wrotefile": "wrote to file: __file__",
@ -792,9 +796,10 @@
"objectSend":"Send a message for each key/value pair", "objectSend":"Send a message for each key/value pair",
"strBuff":"<b>String</b> / <b>Buffer</b>", "strBuff":"<b>String</b> / <b>Buffer</b>",
"array":"<b>Array</b>", "array":"<b>Array</b>",
"splitUsing": "Split using", "splitUsing":"Split using",
"splitLength": "Fixed length of", "splitLength":"Fixed length of",
"stream":"Handle as a stream of messages" "stream":"Handle as a stream of messages",
"addname":" Copy key to "
}, },
"join":{ "join":{
"mode":{ "mode":{

View File

@ -16,24 +16,29 @@
<script type="text/x-red" data-template-name="split"> <script type="text/x-red" data-template-name="split">
<div class="form-row"><span data-i18n="[html]split.intro"></span></div> <div class="form-row"><span data-i18n="[html]split.intro"></span></div>
<div class="form-row"><span data-i18n="[html]split.object"></span></div>
<div class="form-row" style="padding-left: 10px"><span data-i18n="[html]split.objectSend"></span></div>
<div class="form-row"><span data-i18n="[html]split.strBuff"></span></div> <div class="form-row"><span data-i18n="[html]split.strBuff"></span></div>
<div class="form-row"> <div class="form-row">
<label for="node-input-splt" style="padding-left: 10px;margin-right: -10px;" data-i18n="split.splitUsing"></label> <label for="node-input-splt" style="padding-left:10px; margin-right:-10px;" data-i18n="split.splitUsing"></label>
<input type="text" id="node-input-splt" style="width: 70%"> <input type="text" id="node-input-splt" style="width:70%">
<input type="hidden" id="node-input-spltType"> <input type="hidden" id="node-input-spltType">
</div> </div>
<div class="form-row"> <div class="form-row">
<input type="checkbox" id="node-input-stream" style="margin-left: 10px; vertical-align:top; width: auto;"> <input type="checkbox" id="node-input-stream" style="margin-left:10px; vertical-align:top; width:auto;">
<label for="node-input-stream" style="width:auto;" data-i18n="split.stream"></label> <label for="node-input-stream" style="width:auto;" data-i18n="split.stream"></label>
</div> </div>
<div class="form-row"><span data-i18n="[html]split.array"></span></div> <div class="form-row"><span data-i18n="[html]split.array"></span></div>
<div class="form-row"> <div class="form-row">
<label for="node-input-arraySplt" style="padding-left: 10px;margin-right: -10px;" data-i18n="split.splitUsing"></label> <label for="node-input-arraySplt" style="padding-left:10px; margin-right:-10px;" data-i18n="split.splitUsing"></label>
<input type="text" id="node-input-arraySplt" style="width: 70%"> <input type="text" id="node-input-arraySplt" style="width:70%">
<input type="hidden" id="node-input-arraySpltType"> <input type="hidden" id="node-input-arraySpltType">
</div> </div>
<div class="form-row"><span data-i18n="[html]split.object"></span></div>
<div class="form-row" style="padding-left: 10px"><span data-i18n="[html]split.objectSend"></span></div>
<div class="form-row">
<input type="checkbox" id="node-input-addname" style="margin-left:10px; vertical-align:baseline; width:auto;">
<label for="node-input-addname" style="width:auto;" data-i18n="split.addname"></label>
<input type="text" id="node-input-addfname" style="width:70%">
</div>
<hr/> <hr/>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label> <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
@ -73,7 +78,9 @@
spltType: {value:"str"}, spltType: {value:"str"},
arraySplt: {value:1}, arraySplt: {value:1},
arraySpltType: {value:"len"}, arraySpltType: {value:"len"},
stream: {value: false} stream: {value:false},
addname: {value:false},
addfname: {value:"topic"}
}, },
inputs:1, inputs:1,
outputs:1, outputs:1,
@ -104,10 +111,15 @@
{value:"len", label:RED._("node-red:split.splitLength"),validate:/^\d+$/} {value:"len", label:RED._("node-red:split.splitLength"),validate:/^\d+$/}
] ]
}); });
$("#node-input-addfname").typedInput({
typeField: $("#node-input-fnameType"),
types:['msg']
});
} }
}); });
</script> </script>
<script type="text/x-red" data-template-name="join"> <script type="text/x-red" data-template-name="join">
<div class="form-row"> <div class="form-row">
<label data-i18n="join.mode.mode"></label> <label data-i18n="join.mode.mode"></label>

View File

@ -39,6 +39,8 @@ module.exports = function(RED) {
var node = this; var node = this;
node.stream = n.stream; node.stream = n.stream;
node.spltType = n.spltType || "str"; node.spltType = n.spltType || "str";
node.addname = n.addname || false;
node.addfname = n.addfname;
try { try {
if (node.spltType === "str") { if (node.spltType === "str") {
this.splt = (n.splt || "\\n").replace(/\\n/,"\n").replace(/\\r/,"\r").replace(/\\t/,"\t").replace(/\\e/,"\e").replace(/\\f/,"\f").replace(/\\0/,"\0"); this.splt = (n.splt || "\\n").replace(/\\n/,"\n").replace(/\\r/,"\r").replace(/\\t/,"\t").replace(/\\e/,"\e").replace(/\\f/,"\f").replace(/\\0/,"\0");
@ -147,6 +149,7 @@ module.exports = function(RED) {
for (var p in pay) { for (var p in pay) {
if (pay.hasOwnProperty(p)) { if (pay.hasOwnProperty(p)) {
msg.payload = pay[p]; msg.payload = pay[p];
if (node.addname === true) { msg[node.addfname] = p; }
msg.parts.key = p; msg.parts.key = p;
msg.parts.index = j; msg.parts.index = j;
msg.parts.count = l; msg.parts.count = l;

View File

@ -91,11 +91,34 @@ describe('SPLIT node', function() {
msg.parts.should.have.property("key"); msg.parts.should.have.property("key");
msg.parts.should.have.property("count"); msg.parts.should.have.property("count");
msg.parts.should.have.property("index"); msg.parts.should.have.property("index");
msg.topic.should.equal("foo");
if (msg.parts.index === 0) { msg.payload.should.equal(1); } if (msg.parts.index === 0) { msg.payload.should.equal(1); }
if (msg.parts.index === 1) { msg.payload.should.equal("2"); } if (msg.parts.index === 1) { msg.payload.should.equal("2"); }
if (msg.parts.index === 2) { msg.payload.should.equal(true); done(); } if (msg.parts.index === 2) { msg.payload.should.equal(true); done(); }
}); });
sn1.receive({payload:{a:1,b:"2",c:true}}); sn1.receive({topic:"foo",payload:{a:1,b:"2",c:true}});
});
});
it('should split an object into pieces and overwrite their topics', function(done) {
var flow = [{id:"sn1", type:"split", addname:true, addfname:"topic", wires:[["sn2"]]},
{id:"sn2", type:"helper"}];
helper.load(splitNode, flow, function() {
var sn1 = helper.getNode("sn1");
var sn2 = helper.getNode("sn2");
var count = 0;
sn2.on("input", function(msg) {
msg.should.have.property("payload");
msg.should.have.property("parts");
msg.parts.should.have.property("type","object");
msg.parts.should.have.property("key");
msg.parts.should.have.property("count");
msg.parts.should.have.property("index");
if (msg.parts.index === 0) { msg.payload.should.equal(1); msg.topic.should.equal("a"); }
if (msg.parts.index === 1) { msg.payload.should.equal("2"); msg.topic.should.equal("b"); }
if (msg.parts.index === 2) { msg.payload.should.equal(true); msg.topic.should.equal("c"); done(); }
});
sn1.receive({topic:"foo",payload:{a:1,b:"2",c:true}});
}); });
}); });