Skip to content

Commit 7c593c6

Browse files
committed
cleaning up code comments
1 parent d21848b commit 7c593c6

File tree

2 files changed

+93
-22
lines changed

2 files changed

+93
-22
lines changed

lib/converter.js

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +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'),
67
_ = require('underscore'); // Require underscore
78

89
/**
@@ -17,8 +18,10 @@ var defaultOptions = constants.DefaultOptions;
1718
*/
1819
var buildOptions = function (opts, cb) {
1920
opts = _.defaults(opts || {}, defaultOptions);
21+
2022
// Note: _.defaults does a shallow default, we need to deep copy the DELIMITER object
2123
opts.DELIMITER = _.defaults(opts.DELIMITER || {}, defaultOptions.DELIMITER);
24+
2225
// If the delimiter fields are the same, report an error to the caller
2326
if (opts.DELIMITER.FIELD === opts.DELIMITER.ARRAY) { return cb(new Error(constants.Errors.delimitersMustDiffer)); }
2427
// Otherwise, send the options back
@@ -28,17 +31,22 @@ var buildOptions = function (opts, cb) {
2831
// Export the following functions that will be client accessible
2932
module.exports = {
3033

31-
// Client accessible json2csv function
32-
// Takes an array of JSON documents to be converted,
33-
// a callback that will be called with (err, csv) after
34-
// processing is completed, and optional options
34+
/**
35+
* Client accessible json2csv function
36+
* Takes an array of JSON documents to be converted, a callback that will be called with (err, csv)
37+
* after processing is complete, and optional options
38+
* @param array Object[] data to be converted
39+
* @param callback Function callback
40+
* @param opts Object options object
41+
*/
3542
json2csv: function (array, callback, opts) {
3643
// If this was promisified (callback and opts are swapped) then fix the argument order.
3744
if (_.isObject(callback) && !_.isFunction(callback)) {
3845
var func = opts;
3946
opts = callback;
4047
callback = func;
4148
}
49+
4250
buildOptions(opts, function (err, options) { // Build the options
4351
if (err) {
4452
return callback(err);
@@ -49,17 +57,22 @@ module.exports = {
4957
},
5058

5159

52-
// Client accessible csv2json function
53-
// Takes a string of CSV to be converted to a JSON document array,
54-
// a callback that will be called with (err, csv) after
55-
// processing is completed, and optional options
60+
/**
61+
* Client accessible csv2json function
62+
* Takes a string of CSV to be converted to a JSON document array, a callback that will be called
63+
* with (err, json) after processing is complete, and optional options
64+
* @param csv
65+
* @param callback
66+
* @param opts
67+
*/
5668
csv2json: function (csv, callback, opts) {
5769
// If this was promisified (callback and opts are swapped) then fix the argument order.
5870
if (_.isObject(callback) && !_.isFunction(callback)) {
5971
var func = opts;
6072
opts = callback;
6173
callback = func;
6274
}
75+
6376
buildOptions(opts, function (err, options) { // Build the options
6477
if (err) {
6578
return callback(err);

lib/csv-2-json.js

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ var _ = require('underscore'),
55

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

8-
// Generate the JSON heading from the CSV
8+
/**
9+
* Generate the JSON heading from the CSV
10+
* @param lines
11+
* @param callback
12+
* @returns {*}
13+
*/
914
var retrieveHeading = function (lines, callback) {
1015
if (!lines.length) { // If there are no lines passed in, then throw an error
1116
return callback(new Error(constants.Errors.csv2json.noDataRetrieveHeading)); // Pass an error back to the user
@@ -20,7 +25,13 @@ var retrieveHeading = function (lines, callback) {
2025
return heading;
2126
};
2227

23-
// Add a nested key and its value in the given document
28+
/**
29+
* Add a nested key and its value in the given document
30+
* @param key String
31+
* @param value String
32+
* @param doc Object
33+
* @returns {*}
34+
*/
2435
var addNestedKey = function (key, value, doc) {
2536
var subDocumentRoot = doc, // This is the document that we will be using to add the nested keys to.
2637
trackerDocument = subDocumentRoot, // This is the document that will use to iterate through the subDocument, starting at the root
@@ -43,10 +54,20 @@ var keyExists = function (key, doc) {
4354
return (!_.isUndefined(doc[key])); // If the key doesn't exist, then the type is 'undefined'
4455
};
4556

57+
/**
58+
* Does the given value represent an array?
59+
* @param value
60+
* @returns {*|boolean}
61+
*/
4662
var isArrayRepresentation = function (value) {
4763
return (value && value.indexOf('[') === 0 && value.lastIndexOf(']') === value.length-1);
4864
};
4965

66+
/**
67+
* Converts the value from a CSV 'array'
68+
* @param val
69+
* @returns {Array}
70+
*/
5071
var convertArrayRepresentation = function (val) {
5172
val = _.filter(val.substring(1, val.length-1).split(options.DELIMITER.ARRAY), function (value) {
5273
return value;
@@ -59,7 +80,13 @@ var convertArrayRepresentation = function (val) {
5980
return val;
6081
};
6182

62-
// Create a JSON document with the given keys (designated by the CSV header) and the values (from the given line)
83+
/**
84+
* Create a JSON document with the given keys (designated by the CSV header)
85+
* and the values (from the given line)
86+
* @param keys String[]
87+
* @param line String
88+
* @returns {Object} created json document
89+
*/
6390
var createDoc = function (keys, line) {
6491
if (line == '') { return false; } // If we have an empty line, then return false so we can remove all blank lines (falsy values)
6592
var doc = {}, // JSON document to start with and manipulate
@@ -79,7 +106,12 @@ var createDoc = function (keys, line) {
79106
return doc; // Return the created document
80107
};
81108

82-
// Main wrapper function to convert the CSV to the JSON document array
109+
/**
110+
* Main helper function to convert the CSV to the JSON document array
111+
* @param lines String[]
112+
* @param callback Function callback function
113+
* @returns {Array}
114+
*/
83115
var convertCSV = function (lines, callback) {
84116
var generatedHeaders = retrieveHeading(lines, callback), // Retrieve the headings from the CSV, unless the user specified the keys
85117
jsonDocs = [], // Create an array that we can add the generated documents to
@@ -93,21 +125,47 @@ var convertCSV = function (lines, callback) {
93125
return _.filter(jsonDocs, function (doc) { return doc !== false; }); // Return all non 'falsey' values to filter blank lines
94126
};
95127

128+
/**
129+
* Validates the input provided to csv2json.
130+
* Only returns (undefined) if no errors are found
131+
* @param opts Object options object
132+
* @param data String csv string
133+
* @param callback Function callback function
134+
* @returns {*}
135+
*/
136+
var checkErrors = function (opts, data, callback){
137+
138+
};
139+
96140
module.exports = {
97141

98-
// Function to export internally
99-
// Takes options as a document, data as a CSV string, and a callback that will be used to report the results
142+
/**
143+
* Internally exported csv2json function
144+
* Takes options as a document, data as a CSV string, and a callback that will be used to report the results
145+
* @param opts Object options object
146+
* @param data String csv string
147+
* @param callback Function callback function
148+
*/
100149
csv2json: function (opts, data, callback) {
101-
if (!callback) { throw new Error(constants.Errors.callbackRequired); } // If a callback wasn't provided, throw an error
102-
if (!opts) { return callback(new Error(constants.Errors.optionsRequired)); return null; } // Shouldn't happen, but just in case
103-
else { options = opts; } // Options were passed, set the global options value
104-
if (!data) { return callback(new Error(constants.Errors.csv2json.cannotCallCsv2JsonOn + data + '.')); return null; } // If we don't receive data, report an error
105-
if (!_.isString(data)) { // The data is not a string
150+
// If a callback wasn't provided, throw an error
151+
if (!callback) { throw new Error(constants.Errors.callbackRequired); }
152+
153+
// Shouldn't happen, but just in case
154+
if (!opts) { return callback(new Error(constants.Errors.optionsRequired)); return null; }
155+
options = opts; // Options were passed, set the global options value
156+
157+
// If we don't receive data, report an error
158+
if (!data) { return callback(new Error(constants.Errors.csv2json.cannotCallCsv2JsonOn + data + '.')); return null; }
159+
160+
// The data provided is not a string
161+
if (!_.isString(data)) {
106162
return callback(new Error(constants.Errors.csv2json.csvNotString)); // Report an error back to the caller
107163
}
108-
var lines = data.split(options.EOL); // Split the CSV into lines using the specified EOL option
109-
var json = convertCSV(lines, callback); // Retrieve the JSON document array
110-
callback(null, json); // Send the data back to the caller
164+
165+
// Split the CSV into lines using the specified EOL option
166+
var lines = data.split(options.EOL),
167+
json = convertCSV(lines, callback); // Retrieve the JSON document array
168+
return callback(null, json); // Send the data back to the caller
111169
}
112170

113171
};

0 commit comments

Comments
 (0)