Skip to content

Commit

Permalink
move csv fixes to master
Browse files Browse the repository at this point in the history
to fix #1142 in master
  • Loading branch information
Dave Conway-Jones committed Feb 22, 2017
1 parent e2a9be9 commit a625eee
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
29 changes: 26 additions & 3 deletions nodes/core/parsers/70-CSV.js
Expand Up @@ -28,6 +28,7 @@ module.exports = function(RED) {
this.hdrin = n.hdrin || false;
this.hdrout = n.hdrout || false;
this.goodtmpl = true;
var tmpwarn = true;
var node = this;

// pass in an array of column names to be trimed, de-quoted and retrimed
Expand Down Expand Up @@ -71,7 +72,27 @@ module.exports = function(RED) {
}
else {
if ((node.template.length === 1) && (node.template[0] === '')) {
node.warn(RED._("csv.errors.obj_csv"));
if (tmpwarn === true) { // just warn about missing template once
node.warn(RED._("csv.errors.obj_csv"));
tmpwarn = false;
}
ou = "";
for (var p in msg.payload[0]) {
if (msg.payload[0].hasOwnProperty(p)) {
if (typeof msg.payload[0][p] !== "object") {
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
}
}
}
ou = ou.slice(0,-1) + node.ret;
}
else {
for (var t=0; t < node.template.length; t++) {
Expand Down Expand Up @@ -112,7 +133,7 @@ module.exports = function(RED) {
var first = true; // is this the first line
var line = msg.payload;
var tmp = "";
var reg = new RegExp("^[-]?[0-9.]*[\.]?[0-9]*$");
var reg = /^[-]?[0-9]*\.?[0-9]+$/;

// For now we are just going to assume that any \r or \n means an end of line...
// got to be a weird csv that has singleton \r \n in it for another reason...
Expand All @@ -129,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
Expand Down
24 changes: 19 additions & 5 deletions test/nodes/core/parsers/70-CSV_spec.js
Expand Up @@ -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});
});
});
Expand All @@ -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});
});
});
Expand Down Expand Up @@ -179,6 +178,21 @@ describe('CSV node', function() {
n1.emit("input", {payload:testString});
});
});

it('should handle numbers in strings but not IP addresses', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,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) {
msg.should.have.property('payload', { a: "a", b: "127.0.0.1", c: 56.7, d: -32.8, e: "+76.22C" });
done();
});
var testString = "a,127.0.0.1,56.7,-32.8,+76.22C";
n1.emit("input", {payload:testString});
});
});
});

describe('json object to csv', function() {
Expand Down

0 comments on commit a625eee

Please sign in to comment.