From d04d90bd7f4283ea1151e557f9ec2e59ff25d8cb Mon Sep 17 00:00:00 2001 From: Alex Bowyer Date: Tue, 17 Mar 2015 14:14:41 +0000 Subject: [PATCH 1/2] Fix issues with JSON handling --- lib/jsoncsv.js | 14 +++++++++----- src/jsoncsv.coffee | 9 +++++---- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/jsoncsv.js b/lib/jsoncsv.js index 8523155..d86fc9c 100644 --- a/lib/jsoncsv.js +++ b/lib/jsoncsv.js @@ -8,15 +8,15 @@ __extends(JsonCsv, _super); function JsonCsv() { - JsonCsv.__super__.constructor.apply(this, arguments); + return JsonCsv.__super__.constructor.apply(this, arguments); } JsonCsv.prototype.parse = function(data, columns, cb) { var column, csv, field, i, index, j, json, line, _i, _j, _len, _len2; if (data == null) return cb('error no data'); if (columns == null) return cb('error please provide columns as options'); - data = JSON.stringify(data); - json = JSON.parse(data); + data = JSON.parse(data); + json = data[Object.keys(data)[0]]; for (_i = 0, _len = json.length; _i < _len; _i++) { j = json[_i]; delete j['_events']; @@ -37,11 +37,15 @@ } } else { if (json[i] !== void 0) { - if (json[i][index] !== void 0) line += json[i][index] + ","; + if (json[i][index] !== void 0) line += json[i][index]; + if (columns.slice(-1)[0]!=index) { + line += ","; + } } - i = i + 1; + } } + i = i + 1; line += '\r\n'; } line.slice(0, line.Length - 1); diff --git a/src/jsoncsv.coffee b/src/jsoncsv.coffee index f5d688f..8a9c5a9 100644 --- a/src/jsoncsv.coffee +++ b/src/jsoncsv.coffee @@ -8,8 +8,8 @@ class JsonCsv extends require('events').EventEmitter # columns should be array of json nodes return cb('error no data') unless data? return cb('error please provide columns as options') unless columns? - data = JSON.stringify(data) - json = JSON.parse(data) + data = JSON.parse(data) + json = data[Object.keys(data)[0]] for j in json delete j['_events'] delete j['_id'] @@ -26,8 +26,9 @@ class JsonCsv extends require('events').EventEmitter line += "," if field == null or field == undefined else if json[i] != undefined - line += json[i][index] + "," if json[i][index] != undefined - i = i + 1 + line += json[i][index] if json[i][index] != undefined + line += "," if columns.slice(-1)[0] != index + i = i + 1 line += '\r\n' line.slice 0, line.Length - 1 From 80603bddc5ee1152ce3263ee5023d0ed89609cbb Mon Sep 17 00:00:00 2001 From: Alex Bowyer Date: Tue, 17 Mar 2015 18:20:21 +0000 Subject: [PATCH 2/2] add handling for 1348dates, and other improvements --- lib/jsoncsv.js | 89 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 67 insertions(+), 22 deletions(-) diff --git a/lib/jsoncsv.js b/lib/jsoncsv.js index d86fc9c..de8a7b2 100644 --- a/lib/jsoncsv.js +++ b/lib/jsoncsv.js @@ -1,7 +1,19 @@ (function() { var JsonCsv, __hasProp = Object.prototype.hasOwnProperty, - __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; }; + __extends = function(child, parent) { + for (var key in parent) { + if (__hasProp.call(parent, key)) child[key] = parent[key]; + } + + function ctor() { + this.constructor = child; + } + ctor.prototype = parent.prototype; + child.prototype = new ctor; + child.__super__ = parent.prototype; + return child; + }; JsonCsv = (function(_super) { @@ -23,33 +35,66 @@ delete j['_id']; delete j['id']; } - csv = ''; - line = columns.join(',') + '\r\n'; + csv = columns.join(',') + '\r\n'; i = 0; while (i < json.length) { - for (index in json[i]) { - if (json[i][index] !== void 0 && typeof json[i][index] === 'object') { - for (_j = 0, _len2 = columns.length; _j < _len2; _j++) { - column = columns[_j]; - field = json[i][index]["" + column]; - if (field !== void 0) line += field + ","; - if (field === null || field === void 0) line += ","; - } - } else { - if (json[i] !== void 0) { - if (json[i][index] !== void 0) line += json[i][index]; - if (columns.slice(-1)[0]!=index) { - line += ","; + line = ""; + if (typeof json[i] != "undefined") { + var jsonObj = json[i]; + for (var ci = 0; ci < columns.length; ci++) { + column = columns[ci]; + if (jsonObj.hasOwnProperty(column)) { + if (typeof jsonObj[column] != "undefined") { + var fieldValue = jsonObj[column]; + if (typeof fieldValue === 'object') { + if (column == "time") { + firstKey = Object.keys(fieldValue)[0]; + if (firstKey == "$$date") { + var timestamp = new Date(fieldValue[firstKey]); + var year = timestamp.getFullYear(); + var month = timestamp.getMonth() + 1; + if (month < 10) { + month = ("0" + month).slice(-2); + } + var date = timestamp.getDate(); + if (date < 10) { + date = ("0" + date).slice(-2); + } + var hour = timestamp.getHours(); + if (hour < 10) { + hour = ("0" + hour).slice(-2); + } + var min = timestamp.getMinutes(); + if (min < 10) { + min = ("0" + min).slice(-2); + }; + var sec = timestamp.getSeconds(); + if (sec < 10) { + sec = ("0" + sec).slice(-2); + }; + var msec = timestamp.getMilliseconds(); + if (msec < 10) { + msec = (msec + "00").slice(-3); + } else { + if (msec < 100) { + msec = (msec + "0").slice(-3); + } + } + fieldValue = year + '-' + month + '-' + date + 'T' + hour + ':' + min + ':' + sec + '.' + msec + 'Z'; + } + } + } + line += fieldValue; } } - + line += ","; } } - i = i + 1; - line += '\r\n'; + i++; + line = line.slice(0, - 1); + csv += line + "\r\n"; } - line.slice(0, line.Length - 1); - csv += line + "\r\n"; + return cb(null, csv); }; @@ -59,4 +104,4 @@ module.exports = new JsonCsv(); -}).call(this); +}).call(this); \ No newline at end of file