From 61d78934671b0847de6947ab6b17ba3c11d6dcf1 Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Wed, 14 Oct 2015 23:07:25 +0100 Subject: [PATCH] Enhance CSV node to accept simple arrays -> css --- nodes/core/parsers/70-CSV.js | 46 ++++++++++++++-------- test/nodes/core/parsers/70-CSV_spec.js | 54 ++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 16 deletions(-) diff --git a/nodes/core/parsers/70-CSV.js b/nodes/core/parsers/70-CSV.js index 8a0295c8f..5aa4787b4 100644 --- a/nodes/core/parsers/70-CSV.js +++ b/nodes/core/parsers/70-CSV.js @@ -1,5 +1,5 @@ /** - * Copyright 2014 IBM Corp. + * Copyright 2014,2015 IBM Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,23 +55,37 @@ module.exports = function(RED) { } if (!Array.isArray(msg.payload)) { msg.payload = [ msg.payload ]; } for (var s = 0; s < msg.payload.length; s++) { - for (var t=0; t < node.template.length; t++) { - - // aaargh - resorting to eval here - but fairly contained front and back. - var p = RED.util.ensureString(eval("msg.payload[s]."+node.template[t])); - - if (p === "undefined") { p = ""; } - if (p.indexOf(node.quo) !== -1) { // add double quotes if any quotes - console.log("ping"); - p = p.replace(/"/g, '""'); - ou += node.quo + p + node.quo + node.sep; + if ((Array.isArray(msg.payload[s])) || (typeof msg.payload[s] !== "object")) { + if (typeof msg.payload[s] !== "object") { msg.payload = [ msg.payload ]; } + for (var t = 0; t < msg.payload[s].length; t++) { + if (msg.payload[s][t].toString().indexOf(node.quo) !== -1) { // add double quotes if any quotes + msg.payload[s][t] = msg.payload[s][t].toString().replace(/"/g, '""'); + msg.payload[s][t] = node.quo + msg.payload[s][t].toString() + node.quo; + } + 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; + } } - else if (p.indexOf(node.sep) !== -1) { // add quotes if any "commas" - ou += node.quo + p + node.quo + node.sep; - } - else { ou += p + node.sep; } // otherwise just add + ou += msg.payload[s].join(node.sep) + node.ret; + } + else { + for (var t=0; t < node.template.length; t++) { + + // aaargh - resorting to eval here - but fairly contained front and back. + var p = RED.util.ensureString(eval("msg.payload[s]."+node.template[t])); + + if (p === "undefined") { p = ""; } + if (p.indexOf(node.quo) !== -1) { // add double quotes if any quotes + p = p.replace(/"/g, '""'); + ou += node.quo + p + node.quo + node.sep; + } + else if (p.indexOf(node.sep) !== -1) { // add quotes if any "commas" + ou += node.quo + p + node.quo + node.sep; + } + else { ou += p + node.sep; } // otherwise just add + } + ou = ou.slice(0,-1) + node.ret; // remove final "comma" and add "newline" } - ou = ou.slice(0,-1) + node.ret; // remove final "comma" and add "newline" } msg.payload = ou; node.send(msg); diff --git a/test/nodes/core/parsers/70-CSV_spec.js b/test/nodes/core/parsers/70-CSV_spec.js index 284f2b411..3db624271 100644 --- a/test/nodes/core/parsers/70-CSV_spec.js +++ b/test/nodes/core/parsers/70-CSV_spec.js @@ -183,6 +183,60 @@ describe('CSV node', function() { }); }); + it('should convert an array of objects to a multi-line csv', function(done) { + var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", 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\n1,2,3,4\n'); + done(); + } + catch(e) { done(e); } + }); + var testJson = [{ d: 1, b: 3, c: 2, a: 4 },{d:4,a:1,c:3,b:2}]; + n1.emit("input", {payload:testJson}); + }); + }); + + it('should convert a simple array back to a csv', function(done) { + var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", 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,2,3,4\n'); + done(); + } + catch(e) { done(e); } + }); + var testJson = [1,2,3,4]; + n1.emit("input", {payload:testJson}); + }); + }); + + it('should convert aan array of arrays back to a multi-line csv', function(done) { + var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", 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,2,3,4\n4,3,2,1\n'); + done(); + } + catch(e) { done(e); } + }); + var testJson = [[1,2,3,4],[4,3,2,1]]; + n1.emit("input", {payload:testJson}); + }); + }); + it('should be able to include column names as first row', function(done) { var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", hdrout:true, ret:"\r\n", wires:[["n2"]] }, {id:"n2", type:"helper"} ];