Skip to content

Commit a5701ca

Browse files
committed
Adding constants for error messages, testJson2Csv finished, updating old tests so build passes
1 parent c35fa84 commit a5701ca

File tree

8 files changed

+788
-210
lines changed

8 files changed

+788
-210
lines changed

lib/constants.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"Errors" : {
3+
4+
"delimitersMustDiffer": "The field and array delimiters must differ.",
5+
"callbackRequired": "A callback is required!",
6+
"optionsRequired": "Options were not passed and are required.",
7+
8+
"json2csv": {
9+
"cannotCallJson2CsvOn": "Cannot call json2csv on ",
10+
"dataNotArrayOfDocuments": "Data provided was not an array of documents.",
11+
"notSameSchema": "Not all documents have the same schema."
12+
},
13+
14+
"csv2json": {
15+
"cannotCallCsv2JsonOn": "Cannot call csv2json on ",
16+
"csvNotString": "CSV is not a string.",
17+
"noDataRetrieveHeading": "No data provided to retrieve heading."
18+
}
19+
20+
}
21+
}

lib/converter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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
5+
constants = require('./constants'), // Require in constants
56
_ = require('underscore'); // Require underscore
67

78
/**
@@ -28,7 +29,7 @@ var buildOptions = function (opts, cb) {
2829
// Note: _.defaults does a shallow default, we need to deep copy the DELIMITER object
2930
opts.DELIMITER = _.defaults(opts.DELIMITER || {}, defaultOptions.DELIMITER);
3031
// If the delimiter fields are the same, report an error to the caller
31-
if (opts.DELIMITER.FIELD === opts.DELIMITER.ARRAY) { return cb(new Error('The field and array delimiters must differ.')); }
32+
if (opts.DELIMITER.FIELD === opts.DELIMITER.ARRAY) { return cb(new Error(constants.Errors.delimitersMustDiffer)); }
3233
// Otherwise, send the options back
3334
else { return cb(null, opts); }
3435
};

lib/csv-2-json.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use strict';
22

3-
var _ = require('underscore');
3+
var _ = require('underscore'),
4+
constants = require('./constants');
45

56
var options = {}; // Initialize the options - this will be populated when the csv2json function is called.
67

78
// Generate the JSON heading from the CSV
89
var retrieveHeading = function (lines, callback) {
910
if (!lines.length) { // If there are no lines passed in, then throw an error
10-
return callback(new Error("No data provided to retrieve heading.")); // Pass an error back to the user
11+
return callback(new Error(constants.Errors.csv2json.noDataRetrieveHeading)); // Pass an error back to the user
1112
}
1213
var heading = lines[0].split(options.DELIMITER.FIELD); // Grab the top line (header line) and split by the field delimiter
1314
heading = _.map(heading, function (headerKey, index) {
@@ -97,12 +98,12 @@ module.exports = {
9798
// Function to export internally
9899
// Takes options as a document, data as a CSV string, and a callback that will be used to report the results
99100
csv2json: function (opts, data, callback) {
100-
if (!callback) { throw new Error('A callback is required!'); } // If a callback wasn't provided, throw an error
101-
if (!opts) { callback(new Error('Options were not passed and are required.')); return null; } // Shouldn't happen, but just in case
101+
if (!callback) { throw new Error(constants.Errors.callbackRequired); } // If a callback wasn't provided, throw an error
102+
if (!opts) { callback(new Error(constants.Errors.optionsRequired)); return null; } // Shouldn't happen, but just in case
102103
else { options = opts; } // Options were passed, set the global options value
103-
if (!data) { callback(new Error('Cannot call csv2json on ' + data + '.')); return null; } // If we don't receive data, report an error
104+
if (!data) { callback(new Error(constants.Errors.csv2json.cannotCallCsv2JsonOn + data + '.')); return null; } // If we don't receive data, report an error
104105
if (!_.isString(data)) { // The data is not a string
105-
callback(new Error("CSV is not a string.")); // Report an error back to the caller
106+
callback(new Error(constants.Errors.csv2json.csvNotString)); // Report an error back to the caller
106107
}
107108
var lines = data.split(options.EOL); // Split the CSV into lines using the specified EOL option
108109
var json = convertCSV(lines, callback); // Retrieve the JSON document array

lib/json-2-csv.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var _ = require('underscore'),
4+
constants = require('./constants'),
45
Promise = require('bluebird');
56

67
var options = {}; // Initialize the options - this will be populated when the json2csv function is called.
@@ -28,7 +29,7 @@ var generateHeading = function(data) {
2829
// If there is a difference between the schemas, throw the inconsistent schema error
2930
var diff = _.difference(firstDocSchema, _.flatten(keyList));
3031
if (!_.isEqual(diff, [])) {
31-
return reject(new Error('Not all documents have the same schema.'));
32+
return reject(new Error(constants.Errors.json2csv.notSameSchema));
3233
}
3334
});
3435
return resolve(_.flatten(keys[0]));
@@ -98,15 +99,15 @@ module.exports = {
9899
// Function to export internally
99100
// Takes options as a document, data as a JSON document array, and a callback that will be used to report the results
100101
json2csv: function (opts, data, callback) {
101-
if (!callback) { throw new Error('A callback is required!'); } // If a callback wasn't provided, throw an error
102+
if (!callback) { throw new Error(constants.Errors.callbackRequired); } // If a callback wasn't provided, throw an error
102103

103-
if (!opts) { return callback(new Error('Options were not passed and are required.')); } // Shouldn't happen, but just in case
104+
if (!opts) { return callback(new Error(constants.Errors.optionsRequired)); } // Shouldn't happen, but just in case
104105
else { options = opts; } // Options were passed, set the global options value
105106

106-
if (!data) { return callback(new Error('Cannot call json2csv on ' + data + '.')); } // If we don't receive data, report an error
107+
if (!data) { return callback(new Error(constants.Errors.json2csv.cannotCallJson2CsvOn + data + '.')); } // If we don't receive data, report an error
107108

108109
if (!_.isObject(data)) { // If the data was not a single document or an array of documents
109-
return callback(new Error('Data provided was not an array of documents.')); // Report the error back to the caller
110+
return callback(new Error(constants.Errors.json2csv.dataNotArrayOfDocuments)); // Report the error back to the caller
110111
} else if (_.isObject(data) && !data.length) { // Single document, not an array
111112
data = [data]; // Convert to an array of the given document
112113
}
@@ -128,4 +129,4 @@ module.exports = {
128129
});
129130
}
130131

131-
};
132+
};

test/CSV/arrayValueDocs.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
info.name,coursesTaken,year
2-
Mike,[CS2500/CS2510],Sophomore
3-
John,[ANTH1101/POL2312/MATH2142/POL3305/LAW2100],Senior
2+
Mike,[CS2500;CS2510],Sophomore
3+
John,[ANTH1101;POL2312;MATH2142;POL3305;LAW2100],Senior
44
Joe,[],Freshman

test/testComma.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ var json2csvTests = function () {
8686

8787
it('should parse an array of JSON documents to CSV', function (done) {
8888
converter.json2csv(json_arrayValue, function (err, csv) {
89-
csv.should.equal(csv_arrayValue);
89+
csv.should.equal(csv_arrayValue.replace(/;/g, options.DELIMITER.ARRAY));
9090
csv.split(options.EOL).length.should.equal(5);
9191
done();
9292
}, options);
@@ -382,7 +382,7 @@ var json2csvTests = function () {
382382
it('should parse an array of JSON documents to CSV', function (done) {
383383
converter.json2csvAsync(json_arrayValue, options)
384384
.then(function (csv) {
385-
csv.should.equal(csv_arrayValue);
385+
csv.should.equal(csv_arrayValue.replace(/;/g, options.DELIMITER.ARRAY));
386386
csv.split(options.EOL).length.should.equal(5);
387387
done();
388388
})
@@ -636,7 +636,7 @@ var csv2jsonTests = function () {
636636
});
637637

638638
it('should parse a CSV with a nested array to the correct JSON representation', function (done) {
639-
converter.csv2json(csv_arrayValue, function (err, json) {
639+
converter.csv2json(csv_arrayValue.replace(/;/g, options.DELIMITER.ARRAY), function (err, json) {
640640
var isEqual = _.isEqual(json, json_arrayValue);
641641
true.should.equal(isEqual);
642642
done();
@@ -900,7 +900,7 @@ var csv2jsonTests = function () {
900900
});
901901

902902
it('should parse a CSV with a nested array to the correct JSON representation', function (done) {
903-
converter.csv2jsonAsync(csv_arrayValue, options)
903+
converter.csv2jsonAsync(csv_arrayValue.replace(/;/g, options.DELIMITER.ARRAY), options)
904904
.then(function (json) {
905905
var isEqual = _.isEqual(json, json_arrayValue);
906906
true.should.equal(isEqual);

0 commit comments

Comments
 (0)