Skip to content

Commit bc02b77

Browse files
committed
Merge pull request #27 from mrodrig/move-eol-opt
Migrating options.EOL to options.DELIMITER.EOL
2 parents 44ed379 + e37e665 commit bc02b77

File tree

9 files changed

+139
-280
lines changed

9 files changed

+139
-280
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ var converter = require('json-2-csv');
3434
* `FIELD` - String - Field Delimiter. Default: `','`
3535
* `ARRAY` - String - Array Value Delimiter. Default: `';'`
3636
* `WRAP` - String - Wrap values in the delimiter of choice (e.g. wrap values in quotes). Default: `''`
37-
* `CHECK_SCHEMA_DIFFERENCES` - Boolean - Should we require all documents to have the same schema? Default: `true`
37+
* `EOL` - String - End of Line Delimiter. Default: `'\n'`
3838
* `PREPEND_HEADER` - Boolean - Should the auto-generated header be prepended as the first line in the CSV? Default: `true`
39-
* `SORT_HEADER` - Boolean - Should the auto-generated header be sorted? Default: `false`
40-
* `EOL` - String - End of Line Delimiter. Default: `'\n'`
4139
* `KEYS` - Array - Specify the keys (as strings) that should be converted. Default: `null`
4240
* If you have a nested object (ie. {info : {name: 'Mike'}}), then set options.KEYS to ['info.name']
4341
* If you want all keys to be converted, then specify ```null``` or don't specify the option to utilize the default.
@@ -93,7 +91,7 @@ BMW,X5,2014,3287,M
9391
* `FIELD` - String - Field Delimiter. Default: `','`
9492
* `ARRAY` - String - Array Value Delimiter. Default: `';'`
9593
* `WRAP` - String - The character that field values are wrapped in. Default: `''`
96-
* `EOL` - String - End of Line Delimiter. Default: `'\n'`
94+
* `EOL` - String - End of Line Delimiter. Default: `'\n'`
9795
* `PARSE_CSV_NUMBERS` - Boolean - (TODO) Should numbers that are found in the CSV be converted to numbers? Default: `false`
9896
* `KEYS` - Array - Specify the keys (as strings) that should be converted. Default: `null`
9997
* If you have a nested object (ie. {info : {name: 'Mike'}}), then set options.KEYS to ['info.name']

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.4.0",
3+
"version": "2.0.0",
44
"homepage": "https://github.com/mrodrig/json-2-csv",
55
"moduleType": [
66
"node"

lib/constants.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"DELIMITER" : {
2424
"FIELD" : ",",
2525
"ARRAY" : ";",
26-
"WRAP" : ""
26+
"WRAP" : "",
27+
"EOL" : "\n"
2728
},
28-
"EOL" : "\n",
2929
"PREPEND_HEADER" : true,
3030
"SORT_HEADER" : false,
3131
"PARSE_CSV_NUMBERS" : false,

lib/converter.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var json2Csv = require('./json-2-csv'), // Require our json-2-csv code
44
csv2Json = require('./csv-2-json'), // Require our csv-2-json code
55
constants = require('./constants'), // Require in constants
6-
Promise = require('bluebird'),
6+
docPath = require('doc-path'),
77
_ = require('underscore'); // Require underscore
88

99
/**
@@ -17,6 +17,10 @@ var defaultOptions = constants.DefaultOptions;
1717
* If options are provided, then we set each valid key that was passed
1818
*/
1919
var buildOptions = function (opts, cb) {
20+
// PREVIOUS VERSION SUPPORT (so that future versions are backwards compatible)
21+
// Issue #26: opts.EOL should be opts.DELIMITER.EOL -- this will move the option & provide backwards compatibility
22+
if (docPath.evaluatePath(opts, 'EOL')) { docPath.setPath(opts, 'DELIMITER.EOL'); }
23+
2024
opts = _.defaults(opts || {}, defaultOptions);
2125

2226
// Note: _.defaults does a shallow default, we need to deep copy the DELIMITER object

lib/csv-2-json.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ module.exports = {
205205
}
206206

207207
// Split the CSV into lines using the specified EOL option
208-
var lines = data.split(options.EOL),
208+
var lines = data.split(options.DELIMITER.EOL),
209209
json = convertCSV(lines, callback); // Retrieve the JSON document array
210210
return callback(null, json); // Send the data back to the caller
211211
}

lib/json-2-csv.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ var convertField = function (value) {
150150
var generateCsv = function (data, headingKeys) {
151151
// Reduce each JSON document in data to a CSV string and append it to the CSV accumulator
152152
return [headingKeys].concat(_.reduce(data, function (csv, doc) {
153-
return csv += convertData(doc, headingKeys).join(options.DELIMITER.FIELD) + options.EOL;
153+
return csv += convertData(doc, headingKeys).join(options.DELIMITER.FIELD) + options.DELIMITER.EOL;
154154
}, ''));
155155
};
156156

@@ -199,7 +199,7 @@ module.exports = {
199199
}
200200

201201
// If we are prepending the header, then join the header and data by EOL, otherwise just return the data
202-
return callback(null, options.PREPEND_HEADER ? csvHeading + options.EOL + csvData : csvData);
202+
return callback(null, options.PREPEND_HEADER ? csvHeading + options.DELIMITER.EOL + csvData : csvData);
203203
})
204204
.catch(function (err) {
205205
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.5.0",
5+
"version": "2.0.0",
66
"repository": {
77
"type": "git",
88
"url": "http://github.com/mrodrig/json-2-csv.git"

test/testCsv2Json.js

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ var csv2jsonTests = function () {
142142
options = {
143143
DELIMITER : {
144144
FIELD : ',',
145-
ARRAY : '/'
145+
ARRAY : '/',
146+
EOL : '\n'
146147
},
147-
EOL : '\n',
148148
PARSE_CSV_NUMBERS : false
149149
};
150150
});
@@ -286,9 +286,9 @@ var csv2jsonTests = function () {
286286
options = {
287287
DELIMITER : {
288288
FIELD : ';',
289-
ARRAY : '/'
289+
ARRAY : '/',
290+
EOL : '\n'
290291
},
291-
EOL : '\n',
292292
PARSE_CSV_NUMBERS : false
293293
};
294294
});
@@ -298,7 +298,7 @@ var csv2jsonTests = function () {
298298
if (err) { throw err; }
299299
true.should.equal(_.isEqual(err, null));
300300
csv.should.equal(csvTestData.unQuoted.regularJson.replace(/,/g, options.DELIMITER.FIELD));
301-
csv.split(options.EOL).length.should.equal(6);
301+
csv.split(options.DELIMITER.EOL).length.should.equal(6);
302302
done();
303303
}, options);
304304
});
@@ -308,7 +308,7 @@ var csv2jsonTests = function () {
308308
if (err) { throw err; }
309309
true.should.equal(_.isEqual(err, null));
310310
csv.should.equal(csvTestData.unQuoted.nestedJson.replace(/,/g, options.DELIMITER.FIELD));
311-
csv.split(options.EOL).length.should.equal(6);
311+
csv.split(options.DELIMITER.EOL).length.should.equal(6);
312312
done();
313313
}, options);
314314
});
@@ -318,7 +318,7 @@ var csv2jsonTests = function () {
318318
if (err) { throw err; }
319319
true.should.equal(_.isEqual(err, null));
320320
csv.should.equal(csvTestData.unQuoted.nestedJson2.replace(/,/g, options.DELIMITER.FIELD));
321-
csv.split(options.EOL).length.should.equal(4);
321+
csv.split(options.DELIMITER.EOL).length.should.equal(4);
322322
done();
323323
}, options);
324324
});
@@ -328,7 +328,7 @@ var csv2jsonTests = function () {
328328
if (err) { throw err; }
329329
true.should.equal(_.isEqual(err, null));
330330
csv.should.equal(csvTestData.unQuoted.nestedQuotes.replace(/,/g, options.DELIMITER.FIELD));
331-
csv.split(options.EOL).length.should.equal(4);
331+
csv.split(options.DELIMITER.EOL).length.should.equal(4);
332332
done();
333333
}, options);
334334
});
@@ -338,7 +338,7 @@ var csv2jsonTests = function () {
338338
if (err) { throw err; }
339339
true.should.equal(_.isEqual(err, null));
340340
csv.should.equal(csvTestData.unQuoted.noData.replace(/,/g, options.DELIMITER.FIELD));
341-
csv.split(options.EOL).length.should.equal(3); // Still adds newlines for header, first data row, and end of data
341+
csv.split(options.DELIMITER.EOL).length.should.equal(3); // Still adds newlines for header, first data row, and end of data
342342
done();
343343
}, options);
344344
});
@@ -348,7 +348,7 @@ var csv2jsonTests = function () {
348348
if (err) { throw err; }
349349
true.should.equal(_.isEqual(err, null));
350350
csv.should.equal(csvTestData.unQuoted.singleDoc.replace(/,/g, options.DELIMITER.FIELD));
351-
csv.split(options.EOL).length.should.equal(3);
351+
csv.split(options.DELIMITER.EOL).length.should.equal(3);
352352
done();
353353
}, options);
354354
});
@@ -358,7 +358,7 @@ var csv2jsonTests = function () {
358358
if (err) { throw err; }
359359
true.should.equal(_.isEqual(err, null));
360360
csv.should.equal(csvTestData.unQuoted.arrayValue.replace(/;/g, options.DELIMITER.ARRAY).replace(/,/g, options.DELIMITER.FIELD));
361-
csv.split(options.EOL).length.should.equal(5);
361+
csv.split(options.DELIMITER.EOL).length.should.equal(5);
362362
done();
363363
}, options);
364364
});
@@ -370,7 +370,7 @@ var csv2jsonTests = function () {
370370
if (err) { throw err; }
371371
true.should.equal(_.isEqual(err, null));
372372
csv.should.equal(csvTestData.unQuoted.arrayValue_specificKeys.replace(/,/g, opts.DELIMITER.FIELD));
373-
csv.split(options.EOL).length.should.equal(5);
373+
csv.split(options.DELIMITER.EOL).length.should.equal(5);
374374
done();
375375
}, opts);
376376
});
@@ -380,7 +380,7 @@ var csv2jsonTests = function () {
380380
if (err) { throw err; }
381381
true.should.equal(_.isEqual(err, null));
382382
csv.should.equal(csvTestData.unQuoted.regularJson.replace(/,/g, options.DELIMITER.FIELD));
383-
csv.split(options.EOL).length.should.equal(6);
383+
csv.split(options.DELIMITER.EOL).length.should.equal(6);
384384
done();
385385
}, options);
386386
});
@@ -902,9 +902,9 @@ var csv2jsonTests = function () {
902902
options = {
903903
DELIMITER : {
904904
FIELD : ';',
905-
ARRAY : '/'
905+
ARRAY : '/',
906+
EOL : '\n'
906907
},
907-
EOL : '\n',
908908
PARSE_CSV_NUMBERS : false
909909
};
910910
});
@@ -913,7 +913,7 @@ var csv2jsonTests = function () {
913913
converter.json2csvAsync(jsonTestData.regularJson, options)
914914
.then(function(csv) {
915915
csv.should.equal(csvTestData.unQuoted.regularJson.replace(/,/g, options.DELIMITER.FIELD));
916-
csv.split(options.EOL).length.should.equal(6);
916+
csv.split(options.DELIMITER.EOL).length.should.equal(6);
917917
done();
918918
})
919919
.catch(function (err) {
@@ -925,7 +925,7 @@ var csv2jsonTests = function () {
925925
converter.json2csvAsync(jsonTestData.nestedJson, options)
926926
.then(function(csv) {
927927
csv.should.equal(csvTestData.unQuoted.nestedJson.replace(/,/g, options.DELIMITER.FIELD));
928-
csv.split(options.EOL).length.should.equal(6);
928+
csv.split(options.DELIMITER.EOL).length.should.equal(6);
929929
done();
930930
})
931931
.catch(function (err) {
@@ -937,7 +937,7 @@ var csv2jsonTests = function () {
937937
converter.json2csvAsync(jsonTestData.nestedJson2, options)
938938
.then(function(csv) {
939939
csv.should.equal(csvTestData.unQuoted.nestedJson2.replace(/,/g, options.DELIMITER.FIELD));
940-
csv.split(options.EOL).length.should.equal(4);
940+
csv.split(options.DELIMITER.EOL).length.should.equal(4);
941941
done();
942942
})
943943
.catch(function (err) {
@@ -949,7 +949,7 @@ var csv2jsonTests = function () {
949949
converter.json2csvAsync(jsonTestData.nestedQuotes, options)
950950
.then(function(csv) {
951951
csv.should.equal(csvTestData.unQuoted.nestedQuotes.replace(/,/g, options.DELIMITER.FIELD));
952-
csv.split(options.EOL).length.should.equal(4);
952+
csv.split(options.DELIMITER.EOL).length.should.equal(4);
953953
done();
954954
})
955955
.catch(function (err) {
@@ -961,7 +961,7 @@ var csv2jsonTests = function () {
961961
converter.json2csvAsync(jsonTestData.noData, options)
962962
.then(function(csv) {
963963
csv.should.equal(csvTestData.unQuoted.noData.replace(/,/g, options.DELIMITER.FIELD));
964-
csv.split(options.EOL).length.should.equal(3); // Still adds newlines for header, first data row, and end of data
964+
csv.split(options.DELIMITER.EOL).length.should.equal(3); // Still adds newlines for header, first data row, and end of data
965965
done();
966966
})
967967
.catch(function (err) {
@@ -973,7 +973,7 @@ var csv2jsonTests = function () {
973973
converter.json2csvAsync(jsonTestData.singleDoc, options)
974974
.then(function (csv) {
975975
csv.should.equal(csvTestData.unQuoted.singleDoc.replace(/,/g, options.DELIMITER.FIELD));
976-
csv.split(options.EOL).length.should.equal(3);
976+
csv.split(options.DELIMITER.EOL).length.should.equal(3);
977977
done();
978978
})
979979
.catch(function (err) {
@@ -985,7 +985,7 @@ var csv2jsonTests = function () {
985985
converter.json2csvAsync(jsonTestData.arrayValue, options)
986986
.then(function (csv) {
987987
csv.should.equal(csvTestData.unQuoted.arrayValue.replace(/;/g, options.DELIMITER.ARRAY).replace(/,/g, options.DELIMITER.FIELD));
988-
csv.split(options.EOL).length.should.equal(5);
988+
csv.split(options.DELIMITER.EOL).length.should.equal(5);
989989
done();
990990
})
991991
.catch(function (err) {
@@ -999,7 +999,7 @@ var csv2jsonTests = function () {
999999
converter.json2csvAsync(jsonTestData.arrayValue, opts)
10001000
.then(function (csv) {
10011001
csv.should.equal(csvTestData.unQuoted.arrayValue_specificKeys.replace(/,/g, opts.DELIMITER.FIELD));
1002-
csv.split(options.EOL).length.should.equal(5);
1002+
csv.split(options.DELIMITER.EOL).length.should.equal(5);
10031003
done();
10041004
})
10051005
.catch(function (err) {
@@ -1011,7 +1011,7 @@ var csv2jsonTests = function () {
10111011
converter.json2csvAsync(jsonTestData.sameSchemaDifferentOrdering, options)
10121012
.then(function (csv) {
10131013
csv.should.equal(csvTestData.unQuoted.regularJson.replace(/,/g, ';'));
1014-
csv.split(options.EOL).length.should.equal(6);
1014+
csv.split(options.DELIMITER.EOL).length.should.equal(6);
10151015
done();
10161016
})
10171017
.catch(function (err) {
@@ -1059,9 +1059,9 @@ var csv2jsonTests = function () {
10591059
DELIMITER : {
10601060
FIELD : ',',
10611061
ARRAY : '/',
1062-
WRAP : '\"'
1062+
WRAP : '\"',
1063+
EOL : '\n'
10631064
},
1064-
EOL : '\n',
10651065
PARSE_CSV_NUMBERS : false
10661066
};
10671067
});

0 commit comments

Comments
 (0)