Skip to content

Commit

Permalink
More control over CSV parsing & stringifing + node-csv version upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
chrmod committed Jun 29, 2014
1 parent 01dd5aa commit 896d498
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"lodash": "~2.4.1",
"plist": "~0.4.3",
"yamljs": "~0.1.4",
"csv": "~0.3.6",
"csv": "~0.4.0",
"pretty-data": "~0.40.0",
"xml2js": "~0.4.1"
}
Expand Down
25 changes: 11 additions & 14 deletions src/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ var extend = function(options) {
inline: 8,
indent: 2,
csv: {
columns: true,
delimiter: ','
stringify: {},
parse: {
columns: true
}
},
xml: {
renderOpts: {
Expand All @@ -46,22 +48,17 @@ var extend = function(options) {


var convertCsvToJson = function (data, options, done) {
csv()
.from(data, options.csv)
.to.array(function(row) {
var results = JSON.stringify(row, null, options.indent);
done(null, results);
});
csv.parse(data, options.csv.parse, function(err, output) {
var results = JSON.stringify(output, null, options.indent);
done(null, results);
});
};

var convertJsonToCsv = function(data, options, done) {
var results = '';
data = JSON.parse(data);
csv()
.from(data)
.to.string(function(results, count) {
done(null, results);
});
csv.stringify(data, options.csv.stringify, function(err, output) {
done(null, output);
});
};

var convertXmlToJson = function(data, options, done) {
Expand Down
2 changes: 1 addition & 1 deletion test/actual/json2csv.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
21,Brian,Woodward
35,Mikko,Tapionlinna
78,Paul,Armstrong
99,Hariadi,Hinta
99,Hariadi,Hinta
6 changes: 6 additions & 0 deletions test/actual/json2csvWithHeader.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
id,lastname,firstname
82,Jon,Schlinkert
21,Brian,Woodward
35,Mikko,Tapionlinna
78,Paul,Armstrong
99,Hariadi,Hinta
22 changes: 20 additions & 2 deletions test/convert_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ var createFileStreams = function(filename, options) {
describe('convert', function() {

it('should create new stream', function(done) {
var options = { csv: { parse: { columns: null } } };
var expected = '[\n [\n \"beep\"\n ]\n]';
var reader = createReader(['beep']);
var writer = createWriter(function() {
expect(writer.toString()).to.eql(expected);
done();
});

reader.pipe(convert()).pipe(writer);
reader.pipe(convert(options)).pipe(writer);
});


Expand Down Expand Up @@ -102,6 +103,23 @@ describe('convert', function() {
done();
});

it('should convert json to csv with header', function(done) {
var options = {
from: 'json',
to: 'csv',
csv: {
stringify: {
header: true
}
}
};
var files = createFileStreams('json2csvWithHeader', options);
files.reader
.pipe(convert(options))
.pipe(files.writer);
done();
});

it('should convert json to yml', function(done) {
var options = {
from: 'json',
Expand Down Expand Up @@ -137,7 +155,7 @@ describe('convert', function() {
.pipe(files.writer);
done();
});

it('should convert yml to json', function(done) {
var options = {
from: 'yml',
Expand Down
27 changes: 27 additions & 0 deletions test/fixtures/json2csvWithHeader.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"id": "82",
"lastname": "Jon",
"firstname": "Schlinkert"
},
{
"id": "21",
"lastname": "Brian",
"firstname": "Woodward"
},
{
"id": "35",
"lastname": "Mikko",
"firstname": "Tapionlinna"
},
{
"id": "78",
"lastname": "Paul",
"firstname": "Armstrong"
},
{
"id": "99",
"lastname": "Hariadi",
"firstname": "Hinta"
}
]

0 comments on commit 896d498

Please sign in to comment.