New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

simple-json2csv

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-json2csv - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

test/fixtures/simpleWithDates.csv

29

app/index.js
"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;

5

package.json
{
"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();

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc