@@ -9,8 +9,14 @@ var retrieveHeading = function (lines, callback) {
9
9
if ( ! lines . length ) { // If there are no lines passed in, then throw an error
10
10
return callback ( new Error ( "No data provided to retrieve heading." ) ) ; // Pass an error back to the user
11
11
}
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 ;
14
20
} ;
15
21
16
22
// Add a nested key and its value in the given document
@@ -53,21 +59,18 @@ var convertArrayRepresentation = function (val) {
53
59
} ;
54
60
55
61
// 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)
57
64
var doc = { } , // JSON document to start with and manipulate
58
65
val , // Temporary variable to set the current key's value to
59
66
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
- }
64
67
_ . each ( keys , function ( key , indx ) {
65
- val = line [ indx ] === '' ? null : line [ indx ] ;
68
+ val = line [ key . index ] === '' ? null : line [ key . index ] ;
66
69
if ( isArrayRepresentation ( val ) ) {
67
70
val = convertArrayRepresentation ( val ) ;
68
71
}
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
71
74
} else { // Else we just have a straight key:value mapping
72
75
doc [ key ] = val ; // Set the value at the current key
73
76
}
@@ -77,8 +80,11 @@ var createDoc = function (keys, line, callback) {
77
80
78
81
// Main wrapper function to convert the CSV to the JSON document array
79
82
var 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 ;
82
88
lines = lines . splice ( 1 ) ; // Grab all lines except for the header
83
89
_ . each ( lines , function ( line ) { // For each line, create the document and add it to the array of documents
84
90
jsonDocs . push ( createDoc ( headers , line ) ) ;
0 commit comments