From dd47e615eeb5cbb30d34a9fd303329aafda58d13 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Thu, 2 Nov 2017 16:50:13 +0000 Subject: [PATCH] Fix css template with spaces , and add test (#1462) to close #1460 --- nodes/core/parsers/70-CSV.js | 9 ++--- test/nodes/core/parsers/70-CSV_spec.js | 46 +++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/nodes/core/parsers/70-CSV.js b/nodes/core/parsers/70-CSV.js index fa1ece00f..ff272db41 100644 --- a/nodes/core/parsers/70-CSV.js +++ b/nodes/core/parsers/70-CSV.js @@ -78,9 +78,11 @@ module.exports = function(RED) { } ou = ""; for (var p in msg.payload[0]) { + /* istanbul ignore else */ if (msg.payload[0].hasOwnProperty(p)) { + /* istanbul ignore else */ if (typeof msg.payload[0][p] !== "object") { - var q = 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; @@ -100,9 +102,8 @@ module.exports = function(RED) { ou += node.sep; } else { - // aaargh - resorting to eval here - but fairly contained front and back. - var p = RED.util.ensureString(eval("msg.payload[s]."+node.template[t])); - + var p = RED.util.ensureString(RED.util.getMessageProperty(msg,"payload["+s+"]['"+node.template[t]+"']")); + /* istanbul ignore else */ if (p === "undefined") { p = ""; } if (p.indexOf(node.quo) !== -1) { // add double quotes if any quotes p = p.replace(/"/g, '""'); diff --git a/test/nodes/core/parsers/70-CSV_spec.js b/test/nodes/core/parsers/70-CSV_spec.js index 98a842e30..a664d0eab 100644 --- a/test/nodes/core/parsers/70-CSV_spec.js +++ b/test/nodes/core/parsers/70-CSV_spec.js @@ -198,19 +198,55 @@ describe('CSV node', function() { describe('json object to csv', function() { it('should convert a simple object back to a csv', function(done) { - var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] }, + var flow = [ { id:"n1", type:"csv", temp:"a,b,c,,e", 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', '4,3,2,1\n'); + msg.should.have.property('payload', '4,foo,true,,0\n'); done(); } catch(e) { done(e); } }); - var testJson = { d: 1, b: 3, c: 2, a: 4 }; + var testJson = { e:0, d:1, b:"foo", c:true, a:4 }; + n1.emit("input", {payload:testJson}); + }); + }); + + it('should convert a simple object back to a csv with no template', function(done) { + var flow = [ { id:"n1", type:"csv", temp:" ", 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', '1,foo,"ba""r","di,ng"\n'); + done(); + } + catch(e) { done(e); } + }); + var testJson = { d:1, b:"foo", c:"ba\"r", a:"di,ng" }; + n1.emit("input", {payload:testJson}); + }); + }); + + it('should handle a template with spaces in the property names', function(done) { + var flow = [ { id:"n1", type:"csv", temp:"a,b o,c p,,e", 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', '4,foo,true,,0\n'); + done(); + } + catch(e) { done(e); } + }); + var testJson = { e:0, d:1, "b o":"foo", "c p":true, a:4 }; n1.emit("input", {payload:testJson}); }); }); @@ -241,12 +277,12 @@ describe('CSV node', function() { var n2 = helper.getNode("n2"); n2.on("input", function(msg) { try { - msg.should.have.property('payload', '0,1,2,3,4\n'); + msg.should.have.property('payload', ',0,1,foo,"ba""r","di,ng"\n'); done(); } catch(e) { done(e); } }); - var testJson = [0,1,2,3,4]; + var testJson = ["",0,1,"foo",'ba"r','di,ng']; n1.emit("input", {payload:testJson}); }); });