Skip to content

Commit 8347b1c

Browse files
committed
a few more changes - still many more to come
1 parent 414dc39 commit 8347b1c

File tree

3 files changed

+61
-23
lines changed

3 files changed

+61
-23
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"tests"
1515
],
1616
"dependencies": {
17-
"underscore": "1.6.0",
17+
"underscore": "1.8.3",
1818
"bluebird": "2.9.24"
1919
}
2020
}

lib/csv-2-json.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ var retrieveHeading = function (lines, callback) {
1515
if (!lines.length) { // If there are no lines passed in, then throw an error
1616
return callback(new Error(constants.Errors.csv2json.noDataRetrieveHeading)); // Pass an error back to the user
1717
}
18-
var heading = lines[0].split(options.DELIMITER.FIELD); // Grab the top line (header line) and split by the field delimiter
19-
heading = _.map(heading, function (headerKey, index) {
20-
return {
21-
value: headerKey,
22-
index: index
23-
}
24-
});
18+
19+
var heading = _.map(lines[0].split(options.DELIMITER.FIELD),
20+
function (headerKey, index) {
21+
return {
22+
value: headerKey,
23+
index: index
24+
};
25+
});
2526
return heading;
2627
};
2728

@@ -52,7 +53,7 @@ var addNestedKey = function (key, value, doc) {
5253
/**
5354
* Does the given value represent an array?
5455
* @param value
55-
* @returns {*|boolean}
56+
* @returns {boolean}
5657
*/
5758
var isArrayRepresentation = function (value) {
5859
return (value && value.indexOf('[') === 0 && value.lastIndexOf(']') === value.length-1);
@@ -121,7 +122,7 @@ var convertCSV = function (lines, callback) {
121122
};
122123

123124
module.exports = {
124-
125+
125126
/**
126127
* Internally exported csv2json function
127128
* Takes options as a document, data as a CSV string, and a callback that will be used to report the results

lib/json-2-csv.js

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ var _ = require('underscore'),
66

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

9-
// Retrieve the headings for all documents and return it. This checks that all documents have the same schema.
9+
/**
10+
* Retrieve the headings for all documents and return it.
11+
* This checks that all documents have the same schema.
12+
* @param data
13+
* @returns {Promise}
14+
*/
1015
var generateHeading = function(data) {
1116
return new Promise(function (resolve, reject) {
1217
if (options.KEYS) { resolve(options.KEYS); }
18+
1319
var keys = _.map(_.keys(data), function (key, indx) { // for each key
1420
if (_.isObject(data[key])) {
1521
// if the data at the key is a document, then we retrieve the subHeading starting with an empty string heading and the doc
@@ -36,7 +42,12 @@ var generateHeading = function(data) {
3642
});
3743
};
3844

39-
// Takes the parent heading and this doc's data and creates the subdocument headings (string)
45+
/**
46+
* Takes the parent heading and this doc's data and creates the subdocument headings (string)
47+
* @param heading
48+
* @param data
49+
* @returns {Array}
50+
*/
4051
var generateSubHeading = function(heading, data) {
4152
var subKeys, // retrieve the keys from the current document
4253
newKey = ''; // temporary variable to aid in determining the heading - used to generate the 'nested' headings
@@ -54,7 +65,12 @@ var generateSubHeading = function(heading, data) {
5465
return subKeys; // Return the headings joined by our field delimiter
5566
};
5667

57-
// Convert the given data with the given keys
68+
/**
69+
* Convert the given data with the given keys
70+
* @param data
71+
* @param keys
72+
* @returns {Array}
73+
*/
5874
var convertData = function (data, keys) {
5975
var output = [], // Array of CSV representing converted docs
6076
value; // Temporary variable to store the current data
@@ -73,6 +89,11 @@ var convertData = function (data, keys) {
7389
return output; // Return the data joined by our field delimiter
7490
};
7591

92+
/**
93+
* Convert the given value to the CSV representation of the value
94+
* @param value
95+
* @param output
96+
*/
7697
var convertField = function (value, output) {
7798
if (_.isArray(value)) { // We have an array of values
7899
output.push(options.DELIMITER.WRAP + '[' + value.join(options.DELIMITER.ARRAY) + ']' + options.DELIMITER.WRAP);
@@ -81,12 +102,17 @@ var convertField = function (value, output) {
81102
} else if (_.isObject(value)) { // If we have an object
82103
output.push(convertData(value, _.keys(value))); // Push the recursively generated CSV
83104
} else {
84-
value = value === null ? '' : value.toString();
105+
value = value ? value.toString() : '';
85106
output.push(options.DELIMITER.WRAP + value + options.DELIMITER.WRAP); // Otherwise push the current value
86107
}
87108
};
88109

89-
// Generate the CSV representing the given data.
110+
/**
111+
* Generate the CSV representing the given data.
112+
* @param data
113+
* @param headingKeys
114+
* @returns {*}
115+
*/
90116
var generateCsv = function (data, headingKeys) {
91117
// Reduce each JSON document in data to a CSV string and append it to the CSV accumulator
92118
return Promise.resolve([headingKeys, _.reduce(data, function (csv, doc) {
@@ -96,19 +122,30 @@ var generateCsv = function (data, headingKeys) {
96122

97123
module.exports = {
98124

99-
// Function to export internally
100-
// Takes options as a document, data as a JSON document array, and a callback that will be used to report the results
125+
/**
126+
* Internally exported json2csv function
127+
* Takes options as a document, data as a JSON document array, and a callback that will be used to report the results
128+
* @param opts Object options object
129+
* @param data String csv string
130+
* @param callback Function callback function
131+
*/
101132
json2csv: function (opts, data, callback) {
102-
if (!callback) { throw new Error(constants.Errors.callbackRequired); } // If a callback wasn't provided, throw an error
133+
// If a callback wasn't provided, throw an error
134+
if (!callback) { throw new Error(constants.Errors.callbackRequired); }
103135

104-
if (!opts) { return callback(new Error(constants.Errors.optionsRequired)); } // Shouldn't happen, but just in case
105-
else { options = opts; } // Options were passed, set the global options value
136+
// Shouldn't happen, but just in case
137+
if (!opts) { return callback(new Error(constants.Errors.optionsRequired)); }
138+
options = opts; // Options were passed, set the global options value
106139

107-
if (!data) { return callback(new Error(constants.Errors.json2csv.cannotCallJson2CsvOn + data + '.')); } // If we don't receive data, report an error
140+
// If we don't receive data, report an error
141+
if (!data) { return callback(new Error(constants.Errors.json2csv.cannotCallJson2CsvOn + data + '.')); }
108142

109-
if (!_.isObject(data)) { // If the data was not a single document or an array of documents
143+
// If the data was not a single document or an array of documents
144+
if (!_.isObject(data)) {
110145
return callback(new Error(constants.Errors.json2csv.dataNotArrayOfDocuments)); // Report the error back to the caller
111-
} else if (_.isObject(data) && !data.length) { // Single document, not an array
146+
}
147+
// Single document, not an array
148+
else if (_.isObject(data) && !data.length) {
112149
data = [data]; // Convert to an array of the given document
113150
}
114151

0 commit comments

Comments
 (0)