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

preserve other msg properties when passing through CSV node

This commit is contained in:
dceejay 2015-02-22 19:23:36 +00:00
parent 57dedcf816
commit 43054906dc

View File

@ -72,7 +72,8 @@ module.exports = function(RED) {
}
ou = ou.slice(0,-1) + node.ret; // remove final "comma" and add "newline"
}
node.send({payload:ou});
msg.payload = ou;
node.send(msg);
}
catch(e) { node.log(e); }
}
@ -84,27 +85,28 @@ module.exports = function(RED) {
var o = {}; // output object to build up
var a = []; // output array is needed for multiline option
var first = true; // is this the first line
var line = msg.payload;
var tmp = "";
// 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...
// Now process the whole file/line
for (var i = 0; i < msg.payload.length; i++) {
for (var i = 0; i < line.length; i++) {
if ((node.hdrin === true) && first) { // if the template is in the first line
if ((msg.payload[i] === "\n")||(msg.payload[i] === "\r")) { // look for first line break
if ((line[i] === "\n")||(line[i] === "\r")) { // look for first line break
node.template = clean(tmp.split(node.sep));
first = false;
}
else { tmp += msg.payload[i]; }
else { tmp += line[i]; }
}
else {
if (msg.payload[i] === node.quo) { // if it's a quote toggle inside or outside
if (line[i] === node.quo) { // if it's a quote toggle inside or outside
f = !f;
if (msg.payload[i-1] === node.quo) { k[j] += '\"'; } // if it's a quotequote then it's actually a quote
if ((msg.payload[i-1] !== node.sep) && (msg.payload[i+1] !== node.sep)) { k[j] += msg.payload[i]; }
if (line[i-1] === node.quo) { k[j] += '\"'; } // if it's a quotequote then it's actually a quote
if ((line[i-1] !== node.sep) && (line[i+1] !== node.sep)) { k[j] += line[i]; }
}
else if ((msg.payload[i] === node.sep) && f) { // if we are outside of quote (ie valid separator
else if ((line[i] === node.sep) && f) { // if we are outside of quote (ie valid separator
if (!node.goodtmpl) { node.template[j] = "col"+(j+1); }
if ( node.template[j] && (node.template[j] !== "") && (k[j] !== "" ) ) {
if ( (k[j].charAt(0) !== "+") && !isNaN(Number(k[j])) ) { k[j] = Number(k[j]); }
@ -113,7 +115,7 @@ module.exports = function(RED) {
j += 1;
k[j] = "";
}
else if (f && ((msg.payload[i] === "\n") || (msg.payload[i] === "\r"))) { // handle multiple lines
else if (f && ((line[i] === "\n") || (line[i] === "\r"))) { // handle multiple lines
//console.log(j,k,o,k[j]);
if ( node.template[j] && (node.template[j] !== "") && (k[j] !== "") ) {
if ( (k[j].charAt(0) !== "+") && !isNaN(Number(k[j])) ) { k[j] = Number(k[j]); }
@ -121,7 +123,10 @@ module.exports = function(RED) {
o[node.template[j]] = k[j];
}
if (JSON.stringify(o) !== "{}") { // don't send empty objects
if (node.multi === "one") { node.send({payload:o}); } // either send
if (node.multi === "one") {
msg.payload = o;
node.send(msg); // either send
}
else { a.push(o); } // or add to the array
}
j = 0;
@ -129,7 +134,7 @@ module.exports = function(RED) {
o = {};
}
else { // just add to the part of the message
k[j] += msg.payload[i];
k[j] += line[i];
}
}
}
@ -141,12 +146,17 @@ module.exports = function(RED) {
else { k[j].replace(/\r$/,''); }
o[node.template[j]] = k[j];
}
msg.payload = o;
if (JSON.stringify(o) !== "{}") { // don't send empty objects
if (node.multi === "one") { node.send({payload:o}); } // either send
if (node.multi === "one") {
msg.payload = o;
node.send(msg); // either send
}
else { a.push(o); } // or add to the aray
}
if (node.multi !== "one") { node.send({payload:a}); } // finally send the array
if (node.multi !== "one") {
msg.payload = a;
node.send(msg); // finally send the array
}
}
catch(e) { node.log(e); }
}