Permalink
Browse files

Merge pull request #34 from hampelm/master

Actually fixes undefined records in CSV exports
  • Loading branch information...
2 parents 626efc8 + d617718 commit f8b6a3667af706af4e0bcc18a0a038149dd55873 @hampelm hampelm committed Nov 8, 2012
Showing with 24 additions and 9 deletions.
  1. +20 −6 responses.js
  2. +2 −2 test/test.export_csv.js
  3. +2 −1 test/test.parcels.js
View
@@ -47,13 +47,14 @@ function listToCSVString(row, headers, maxEltsInCell) {
for (i = 0; i < row.length; i += 1) {
if (maxEltsInCell[headers[i]] === 1) {
- if (row[i] === undefined) {
+ // Check if the value is undefined
+ if(row[i] === undefined) {
row[i] = '';
- };
+ }
// Check if we need to escape the value
row[i] = String(row[i]);
- if(row[i].indexOf(",") !== -1){
+ if(row[i].indexOf(',') !== -1){
row[i] = '"' + row[i] + '"';
}
@@ -64,12 +65,26 @@ function listToCSVString(row, headers, maxEltsInCell) {
// There might be multiple items in this cell.
var len;
if (!isArray(row[i])) {
+
// This row only has one answer in this column, so just push that.
- arr.push(row[i]);
+ // Check first to see if it's an empty value
+ if(row[i] !== undefined) {
+
+ // Check if we need to escape the value
+ row[i] = String(row[i]);
+ if(row[i].indexOf(',') !== -1){
+ row[i] = '"' + row[i] + '"';
+ }
+
+ arr.push(row[i]);
+ }else {
+ arr.push('');
+ }
+
len = 1;
} else {
// If it's an array of responses, join them with a semicolon
- arr.push(row[i].join(";"));
+ arr.push(row[i].join(';'));
}
}
}
@@ -126,7 +141,6 @@ function KMLWriter(response, rows, headers, maxEltsInCell){
console.log(rows);
// Turn each row into a KML line
for (i = 0; i < rows.length; i++) {
- console.log("Writing list");
response.write(listToKMLString(rows[i], headers, maxEltsInCell));
response.write('\n');
}
@@ -6,7 +6,7 @@ var CSVWriter = require('./../responses.js').CSVWriter;
suite('In csvExport,', function(){
- var row = ['a', 2, undefined];
+ var row = ['a', 2, '3', undefined, ""];
var headers = ['first', 'second', 'third'];
var headerCount = {
'first': 1,
@@ -67,7 +67,7 @@ suite('In csvExport,', function(){
test('commasep should turn a simple list into a csv string', function(){
var csv = listToCSVString(row, headers, headerCount);
- var expected = 'a,2,';
+ var expected = 'a,2,3,,';
assert.equal(csv,expected);
});
@@ -20,6 +20,7 @@ function shouldBeParcel(item) {
item.centroid.coordinates.should.have.lengthOf(2);
item.should.have.property('parcelId');
item.should.have.property('polygon');
+ item.should.have.property('type');
item.polygon.should.have.property('type');
item.polygon.type.should.equal('MultiPolygon');
item.polygon.should.have.property('coordinates');
@@ -129,7 +130,7 @@ suite('Parcels', function () {
}, function (error, response, body) {
should.not.exist(error);
response.statusCode.should.equal(200);
- response.should.be.json
+ response.should.be.json;
var parsed = JSON.parse(body);
parsed.should.be.an.instanceOf(Array);

0 comments on commit f8b6a36

Please sign in to comment.