From 01afb18d3022abbe5c8fccd1811f699418941116 Mon Sep 17 00:00:00 2001 From: Keith Collins Date: Wed, 9 Jul 2014 18:17:48 -0400 Subject: [PATCH 1/4] added header row to CSV output this takes the keys from the json input and creates a header row out of them, if they exist. --- lib/express-csv.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/express-csv.js b/lib/express-csv.js index fcc1d69..533cbb8 100644 --- a/lib/express-csv.js +++ b/lib/express-csv.js @@ -98,7 +98,13 @@ res.csv = function(obj, headers, status) { this.charset = this.charset || 'utf-8'; this.header('Content-Type', 'text/csv'); - obj.forEach(function(item) { + obj.forEach(function(item,i) { + if (i == 0) { + var keys = Object.keys(item); + if (Object.prototype.toString.call(keys) === '[object Array]' && keys.length > 0) { + body += keys.map(escape).join(exports.separator) + '\r\n'; + } + } if (!(item instanceof Array)) item = objToArray(item); body += item.map(escape).join(exports.separator) + '\r\n'; }); From 742d343681fe97da7b0ab4baebe33b720439d362 Mon Sep 17 00:00:00 2001 From: Keith Collins Date: Thu, 10 Jul 2014 12:01:21 -0400 Subject: [PATCH 2/4] Update express-csv.js --- lib/express-csv.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/express-csv.js b/lib/express-csv.js index 533cbb8..5e080db 100644 --- a/lib/express-csv.js +++ b/lib/express-csv.js @@ -101,7 +101,7 @@ res.csv = function(obj, headers, status) { obj.forEach(function(item,i) { if (i == 0) { var keys = Object.keys(item); - if (Object.prototype.toString.call(keys) === '[object Array]' && keys.length > 0) { + if (typeof keys !== "undefined" && Object.prototype.toString.call(keys) === '[object Array]' && keys.length > 0) { body += keys.map(escape).join(exports.separator) + '\r\n'; } } From 4c0fc769ca4e28b3691e465401f90c67a23758e2 Mon Sep 17 00:00:00 2001 From: Keith Collins Date: Thu, 10 Jul 2014 12:17:25 -0400 Subject: [PATCH 3/4] Update express-csv.js making some changes to pass test --- lib/express-csv.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/express-csv.js b/lib/express-csv.js index 5e080db..edc6bf3 100644 --- a/lib/express-csv.js +++ b/lib/express-csv.js @@ -99,13 +99,13 @@ res.csv = function(obj, headers, status) { this.header('Content-Type', 'text/csv'); obj.forEach(function(item,i) { + if (!(item instanceof Array)) item = objToArray(item); if (i == 0) { var keys = Object.keys(item); - if (typeof keys !== "undefined" && Object.prototype.toString.call(keys) === '[object Array]' && keys.length > 0) { + if (typeof keys !== "undefined") { body += keys.map(escape).join(exports.separator) + '\r\n'; } } - if (!(item instanceof Array)) item = objToArray(item); body += item.map(escape).join(exports.separator) + '\r\n'; }); From b0d7a10c829c7a6a9025346803dd251a108dd637 Mon Sep 17 00:00:00 2001 From: Keith Collins Date: Thu, 10 Jul 2014 13:00:56 -0400 Subject: [PATCH 4/4] Update express-csv.js another change to pass testing --- lib/express-csv.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/express-csv.js b/lib/express-csv.js index edc6bf3..9e75d31 100644 --- a/lib/express-csv.js +++ b/lib/express-csv.js @@ -99,13 +99,13 @@ res.csv = function(obj, headers, status) { this.header('Content-Type', 'text/csv'); obj.forEach(function(item,i) { - if (!(item instanceof Array)) item = objToArray(item); - if (i == 0) { - var keys = Object.keys(item); - if (typeof keys !== "undefined") { + if (i == 0 && item instanceof Object) { + keys = Object.keys(item); + if (typeof keys !== "undefined" && keys instanceof Array && keys.length > 0) { body += keys.map(escape).join(exports.separator) + '\r\n'; } } + if (!(item instanceof Array)) item = objToArray(item); body += item.map(escape).join(exports.separator) + '\r\n'; });