Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 71 additions & 22 deletions lib/jsoncsv.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,100 @@
(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) {

__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'];
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] + ",";
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;
}
}
i = i + 1;
line += ",";
}
}
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);
};

Expand All @@ -55,4 +104,4 @@

module.exports = new JsonCsv();

}).call(this);
}).call(this);
9 changes: 5 additions & 4 deletions src/jsoncsv.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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
Expand Down