@@ -6,10 +6,16 @@ var _ = require('underscore'),
66
77var 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+ */
1015var 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+ */
4051var 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+ */
5874var 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+ */
7697var 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+ */
90116var 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
97123module . 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