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

Let CSV correct parts if we remove header row.

and add test
This commit is contained in:
Dave Conway-Jones 2017-12-05 17:34:14 +00:00
parent 8bb9b594cf
commit 27db727321
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
2 changed files with 47 additions and 11 deletions

View File

@ -72,6 +72,7 @@ module.exports = function(RED) {
}
else {
if ((node.template.length === 1) && (node.template[0] === '')) {
/* istanbul ignore else */
if (tmpwarn === true) { // just warn about missing template once
node.warn(RED._("csv.errors.obj_csv"));
tmpwarn = false;
@ -135,6 +136,9 @@ module.exports = function(RED) {
var line = msg.payload;
var tmp = "";
var reg = /^[-]?[0-9]*\.?[0-9]+$/;
if (msg.hasOwnProperty("parts")) {
if (msg.parts.index > 0) { first = false; }
}
// For now we are just going to assume that any \r or \n means an end of line...
// got to be a weird csv that has singleton \r \n in it for another reason...
@ -201,22 +205,26 @@ module.exports = function(RED) {
msg.payload = a;
node.send(msg); // finally send the array
}
else {
else {
var has_parts = msg.hasOwnProperty("parts");
var len = a.length;
for(var i = 0; i < len; i++) {
var len = a.length;
for (var i = 0; i < len; i++) {
var newMessage = RED.util.cloneMessage(msg);
newMessage.payload = a[i];
if(!has_parts) {
newMessage.parts = {
id: msg._msgid,
index: i,
count: len
};
if (!has_parts) {
newMessage.parts = {
id: msg._msgid,
index: i,
count: len
};
}
else if (node.hdrin) { // if we removed the header line then shift the counts by 1
newMessage.parts.index -= 1;
newMessage.parts.count -= 1;
}
node.send(newMessage);
}
}
}
}
}
catch(e) { node.error(e,msg); }
}

View File

@ -237,6 +237,34 @@ describe('CSV node', function() {
});
});
it('should be able to use the first of multiple parts as a template if parts are present', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"", hdrin:true, wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var c = 0;
n2.on("input", function(msg) {
if (c === 0) {
msg.should.have.property('payload', { w: 1, x: 2, y: 3, z: 4 });
check_parts(msg, 0, 2);
c += 1;
}
else {
msg.should.have.property('payload', { w: 5, x: 6, y: 7, z: 8 });
check_parts(msg, 1, 2);
done();
}
});
var testString1 = "w,x,y,z\n";
var testString2 = "1,2,3,4\n";
var testString3 = "5,6,7,8\n";
n1.emit("input", {payload:testString1, parts:{id:"X", index:0, count:3}});
n1.emit("input", {payload:testString2, parts:{id:"X", index:1, count:3}});
n1.emit("input", {payload:testString3, parts:{id:"X", index:2, count:3}});
});
});
});
describe('json object to csv', function() {