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

View File

@ -16,24 +16,29 @@
<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.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">
<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%">
<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="hidden" id="node-input-spltType">
</div>
<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>
</div>
<div class="form-row"><span data-i18n="[html]split.array"></span></div>
<div class="form-row">
<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%">
<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="hidden" id="node-input-arraySpltType">
</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/>
<div class="form-row">
<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"},
arraySplt: {value:1},
arraySpltType: {value:"len"},
stream: {value: false}
stream: {value:false},
addname: {value:false},
addfname: {value:"topic"}
},
inputs:1,
outputs:1,
@ -104,10 +111,15 @@
{value:"len", label:RED._("node-red:split.splitLength"),validate:/^\d+$/}
]
});
$("#node-input-addfname").typedInput({
typeField: $("#node-input-fnameType"),
types:['msg']
});
}
});
</script>
<script type="text/x-red" data-template-name="join">
<div class="form-row">
<label data-i18n="join.mode.mode"></label>

View File

@ -39,6 +39,8 @@ module.exports = function(RED) {
var node = this;
node.stream = n.stream;
node.spltType = n.spltType || "str";
node.addname = n.addname || false;
node.addfname = n.addfname;
try {
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");
@ -147,6 +149,7 @@ module.exports = function(RED) {
for (var p in pay) {
if (pay.hasOwnProperty(p)) {
msg.payload = pay[p];
if (node.addname === true) { msg[node.addfname] = p; }
msg.parts.key = p;
msg.parts.index = j;
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("count");
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 === 1) { msg.payload.should.equal("2"); }
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}});
});
});