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