From bfcd795687b95d8073b7a761c45ed1a30ae4210f Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Mon, 20 Feb 2017 22:44:37 +0000 Subject: [PATCH] Fix wrong number of double quotes in CSV parsing to close #1162 --- nodes/core/parsers/70-CSV.js | 14 ++++++++++++-- test/nodes/core/parsers/70-CSV_spec.js | 9 ++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/nodes/core/parsers/70-CSV.js b/nodes/core/parsers/70-CSV.js index 9d10e70a0..fa1ece00f 100644 --- a/nodes/core/parsers/70-CSV.js +++ b/nodes/core/parsers/70-CSV.js @@ -80,7 +80,15 @@ module.exports = function(RED) { for (var p in msg.payload[0]) { if (msg.payload[0].hasOwnProperty(p)) { if (typeof msg.payload[0][p] !== "object") { - ou += msg.payload[0][p] + ","; + var q = msg.payload[0][p]; + if (q.indexOf(node.quo) !== -1) { // add double quotes if any quotes + q = q.replace(/"/g, '""'); + ou += node.quo + q + node.quo + node.sep; + } + else if (q.indexOf(node.sep) !== -1) { // add quotes if any "commas" + ou += node.quo + q + node.quo + node.sep; + } + else { ou += q + node.sep; } // otherwise just add } } } @@ -142,7 +150,9 @@ module.exports = function(RED) { else { if (line[i] === node.quo) { // if it's a quote toggle inside or outside f = !f; - if (line[i-1] === node.quo) { k[j] += '\"'; } // if it's a quotequote then it's actually a quote + if (line[i-1] === node.quo) { + if (f === false) { 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 ((line[i] === node.sep) && f) { // if it is the end of the line then finish diff --git a/test/nodes/core/parsers/70-CSV_spec.js b/test/nodes/core/parsers/70-CSV_spec.js index b7a240451..98a842e30 100644 --- a/test/nodes/core/parsers/70-CSV_spec.js +++ b/test/nodes/core/parsers/70-CSV_spec.js @@ -119,7 +119,7 @@ describe('CSV node', function() { msg.should.have.property('payload', { a: 1, b: -2, c: '+3', d: 4, e: -5, f: 'ab"cd', g: 'with,a,comma' }); done(); }); - var testString = '"1","-2","+3","04","-05",ab""cd,"with,a,comma"'+String.fromCharCode(10); + var testString = '"1","-2","+3","04","-05","ab""cd","with,a,comma"'+String.fromCharCode(10); n1.emit("input", {payload:testString}); }); }); @@ -132,12 +132,11 @@ describe('CSV node', function() { var n2 = helper.getNode("n2"); n2.on("input", function(msg) { //console.log(msg); - msg.should.have.property('payload', { a: 1, b: -2, c: '+3', d: 4, e: -5, f: 'ab"cd', g: 'with,a,comma' }); + msg.should.have.property('payload', { a: "with,an", b: "odd,number", c: "ofquotes" }); + //msg.should.have.property('payload', { a: 1, b: -2, c: '+3', d: 4, e: -5, f: 'ab"cd', g: 'with,a,comma' }); done(); }); - var testString = '"with,a"n,odd",num"ber,of"quotes'+String.fromCharCode(10); - n1.emit("input", {payload:testString}); - testString = '"1","-2","+3","04","-05",ab""cd,"with,a,comma"'+String.fromCharCode(10); + var testString = '"with,a"n,odd","num"ber","of"qu"ot"es"'+String.fromCharCode(10); n1.emit("input", {payload:testString}); }); });