simple-json2csv
Advanced tools
Comparing version 0.0.2 to 0.0.3
"use strict"; | ||
// polyfills | ||
require('date-utils'); | ||
var Readable = require('stream').Readable || require('readable-stream'), | ||
@@ -7,2 +10,7 @@ util = require('util'), | ||
// constants | ||
var DATE_FORMAT = 'MMM-DD-YYYY'; | ||
/** | ||
@@ -63,3 +71,3 @@ * Creates a new Json2Csv instance. | ||
var headers = this._fields.map(function(field) { | ||
return field.header || field.name; | ||
return '"' + (field.header || field.name) + '"'; | ||
}); | ||
@@ -79,4 +87,5 @@ | ||
var data = []; | ||
var self = this; | ||
this._fields.forEach(function(field) { | ||
data.push(row[field.name]); | ||
data.push(self._format(row[field.name])); | ||
}); | ||
@@ -88,3 +97,3 @@ | ||
Json2Csv.prototype._sendData = function(data) { | ||
var ret = this.push('"' + data.join('","') + '"' + os.EOL); | ||
var ret = this.push(data.join(',') + os.EOL); | ||
@@ -96,2 +105,16 @@ // keep a count of lines written | ||
Json2Csv.prototype._format = function(item) { | ||
if (_.isFunction(item)) { | ||
return this._format(item.call(null)); | ||
} else if (_.isDate(item)) { | ||
return '"' + item.toFormat(DATE_FORMAT) + '"'; | ||
} else if (_.isNumber(item)) { | ||
return item; | ||
} else if (!item) { | ||
return '""'; | ||
} else { | ||
return '"' + item + '"'; | ||
} | ||
}; | ||
module.exports = Json2Csv; |
{ | ||
"name": "simple-json2csv", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "A simple json to csv converter nodejs module", | ||
@@ -29,3 +29,4 @@ "main": "index.js", | ||
"readable-stream": "~1.1.9", | ||
"underscore": "~1.5.2" | ||
"underscore": "~1.5.2", | ||
"date-utils": "~1.2.14" | ||
}, | ||
@@ -32,0 +33,0 @@ "devDependencies": { |
@@ -32,4 +32,4 @@ # simple-json2csv | ||
fields: [ | ||
{ field: "name", header: "Name" }, | ||
{ field: "email", header: "Email Address" } | ||
{ name: "name", header: "Name" }, | ||
{ name: "email", header: "Email Address" } | ||
], | ||
@@ -36,0 +36,0 @@ data: [ |
@@ -22,2 +22,3 @@ "use strict"; | ||
it('should apply transformation and print csv', function(done) { | ||
// setup | ||
this.options.transform = function(row) { | ||
@@ -27,4 +28,7 @@ row.email = row.email.replace('domain','google'); | ||
}; | ||
// test | ||
var json2Csv = new SimpleJson2Csv(this.options); | ||
collect(json2Csv, function(csv) { | ||
// verify | ||
expect(csv).to.equal(fs.readFileSync(path.join(__dirname, './fixtures/simpleTransform.csv')).toString()); | ||
@@ -35,4 +39,21 @@ done(); | ||
it('should apply formatting and print csv', function(done) { | ||
// setup | ||
this.options.fields.push({ name: 'dob', header: 'Birthday'}); | ||
this.options.data.map(function(row) { | ||
row.dob = new Date(Date.parse('Oct-08-1956', 'MMM-dd-yyyy')); | ||
return row; | ||
}); | ||
// test | ||
var json2Csv = new SimpleJson2Csv(this.options); | ||
collect(json2Csv, function(csv) { | ||
// verify | ||
expect(csv).to.equal(fs.readFileSync(path.join(__dirname, './fixtures/simpleWithDates.csv')).toString()); | ||
done(); | ||
}); | ||
}); | ||
it('should output csv to a file', function(done) { | ||
// prep | ||
// setup | ||
// XXX: all sync methods so test is faster. not advisable in actual code | ||
@@ -44,5 +65,7 @@ var outCsv = path.join(__dirname, './fixtures/output.csv'); | ||
// test | ||
var json2Csv = new SimpleJson2Csv(this.options); | ||
var out = fs.createWriteStream(outCsv); | ||
out.on('close', function() { | ||
// verify | ||
expect(fs.readFileSync(outCsv).toString()) | ||
@@ -62,5 +85,9 @@ .to.equal(fs.readFileSync(path.join(__dirname, './fixtures/simple.csv')).toString()); | ||
it('should handle string fields', function(done) { | ||
// setup | ||
this.options.fields = [ 'name', 'email']; | ||
// test | ||
var json2Csv = new SimpleJson2Csv(this.options); | ||
collect(json2Csv, function(csv) { | ||
// verify | ||
expect(csv).to.equal(fs.readFileSync(path.join(__dirname, './fixtures/simpleNoHeaders.csv')).toString()); | ||
@@ -67,0 +94,0 @@ done(); |
AI-detected possible typosquat
Supply chain riskAI has identified this package as a potential typosquat of a more popular package. This suggests that the package may be intentionally mimicking another package's name, description, or other metadata.
Found 1 instance in 1 package
14161
15
250
0
3
+ Addeddate-utils@~1.2.14
+ Addeddate-utils@1.2.21(transitive)