@@ -130,7 +130,7 @@ var splitLine = function (line) {
130130 character ,
131131 charBefore ,
132132 charAfter ,
133- lastLineIndex = line . length - 1 ,
133+ lastCharacterIndex = line . length - 1 ,
134134 stateVariables = {
135135 insideWrapDelimiter : false ,
136136 parsingValue : true ,
@@ -141,26 +141,35 @@ var splitLine = function (line) {
141141 while ( index < line . length ) {
142142 character = line [ index ] ;
143143 charBefore = index ? line [ index - 1 ] : '' ;
144- charAfter = index < lastLineIndex ? line [ index + 1 ] : '' ;
144+ charAfter = index < lastCharacterIndex ? line [ index + 1 ] : '' ;
145145
146- console . log ( charBefore , character , charAfter ) ;
147146 // If we reached the end of the line and are in the wrap delimiter, substring up to, but not including this char
148- if ( index === lastLineIndex && stateVariables . insideWrapDelimiter ) {
149- console . log ( 'CASE 1' ) ;
147+ if ( index === lastCharacterIndex && stateVariables . insideWrapDelimiter ) {
150148 stateVariables . parsingValue = false ;
151149 splitLine . push ( line . substring ( stateVariables . startIndex , index ) ) ;
152150 index ++ ;
153151 }
154152 // If we reached the end of the line and are not in the wrap delimiter
155- else if ( index === lastLineIndex ) {
156- console . log ( 'CASE 2' ) ;
153+ else if ( index === lastCharacterIndex ) {
157154 stateVariables . parsingValue = false ;
158155 splitLine . push ( line . substring ( stateVariables . startIndex ) ) ;
159156 index ++ ;
160157 }
158+ // If the line starts with a wrap delimiter
159+ else if ( character === options . DELIMITER . WRAP && index === 0 ) {
160+ stateVariables . startIndex = index + 1 ;
161+ stateVariables . insideWrapDelimiter = true ;
162+ stateVariables . parsingValue = true ;
163+ index ++ ;
164+ }
165+ // If the first field is empty
166+ else if ( character === options . DELIMITER . FIELD && index == 0 ) {
167+ splitLine . push ( '' ) ;
168+ stateVariables . parsingValue = false ;
169+ index ++ ;
170+ }
161171 // If we reached a wrap delimiter with a field delimiter after it (ie. *",)
162172 else if ( character === options . DELIMITER . WRAP && charAfter === options . DELIMITER . FIELD ) {
163- console . log ( 'CASE 3' ) ;
164173 splitLine . push ( line . substring ( stateVariables . startIndex , index ) ) ;
165174 stateVariables . startIndex = index + 2 ;
166175 stateVariables . insideWrapDelimiter = false ;
@@ -169,29 +178,32 @@ var splitLine = function (line) {
169178 }
170179 // If we reached a wrap delimiter with a field delimiter after it (ie. ,"*)
171180 else if ( character === options . DELIMITER . WRAP && charBefore === options . DELIMITER . FIELD ) {
172- console . log ( 'CASE 4' ) ;
173181 if ( stateVariables . parsingValue ) {
174182 splitLine . push ( line . substring ( stateVariables . startIndex , index - 1 ) ) ;
175183 }
176184 stateVariables . insideWrapDelimiter = true ;
177185 stateVariables . parsingValue = true ;
178186 stateVariables . startIndex = ++ index ;
179187 }
180- // TODO: if index === 0 and comma, then first field is empty
181- // TODO: if character === '"' and index === 0
182188 // If we reached a field delimiter and are not inside the wrap delimiters (ie. *,*)
183189 else if ( character === options . DELIMITER . FIELD && charBefore !== options . DELIMITER . WRAP
184190 && charAfter !== options . DELIMITER . WRAP && ! stateVariables . insideWrapDelimiter
185191 && stateVariables . parsingValue ) {
186- console . log ( 'CASE 5' ) ;
187192 splitLine . push ( line . substring ( stateVariables . startIndex , index ) ) ;
188193 stateVariables . insideWrapDelimiter = false ;
189194 stateVariables . parsingValue = true ;
190195 stateVariables . startIndex = ++ index ;
191- } else {
196+ }
197+ else if ( character === options . DELIMITER . FIELD && charBefore === options . DELIMITER . WRAP
198+ && charAfter !== options . DELIMITER . WRAP ) {
199+ stateVariables . insideWrapDelimiter = false ;
200+ stateVariables . parsingValue = true ;
201+ stateVariables . startIndex = ++ index ;
202+ }
203+ // Otherwise...
204+ else {
192205 index ++ ;
193206 }
194- console . log ( splitLine ) ;
195207 }
196208
197209 return splitLine ;
0 commit comments