mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
fix csv node template reset when array complete
and add tests to close #2853
This commit is contained in:
parent
23f0cd3a26
commit
4313cbaa5c
@ -52,7 +52,8 @@ module.exports = function(RED) {
|
||||
else { node.goodtmpl = true; }
|
||||
return col;
|
||||
}
|
||||
node.template = clean(node.template);
|
||||
var template = clean(node.template);
|
||||
var notemplate = node.template.length === 1 && node.template[0] === '';
|
||||
node.hdrSent = false;
|
||||
|
||||
this.on("input", function(msg) {
|
||||
@ -62,18 +63,21 @@ module.exports = function(RED) {
|
||||
if (msg.hasOwnProperty("payload")) {
|
||||
if (typeof msg.payload == "object") { // convert object to CSV string
|
||||
try {
|
||||
if (!(notemplate && (msg.hasOwnProperty("parts") && msg.parts.hasOwnProperty("index") && msg.parts.index > 0))) {
|
||||
template = clean(node.template);
|
||||
}
|
||||
var ou = "";
|
||||
if (!Array.isArray(msg.payload)) { msg.payload = [ msg.payload ]; }
|
||||
if (node.hdrout !== "none" && node.hdrSent === false) {
|
||||
if ((node.template.length === 1) && (node.template[0] === '')) {
|
||||
if ((template.length === 1) && (template[0] === '')) {
|
||||
if (msg.hasOwnProperty("columns")) {
|
||||
node.template = clean((msg.columns || "").split(","));
|
||||
template = clean((msg.columns || "").split(","));
|
||||
}
|
||||
else {
|
||||
node.template = Object.keys(msg.payload[0]);
|
||||
template = Object.keys(msg.payload[0]);
|
||||
}
|
||||
}
|
||||
ou += node.template.join(node.sep) + node.ret;
|
||||
ou += template.join(node.sep) + node.ret;
|
||||
if (node.hdrout === "once") { node.hdrSent = true; }
|
||||
}
|
||||
for (var s = 0; s < msg.payload.length; s++) {
|
||||
@ -92,10 +96,10 @@ module.exports = function(RED) {
|
||||
ou += msg.payload[s].join(node.sep) + node.ret;
|
||||
}
|
||||
else {
|
||||
if ((node.template.length === 1) && (node.template[0] === '') && (msg.hasOwnProperty("columns"))) {
|
||||
node.template = clean((msg.columns || "").split(","));
|
||||
if ((template.length === 1) && (template[0] === '') && (msg.hasOwnProperty("columns"))) {
|
||||
template = clean((msg.columns || "").split(","));
|
||||
}
|
||||
if ((node.template.length === 1) && (node.template[0] === '')) {
|
||||
if ((template.length === 1) && (template[0] === '')) {
|
||||
/* istanbul ignore else */
|
||||
if (tmpwarn === true) { // just warn about missing template once
|
||||
node.warn(RED._("csv.errors.obj_csv"));
|
||||
@ -121,12 +125,12 @@ module.exports = function(RED) {
|
||||
ou = ou.slice(0,-1) + node.ret;
|
||||
}
|
||||
else {
|
||||
for (var t=0; t < node.template.length; t++) {
|
||||
if (node.template[t] === '') {
|
||||
for (var t=0; t < template.length; t++) {
|
||||
if (template[t] === '') {
|
||||
ou += node.sep;
|
||||
}
|
||||
else {
|
||||
var p = RED.util.ensureString(RED.util.getMessageProperty(msg,"payload["+s+"]['"+node.template[t]+"']"));
|
||||
var p = RED.util.ensureString(RED.util.getMessageProperty(msg,"payload["+s+"]['"+template[t]+"']"));
|
||||
/* istanbul ignore else */
|
||||
if (p === "undefined") { p = ""; }
|
||||
if (p.indexOf(node.quo) !== -1) { // add double quotes if any quotes
|
||||
@ -144,7 +148,7 @@ module.exports = function(RED) {
|
||||
}
|
||||
}
|
||||
msg.payload = ou;
|
||||
msg.columns = node.template.join(',');
|
||||
msg.columns = template.join(',');
|
||||
if (msg.payload !== '') { node.send(msg); }
|
||||
}
|
||||
catch(e) { node.error(e,msg); }
|
||||
|
@ -680,6 +680,24 @@ describe('CSV node', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to include column names as first row, and missing properties', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", hdrout:true, ret:"\r\n", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property('payload', 'col1,col2,col3,col4\r\nH1,H2,H3,H4\r\nA,B,,\r\nA,,C,\r\nA,,,D\r\n');
|
||||
done();
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
var testJson = [{"col1":"H1","col2":"H2","col3":"H3","col4":"H4"},{"col1":"A","col2":"B"},{"col1":"A","col3":"C"},{"col1":"A","col4":"D"}];
|
||||
n1.emit("input", {payload:testJson});
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to pass in column names', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", temp:"", hdrout:"once", ret:"\r\n", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
@ -701,9 +719,27 @@ describe('CSV node', function() {
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
var testJson = [{ d: 1, b: 3, c: 2, a: 4 }];
|
||||
n1.emit("input", {payload:testJson, columns:"a,,b,a"});
|
||||
n1.emit("input", {payload:testJson});
|
||||
n1.emit("input", {payload:testJson});
|
||||
n1.emit("input", {payload:testJson, columns:"a,,b,a", parts:{index:0}});
|
||||
n1.emit("input", {payload:testJson, parts:{index:1}});
|
||||
n1.emit("input", {payload:testJson, parts:{index:2}});
|
||||
});
|
||||
});
|
||||
|
||||
it('should be able to pass in column names - with payload as an array', function(done) {
|
||||
var flow = [ { id:"n1", type:"csv", hdrout:"once", ret:"\r\n", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(csvNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
n2.on("input", function(msg) {
|
||||
try {
|
||||
msg.should.have.property('payload', 'a,,b,a\r\n4,,3,4\r\n4,,3,4\r\n4,,3,4\r\n');
|
||||
done()
|
||||
}
|
||||
catch(e) { done(e); }
|
||||
});
|
||||
var testJson = { d: 1, b: 3, c: 2, a: 4 };
|
||||
n1.emit("input", {payload:[testJson,testJson,testJson], columns:"a,,b,a"});
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user