Skip to content

Commit e8e826a

Browse files
committed
Cleaning up the typeof calls - working on refactoring the retrieveHeading function now
1 parent b465714 commit e8e826a

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

lib/csv-2-json.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var addNestedKey = function (key, value, doc) {
3434

3535
// Helper function to check if the given key already exists in the given document
3636
var keyExists = function (key, doc) {
37-
return (typeof doc[key] !== 'undefined'); // If the key doesn't exist, then the type is 'undefined'
37+
return (!_.isUndefined(doc[key])); // If the key doesn't exist, then the type is 'undefined'
3838
};
3939

4040
var isArrayRepresentation = function (value) {
@@ -96,7 +96,7 @@ module.exports = {
9696
if (!opts) { callback(new Error('Options were not passed and are required.')); return null; } // Shouldn't happen, but just in case
9797
else { options = opts; } // Options were passed, set the global options value
9898
if (!data) { callback(new Error('Cannot call csv2json on ' + data + '.')); return null; } // If we don't receive data, report an error
99-
if (typeof data !== 'string') { // The data is not a string
99+
if (!_.isString(data)) { // The data is not a string
100100
callback(new Error("CSV is not a string.")); // Report an error back to the caller
101101
}
102102
var lines = data.split(options.EOL); // Split the CSV into lines using the specified EOL option

lib/json-2-csv.js

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var retrieveSubHeading = function (heading, data) {
1212
_.each(subKeys, function (subKey, indx) {
1313
// If the given heading is empty, then we set the heading to be the subKey, otherwise set it as a nested heading w/ a dot
1414
newKey = heading === '' ? subKey : heading + '.' + subKey;
15-
if (typeof data[subKey] === 'object' && data[subKey] !== null && typeof data[subKey].length === 'undefined' && _.keys(data[subKey]).length > 0) { // If we have another nested document
15+
if (_.isObject(data[subKey]) && !_.isNull(data[subKey]) && _.isUndefined(data[subKey].length) && _.keys(data[subKey]).length > 0) { // If we have another nested document
1616
subKeys[indx] = retrieveSubHeading(newKey, data[subKey]); // Recur on the subdocument to retrieve the full key name
1717
} else {
1818
subKeys[indx] = (options.DELIMITER.WRAP || '') + (newKey || '') + (options.DELIMITER.WRAP || ''); // Set the key name since we don't have a sub document
@@ -22,21 +22,19 @@ var retrieveSubHeading = function (heading, data) {
2222
};
2323

2424
// Retrieve the headings for all documents and return it. This checks that all documents have the same schema.
25-
var retrieveHeading = function (data) {
26-
return function (cb) { // Returns a function that takes a callback - the function is passed to async.parallel
27-
var keys = _.keys(data); // Retrieve the current data keys
28-
_.each(keys, function (key, indx) { // for each key
29-
if (typeof data[key] === 'object') {
30-
// if the data at the key is a document, then we retrieve the subHeading starting with an empty string heading and the doc
31-
keys[indx] = retrieveSubHeading('', data[key]);
32-
}
33-
});
34-
// Retrieve the unique array of headings (keys)
35-
keys = _.uniq(keys);
36-
// If we have more than 1 unique list, then not all docs have the same schema - report an error
37-
if (keys.length > 1) { throw new Error('Not all documents have the same schema.', keys); }
38-
return cb(null, _.flatten(keys).join(options.DELIMITER.FIELD)); // Return headings back
39-
};
25+
var retrieveHeading = function (data, cb) {
26+
var keys = _.keys(data); // Retrieve the current data keys
27+
_.each(keys, function (key, indx) { // for each key
28+
if (_.isObject(data[key])) {
29+
// if the data at the key is a document, then we retrieve the subHeading starting with an empty string heading and the doc
30+
keys[indx] = retrieveSubHeading('', data[key]);
31+
}
32+
});
33+
// Retrieve the unique array of headings (keys)
34+
keys = _.uniq(keys);
35+
// If we have more than 1 unique list, then not all docs have the same schema - report an error
36+
if (keys.length > 1) { throw new Error('Not all documents have the same schema.', keys); }
37+
return cb(null, _.flatten(keys).join(options.DELIMITER.FIELD)); // Return headings back
4038
};
4139

4240
// Convert the given data with the given keys
@@ -62,11 +60,9 @@ var convertData = function (data, keys) {
6260
};
6361

6462
// Generate the CSV representing the given data.
65-
var generateCsv = function (data) {
66-
return function (cb) { // Returns a function that takes a callback - the function is passed to async.parallel
67-
// Reduce each JSON document in data to a CSV string and append it to the CSV accumulator
68-
return cb(null, _.reduce(data, function (csv, doc) { return csv += convertData(doc, _.keys(doc)) + options.EOL; }, ''));
69-
};
63+
var generateCsv = function (data, cb) {
64+
// Reduce each JSON document in data to a CSV string and append it to the CSV accumulator
65+
return cb(null, _.reduce(data, function (csv, doc) { return csv += convertData(doc, _.keys(doc)) + options.EOL; }, ''));
7066
};
7167

7268
module.exports = {
@@ -78,13 +74,14 @@ module.exports = {
7874
if (!opts) { callback(new Error('Options were not passed and are required.')); return null; } // Shouldn't happen, but just in case
7975
else { options = opts; } // Options were passed, set the global options value
8076
if (!data) { callback(new Error('Cannot call json2csv on ' + data + '.')); return null; } // If we don't receive data, report an error
81-
if (typeof data !== 'object') { // If the data was not a single document or an array of documents
77+
if (!_.isObject(data)) { // If the data was not a single document or an array of documents
8278
return cb(new Error('Data provided was not an array of documents.')); // Report the error back to the caller
83-
} else if (typeof data === 'object' && !data.length) { // Single document, not an array
79+
} else if (_.isObject(data) && !data.length) { // Single document, not an array
8480
data = [data]; // Convert to an array of the given document
8581
}
82+
8683
// Retrieve the heading and the CSV asynchronously in parallel
87-
async.parallel([retrieveHeading(data), generateCsv(data)], function (err, res) {
84+
async.parallel([_.partial(retrieveHeading, data), _.partial(generateCsv, data)], function (err, res) {
8885
if (!err) {
8986
// Data received with no errors, join the two responses with an end of line delimiter to setup heading and CSV body
9087
return callback(null, res.join(options.EOL));

0 commit comments

Comments
 (0)