mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Enhance CSV node to accept simple arrays -> css
This commit is contained in:
parent
8f26c01f4b
commit
61d7893467
@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright 2014 IBM Corp.
|
* Copyright 2014,2015 IBM Corp.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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 ]; }
|
if (!Array.isArray(msg.payload)) { msg.payload = [ msg.payload ]; }
|
||||||
for (var s = 0; s < msg.payload.length; s++) {
|
for (var s = 0; s < msg.payload.length; s++) {
|
||||||
for (var t=0; t < node.template.length; t++) {
|
if ((Array.isArray(msg.payload[s])) || (typeof msg.payload[s] !== "object")) {
|
||||||
|
if (typeof msg.payload[s] !== "object") { msg.payload = [ msg.payload ]; }
|
||||||
// aaargh - resorting to eval here - but fairly contained front and back.
|
for (var t = 0; t < msg.payload[s].length; t++) {
|
||||||
var p = RED.util.ensureString(eval("msg.payload[s]."+node.template[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, '""');
|
||||||
if (p === "undefined") { p = ""; }
|
msg.payload[s][t] = node.quo + msg.payload[s][t].toString() + node.quo;
|
||||||
if (p.indexOf(node.quo) !== -1) { // add double quotes if any quotes
|
}
|
||||||
console.log("ping");
|
else if (msg.payload[s][t].toString().indexOf(node.sep) !== -1) { // add quotes if any "commas"
|
||||||
p = p.replace(/"/g, '""');
|
msg.payload[s][t] = node.quo + msg.payload[s][t].toString() + node.quo;
|
||||||
ou += node.quo + p + node.quo + node.sep;
|
}
|
||||||
}
|
}
|
||||||
else if (p.indexOf(node.sep) !== -1) { // add quotes if any "commas"
|
ou += msg.payload[s].join(node.sep) + node.ret;
|
||||||
ou += node.quo + p + node.quo + node.sep;
|
}
|
||||||
}
|
else {
|
||||||
else { ou += p + node.sep; } // otherwise just add
|
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;
|
msg.payload = ou;
|
||||||
node.send(msg);
|
node.send(msg);
|
||||||
|
@ -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) {
|
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"]] },
|
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", hdrout:true, ret:"\r\n", wires:[["n2"]] },
|
||||||
{id:"n2", type:"helper"} ];
|
{id:"n2", type:"helper"} ];
|
||||||
|
Loading…
Reference in New Issue
Block a user