@@ -9,8 +9,14 @@ var retrieveHeading = function (lines, callback) {
99 if ( ! lines . length ) { // If there are no lines passed in, then throw an error
1010 return callback ( new Error ( "No data provided to retrieve heading." ) ) ; // Pass an error back to the user
1111 }
12- var heading = lines [ 0 ] ; // Grab the top line (header line)
13- return heading . split ( options . DELIMITER . FIELD ) ; // Return the heading split by the field delimiter
12+ var heading = lines [ 0 ] . split ( options . DELIMITER . FIELD ) ; // Grab the top line (header line) and split by the field delimiter
13+ heading = _ . map ( heading , function ( headerKey , index ) {
14+ return {
15+ value : headerKey ,
16+ index : index
17+ }
18+ } ) ;
19+ return heading ;
1420} ;
1521
1622// Add a nested key and its value in the given document
@@ -53,21 +59,18 @@ var convertArrayRepresentation = function (val) {
5359} ;
5460
5561// Create a JSON document with the given keys (designated by the CSV header) and the values (from the given line)
56- var createDoc = function ( keys , line , callback ) {
62+ var createDoc = function ( keys , line ) {
63+ if ( line == '' ) { return false ; } // If we have an empty line, then return false so we can remove all blank lines (falsy values)
5764 var doc = { } , // JSON document to start with and manipulate
5865 val , // Temporary variable to set the current key's value to
5966 line = line . trim ( ) . split ( options . DELIMITER . FIELD ) ; // Split the line using the given field delimiter after trimming whitespace
60- if ( line == '' ) { return false ; } // If we have an empty line, then return false so we can remove all blank lines (falsy values)
61- if ( keys . length !== line . length ) { // If the number of keys is different than the number of values in the current line
62- return callback ( new Error ( "Not every line has a correct number of values." ) ) ; // Pass the error back to the client
63- }
6467 _ . each ( keys , function ( key , indx ) {
65- val = line [ indx ] === '' ? null : line [ indx ] ;
68+ val = line [ key . index ] === '' ? null : line [ key . index ] ;
6669 if ( isArrayRepresentation ( val ) ) {
6770 val = convertArrayRepresentation ( val ) ;
6871 }
69- if ( key . indexOf ( '.' ) ) { // If key has '.' representing nested document
70- doc = addNestedKey ( key , val , doc ) ; // Update the document to add the nested key structure
72+ if ( key . value . indexOf ( '.' ) ) { // If key has '.' representing nested document
73+ doc = addNestedKey ( key . value , val , doc ) ; // Update the document to add the nested key structure
7174 } else { // Else we just have a straight key:value mapping
7275 doc [ key ] = val ; // Set the value at the current key
7376 }
@@ -77,8 +80,11 @@ var createDoc = function (keys, line, callback) {
7780
7881// Main wrapper function to convert the CSV to the JSON document array
7982var convertCSV = function ( lines , callback ) {
80- var headers = retrieveHeading ( lines , callback ) , // Retrieve the headings from the CSV
81- jsonDocs = [ ] ; // Create an array that we can add the generated documents to
83+ var generatedHeaders = retrieveHeading ( lines , callback ) , // Retrieve the headings from the CSV, unless the user specified the keys
84+ jsonDocs = [ ] , // Create an array that we can add the generated documents to
85+ headers = options . KEYS ? _ . filter ( generatedHeaders , function ( headerKey ) {
86+ return _ . contains ( options . KEYS , headerKey . value ) ;
87+ } ) : generatedHeaders ;
8288 lines = lines . splice ( 1 ) ; // Grab all lines except for the header
8389 _ . each ( lines , function ( line ) { // For each line, create the document and add it to the array of documents
8490 jsonDocs . push ( createDoc ( headers , line ) ) ;
0 commit comments