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

Merge pull request #3716 from node-red/CSV-fix-n-handling-inside-text-fields

Fix CSV node to handle \n when outputting text fields
This commit is contained in:
Nick O'Leary 2022-06-28 20:55:29 +01:00 committed by GitHub
commit 52db4b3321
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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});
}); });
}); });
@ -777,7 +777,7 @@ describe('CSV node', function() {
} }
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:2}];
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});
}); });
}); });