Skip to content

Commit 08c6003

Browse files
committed
Merge branch 'master' of https://github.com/mrodrig/json-2-csv
2 parents fef33c6 + b421bd0 commit 08c6003

File tree

6 files changed

+119
-30
lines changed

6 files changed

+119
-30
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-2-csv",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"homepage": "https://github.com/mrodrig/json-2-csv",
55
"moduleType": [
66
"node"

lib/constants.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"WRAP" : ""
2727
},
2828
"EOL" : "\n",
29+
"PREPEND_HEADER" : true,
2930
"PARSE_CSV_NUMBERS" : false,
3031
"KEYS" : null
3132
}

lib/json-2-csv.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,19 @@ module.exports = {
146146
generateHeading(data)
147147
.then(_.partial(generateCsv, data))
148148
.spread(function (csvHeading, csvData) {
149-
if (options.DELIMITER.WRAP) {
149+
// If the fields are supposed to be wrapped... (only perform this if we are actually prepending the header)
150+
if (options.DELIMITER.WRAP && options.PREPEND_HEADER) {
150151
csvHeading = _.map(csvHeading, function(headingKey) {
151152
return options.DELIMITER.WRAP + headingKey + options.DELIMITER.WRAP;
152153
});
153154
}
154-
csvHeading = csvHeading.join(options.DELIMITER.FIELD);
155-
return callback(null, [csvHeading, csvData].join(options.EOL));
155+
// If we are prepending the header, then join the csvHeading fields
156+
if (options.PREPEND_HEADER) {
157+
csvHeading = csvHeading.join(options.DELIMITER.FIELD);
158+
}
159+
160+
// If we are prepending the header, then join the header and data by EOL, otherwise just return the data
161+
return callback(null, options.PREPEND_HEADER ? csvHeading + options.EOL + csvData : csvData);
156162
})
157163
.catch(function (err) {
158164
return callback(err);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"author": "mrodrig",
33
"name": "json-2-csv",
44
"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
5-
"version": "1.2.0",
5+
"version": "1.2.1",
66
"repository": {
77
"type": "git",
88
"url": "http://github.com/mrodrig/json-2-csv.git"

test.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/**
2-
* Created by mrodrigues on 2/17/15.
3-
*/
41
var Promise = require('bluebird'),
52
converter = Promise.promisifyAll(require('./lib/converter'));
63

@@ -29,22 +26,23 @@ var csv = 'Make,Model,Year,Specifications.Mileage,Specifications.Trim\n' +
2926
'Nissan,Murano,2013,7106,S AWD\n' +
3027
'BMW,X5,2014,3287,M';
3128

32-
//converter.json2csv(documents, function (err, csv) {
33-
// if (!err) {
34-
// return console.log('csv', csv);
35-
// }
36-
// return console.log('err', err);
37-
// },
38-
// {
39-
// DELIMITER : {
40-
// FIELD : ',',
41-
// ARRAY : '/',
42-
// WRAP : '\"'
43-
// },
44-
// EOL : '\n',
45-
// PARSE_CSV_NUMBERS : false,
46-
// KEYS: ['Make', 'Model', 'Specifications.Mileage']
47-
// });
29+
converter.json2csv(documents, function (err, csv) {
30+
if (!err) {
31+
return console.log('csv', csv);
32+
}
33+
return console.log('err', err);
34+
},
35+
{
36+
DELIMITER : {
37+
FIELD : ',',
38+
ARRAY : '/',
39+
WRAP : '\"'
40+
},
41+
EOL : '\n',
42+
PREPEND_HEADER : false,
43+
PARSE_CSV_NUMBERS : false,
44+
KEYS: ['Make', 'Model', 'Specifications.Mileage']
45+
});
4846

4947
//converter.json2csvAsync(documents, {})
5048
// .then(function (csv) {
@@ -54,12 +52,12 @@ var csv = 'Make,Model,Year,Specifications.Mileage,Specifications.Trim\n' +
5452
// console.log(err.stack);
5553
// });
5654

57-
converter.csv2json(csv, function (err, json) {
58-
if (!err) {
59-
return console.log('json', json);
60-
}
61-
return console.log('err', err);
62-
}, {KEYS: ['Model', 'Specifications.Mileage']});
55+
//converter.csv2json(csv, function (err, json) {
56+
// if (!err) {
57+
// return console.log('json', json);
58+
// }
59+
// return console.log('err', err);
60+
//}, {KEYS: ['Model', 'Specifications.Mileage']});
6361

6462
//converter.csv2jsonAsync(csv, {})
6563
// .then(function (json) {

test/testJson2Csv.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,19 @@ var json2csvTests = function () {
270270
}, options);
271271
});
272272

273+
it('should repress the heading', function (done) {
274+
opts = JSON.parse(JSON.stringify(options));
275+
opts.PREPEND_HEADER = false;
276+
277+
converter.json2csv(jsonTestData.sameSchemaDifferentOrdering, function (err, csv) {
278+
if (err) { throw err; }
279+
true.should.equal(_.isEqual(err, null));
280+
csv.should.equal(csvTestData.unQuoted.regularJson.split(options.EOL).slice(1).join(options.EOL));
281+
csv.split(options.EOL).length.should.equal(5);
282+
done();
283+
}, opts);
284+
});
285+
273286
it('should throw an error if the documents do not have the same schema', function (done) {
274287
converter.json2csv(jsonTestData.differentSchemas, function (err, csv) {
275288
err.message.should.equal(constants.Errors.json2csv.notSameSchema);
@@ -433,6 +446,19 @@ var json2csvTests = function () {
433446
}, options);
434447
});
435448

449+
it('should repress the heading', function (done) {
450+
opts = JSON.parse(JSON.stringify(options));
451+
opts.PREPEND_HEADER = false;
452+
453+
converter.json2csv(jsonTestData.sameSchemaDifferentOrdering, function (err, csv) {
454+
if (err) { throw err; }
455+
true.should.equal(_.isEqual(err, null));
456+
csv.should.equal(csvTestData.unQuoted.regularJson.replace(/,/g, options.DELIMITER.FIELD).split(options.EOL).slice(1).join(options.EOL));
457+
csv.split(options.EOL).length.should.equal(5);
458+
done();
459+
}, opts);
460+
});
461+
436462
it('should throw an error if the documents do not have the same schema', function (done) {
437463
converter.json2csv(jsonTestData.differentSchemas, function (err, csv) {
438464
err.message.should.equal(constants.Errors.json2csv.notSameSchema);
@@ -606,6 +632,19 @@ var json2csvTests = function () {
606632
}, options);
607633
});
608634

635+
it('should repress the heading', function (done) {
636+
opts = JSON.parse(JSON.stringify(options));
637+
opts.PREPEND_HEADER = false;
638+
639+
converter.json2csv(jsonTestData.sameSchemaDifferentOrdering, function (err, csv) {
640+
if (err) { throw err; }
641+
true.should.equal(_.isEqual(err, null));
642+
csv.should.equal(csvTestData.quoted.regularJson.split(options.EOL).slice(1).join(options.EOL));
643+
csv.split(options.EOL).length.should.equal(5);
644+
done();
645+
}, opts);
646+
});
647+
609648
it('should throw an error if the documents do not have the same schema', function (done) {
610649
converter.json2csv(jsonTestData.differentSchemas, function (err, csv) {
611650
err.message.should.equal(constants.Errors.json2csv.notSameSchema);
@@ -966,6 +1005,21 @@ var json2csvTests = function () {
9661005
});
9671006
});
9681007

1008+
it('should repress the heading', function (done) {
1009+
opts = JSON.parse(JSON.stringify(options));
1010+
opts.PREPEND_HEADER = false;
1011+
1012+
converter.json2csvAsync(jsonTestData.sameSchemaDifferentOrdering, opts)
1013+
.then(function (csv) {
1014+
csv.should.equal(csvTestData.unQuoted.regularJson.split(options.EOL).slice(1).join(options.EOL));
1015+
csv.split(options.EOL).length.should.equal(5);
1016+
done();
1017+
})
1018+
.catch(function (err) {
1019+
throw err;
1020+
});
1021+
});
1022+
9691023
it('should throw an error if the documents do not have the same schema', function (done) {
9701024
converter.json2csvAsync(jsonTestData.differentSchemas, options)
9711025
.then(function (csv) {
@@ -1123,6 +1177,21 @@ var json2csvTests = function () {
11231177
});
11241178
});
11251179

1180+
it('should repress the heading', function (done) {
1181+
opts = JSON.parse(JSON.stringify(options));
1182+
opts.PREPEND_HEADER = false;
1183+
1184+
converter.json2csvAsync(jsonTestData.sameSchemaDifferentOrdering, opts)
1185+
.then(function (csv) {
1186+
csv.should.equal(csvTestData.unQuoted.regularJson.replace(/,/g, options.DELIMITER.FIELD).split(options.EOL).slice(1).join(options.EOL));
1187+
csv.split(options.EOL).length.should.equal(5);
1188+
done();
1189+
})
1190+
.catch(function (err) {
1191+
throw err;
1192+
});
1193+
});
1194+
11261195
it('should throw an error if the documents do not have the same schema', function (done) {
11271196
converter.json2csvAsync(jsonTestData.differentSchemas, options)
11281197
.then(function (csv) {
@@ -1292,6 +1361,21 @@ var json2csvTests = function () {
12921361
});
12931362
});
12941363

1364+
it('should repress the heading', function (done) {
1365+
opts = JSON.parse(JSON.stringify(options));
1366+
opts.PREPEND_HEADER = false;
1367+
1368+
converter.json2csvAsync(jsonTestData.sameSchemaDifferentOrdering, opts)
1369+
.then(function (csv) {
1370+
csv.should.equal(csvTestData.quoted.regularJson.split(options.EOL).slice(1).join(options.EOL));
1371+
csv.split(options.EOL).length.should.equal(5);
1372+
done();
1373+
})
1374+
.catch(function (err) {
1375+
throw err;
1376+
});
1377+
});
1378+
12951379
it('should throw an error if the documents do not have the same schema', function (done) {
12961380
converter.json2csvAsync(jsonTestData.differentSchemas, options)
12971381
.then(function (csv) {

0 commit comments

Comments
 (0)