@@ -5,7 +5,12 @@ var _ = require('underscore'),
55
66var 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+ */
914var 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+ */
2435var 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+ */
4662var 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+ */
5071var 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+ */
6390var 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+ */
83115var 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+
96140module . 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