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

Fix CSV node to handle \n when outputting text fields

and add tests
This commit is contained in:
Dave Conway-Jones 2022-06-28 10:14:12 +01:00
parent 4467c0df31
commit b60fd36c6e
No known key found for this signature in database
GPG Key ID: 88BA2B8A411BE9FF
2 changed files with 17 additions and 14 deletions

View File

@ -89,6 +89,9 @@ module.exports = function(RED) {
else if (msg.payload[s][t].toString().indexOf(node.sep) !== -1) { // add quotes if any "commas" else if (msg.payload[s][t].toString().indexOf(node.sep) !== -1) { // add quotes if any "commas"
msg.payload[s][t] = node.quo + msg.payload[s][t].toString() + node.quo; msg.payload[s][t] = node.quo + msg.payload[s][t].toString() + node.quo;
} }
else if (msg.payload[s][t].toString().indexOf("\n") !== -1) { // add quotes if any "\n"
msg.payload[s][t] = node.quo + msg.payload[s][t].toString() + node.quo;
}
} }
ou += msg.payload[s].join(node.sep) + node.ret; ou += msg.payload[s].join(node.sep) + node.ret;
} }
@ -112,7 +115,7 @@ module.exports = function(RED) {
q = q.replace(/"/g, '""'); q = q.replace(/"/g, '""');
ou += node.quo + q + node.quo + node.sep; ou += node.quo + q + node.quo + node.sep;
} }
else if (q.indexOf(node.sep) !== -1) { // add quotes if any "commas" else if (q.indexOf(node.sep) !== -1 || p.indexOf("\n") !== -1) { // add quotes if any "commas" or "\n"
ou += node.quo + q + node.quo + node.sep; ou += node.quo + q + node.quo + node.sep;
} }
else { ou += q + node.sep; } // otherwise just add else { ou += q + node.sep; } // otherwise just add
@ -134,7 +137,7 @@ module.exports = function(RED) {
p = p.replace(/"/g, '""'); p = p.replace(/"/g, '""');
ou += node.quo + p + node.quo + node.sep; ou += node.quo + p + node.quo + node.sep;
} }
else if (p.indexOf(node.sep) !== -1) { // add quotes if any "commas" else if (p.indexOf(node.sep) !== -1 || p.indexOf("\n") !== -1) { // add quotes if any "commas" or "\n"
ou += node.quo + p + node.quo + node.sep; ou += node.quo + p + node.quo + node.sep;
} }
else { ou += p + node.sep; } // otherwise just add else { ou += p + node.sep; } // otherwise just add

View File

@ -693,19 +693,19 @@ describe('CSV node', function() {
describe('json object to csv', function() { describe('json object to csv', function() {
it('should convert a simple object back to a csv', function(done) { it('should convert a simple object back to a csv', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,,e", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,,e,f", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try { try {
msg.should.have.property('payload', '4,foo,true,,0\n'); msg.should.have.property('payload', '4,foo,true,,0,"Hello\nWorld"\n');
done(); done();
} }
catch(e) { done(e); } catch(e) { done(e); }
}); });
var testJson = { e:0, d:1, b:"foo", c:true, a:4 }; var testJson = { e:0, d:1, b:"foo", c:true, a:4, f:"Hello\nWorld" };
n1.emit("input", {payload:testJson}); n1.emit("input", {payload:testJson});
}); });
}); });
@ -790,12 +790,12 @@ describe('CSV node', function() {
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try { try {
msg.should.have.property('payload', 'a,b,c,d\n4,3,2,1\n1,2,3,4\n'); msg.should.have.property('payload', 'a,b,c,d\n4,3,2,1\n1,2,3,"a\nb"\n');
done(); done();
} }
catch(e) { done(e); } catch(e) { done(e); }
}); });
var testJson = [{ d: 1, b: 3, c: 2, a: 4 },{d:4,a:1,c:3,b:2}]; var testJson = [{ d:1, b:3, c:2, a:4 },{d:"a\nb", a:1, c:3, b:2}];
n1.emit("input", {payload:testJson}); n1.emit("input", {payload:testJson});
}); });
}); });
@ -826,12 +826,12 @@ describe('CSV node', function() {
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try { try {
msg.should.have.property('payload', 'd,b,c,a\n1,3,2,4\n4,2,3,1\n'); msg.should.have.property('payload', 'd,b,c,a\n1,3,2,4\n4,"f\ng",3,1\n');
done(); done();
} }
catch(e) { done(e); } catch(e) { done(e); }
}); });
var testJson = [{ d: 1, b: 3, c: 2, a: 4 },{d:4,a:1,c:3,b:2}]; var testJson = [{ d: 1, b: 3, c: 2, a: 4 },{d:4,a:1,c:3,b:"f\ng"}];
n1.emit("input", {payload:testJson}); n1.emit("input", {payload:testJson});
}); });
}); });
@ -844,12 +844,12 @@ describe('CSV node', function() {
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try { try {
msg.should.have.property('payload', ',0,1,foo,"ba""r","di,ng"\n'); msg.should.have.property('payload', ',0,1,foo,"ba""r","di,ng","fa\nba"\n');
done(); done();
} }
catch(e) { done(e); } catch(e) { done(e); }
}); });
var testJson = ["",0,1,"foo",'ba"r','di,ng']; var testJson = ["",0,1,"foo",'ba"r','di,ng',"fa\nba"];
n1.emit("input", {payload:testJson}); n1.emit("input", {payload:testJson});
}); });
}); });
@ -898,12 +898,12 @@ describe('CSV node', function() {
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
try { 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'); msg.should.have.property('payload', 'col1,col2,col3,col4\r\nH1,H2,H3,H4\r\nA,B,,\r\nA,,C,\r\nA,,,"D\nE"\r\n');
done(); done();
} }
catch(e) { done(e); } 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"}]; var testJson = [{"col1":"H1","col2":"H2","col3":"H3","col4":"H4"},{"col1":"A","col2":"B"},{"col1":"A","col3":"C"},{"col1":"A","col4":"D\nE"}];
n1.emit("input", {payload:testJson}); n1.emit("input", {payload:testJson});
}); });
}); });