@@ -6,21 +6,6 @@ var _ = require('underscore'),
66
77var options = { } ; // Initialize the options - this will be populated when the csv2json function is called.
88
9- /** Helper function from http://phpjs.org/functions/addslashes/ **/
10- var addSlashes = function ( str ) {
11- return ( str + '' )
12- . replace ( / [ \\ " ' ] / g, '\\$&' )
13- . replace ( / \u0000 / g, '\\0' ) ;
14- } ;
15-
16- var lstrip = function ( str , search ) {
17- return str . replace ( new RegExp ( '^' + search + '*' ) , '' ) ;
18- } ;
19-
20- var rstrip = function ( str , search ) {
21- return str . replace ( new RegExp ( search + '*$' ) , '' ) ;
22- } ;
23-
249/**
2510 * Generate the JSON heading from the CSV
2611 * @param lines
@@ -138,43 +123,36 @@ var splitLine = function (line) {
138123 } ,
139124 index = 0 ;
140125
126+ // Loop through each character in the line to identify where to split the values
141127 while ( index < line . length ) {
142- character = line [ index ] ;
128+ // Current character
129+ character = line [ index ] ;
130+ // Previous character
143131 charBefore = index ? line [ index - 1 ] : '' ;
132+ // Next character
144133 charAfter = index < lastCharacterIndex ? line [ index + 1 ] : '' ;
145134
146- // If we reached the end of the line and are in the wrap delimiter, substring up to, but not including this char
147- if ( index === lastCharacterIndex && stateVariables . insideWrapDelimiter ) {
148- stateVariables . parsingValue = false ;
149- splitLine . push ( line . substring ( stateVariables . startIndex , index ) ) ;
150- index ++ ;
151- }
152- // If we reached the end of the line and are not in the wrap delimiter
153- else if ( index === lastCharacterIndex ) {
154- stateVariables . parsingValue = false ;
155- splitLine . push ( line . substring ( stateVariables . startIndex ) ) ;
156- index ++ ;
135+ // If we reached the end of the line, add the remaining value
136+ if ( index === lastCharacterIndex ) {
137+ splitLine . push ( line . substring ( stateVariables . startIndex , stateVariables . insideWrapDelimiter ? index : undefined ) ) ;
157138 }
158139 // If the line starts with a wrap delimiter
159140 else if ( character === options . DELIMITER . WRAP && index === 0 ) {
160- stateVariables . startIndex = index + 1 ;
161141 stateVariables . insideWrapDelimiter = true ;
162142 stateVariables . parsingValue = true ;
163- index ++ ;
143+ stateVariables . startIndex = index + 1 ;
164144 }
165- // If the first field is empty
145+ // If the first field is empty (ie. line starts with a field delimiter)
166146 else if ( character === options . DELIMITER . FIELD && index == 0 ) {
167147 splitLine . push ( '' ) ;
168148 stateVariables . parsingValue = false ;
169- index ++ ;
170149 }
171150 // If we reached a wrap delimiter with a field delimiter after it (ie. *",)
172151 else if ( character === options . DELIMITER . WRAP && charAfter === options . DELIMITER . FIELD ) {
173152 splitLine . push ( line . substring ( stateVariables . startIndex , index ) ) ;
174- stateVariables . startIndex = index + 2 ;
153+ stateVariables . startIndex = index + 2 ; // next value starts after the field delimiter
175154 stateVariables . insideWrapDelimiter = false ;
176155 stateVariables . parsingValue = false ;
177- index ++ ;
178156 }
179157 // If we reached a wrap delimiter with a field delimiter after it (ie. ,"*)
180158 else if ( character === options . DELIMITER . WRAP && charBefore === options . DELIMITER . FIELD ) {
@@ -183,27 +161,23 @@ var splitLine = function (line) {
183161 }
184162 stateVariables . insideWrapDelimiter = true ;
185163 stateVariables . parsingValue = true ;
186- stateVariables . startIndex = ++ index ;
164+ stateVariables . startIndex = index + 1 ;
187165 }
188166 // If we reached a field delimiter and are not inside the wrap delimiters (ie. *,*)
189167 else if ( character === options . DELIMITER . FIELD && charBefore !== options . DELIMITER . WRAP
190168 && charAfter !== options . DELIMITER . WRAP && ! stateVariables . insideWrapDelimiter
191169 && stateVariables . parsingValue ) {
192170 splitLine . push ( line . substring ( stateVariables . startIndex , index ) ) ;
193- stateVariables . insideWrapDelimiter = false ;
194- stateVariables . parsingValue = true ;
195- stateVariables . startIndex = ++ index ;
171+ stateVariables . startIndex = index + 1 ;
196172 }
197173 else if ( character === options . DELIMITER . FIELD && charBefore === options . DELIMITER . WRAP
198174 && charAfter !== options . DELIMITER . WRAP ) {
199175 stateVariables . insideWrapDelimiter = false ;
200176 stateVariables . parsingValue = true ;
201- stateVariables . startIndex = ++ index ;
202- }
203- // Otherwise...
204- else {
205- index ++ ;
177+ stateVariables . startIndex = index + 1 ;
206178 }
179+ // Otherwise increment to the next character
180+ index ++ ;
207181 }
208182
209183 return splitLine ;
0 commit comments