@@ -130,7 +130,7 @@ var splitLine = function (line) {
130
130
character ,
131
131
charBefore ,
132
132
charAfter ,
133
- lastLineIndex = line . length - 1 ,
133
+ lastCharacterIndex = line . length - 1 ,
134
134
stateVariables = {
135
135
insideWrapDelimiter : false ,
136
136
parsingValue : true ,
@@ -141,26 +141,35 @@ var splitLine = function (line) {
141
141
while ( index < line . length ) {
142
142
character = line [ index ] ;
143
143
charBefore = index ? line [ index - 1 ] : '' ;
144
- charAfter = index < lastLineIndex ? line [ index + 1 ] : '' ;
144
+ charAfter = index < lastCharacterIndex ? line [ index + 1 ] : '' ;
145
145
146
- console . log ( charBefore , character , charAfter ) ;
147
146
// 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 ) {
150
148
stateVariables . parsingValue = false ;
151
149
splitLine . push ( line . substring ( stateVariables . startIndex , index ) ) ;
152
150
index ++ ;
153
151
}
154
152
// 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 ) {
157
154
stateVariables . parsingValue = false ;
158
155
splitLine . push ( line . substring ( stateVariables . startIndex ) ) ;
159
156
index ++ ;
160
157
}
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
+ }
161
171
// If we reached a wrap delimiter with a field delimiter after it (ie. *",)
162
172
else if ( character === options . DELIMITER . WRAP && charAfter === options . DELIMITER . FIELD ) {
163
- console . log ( 'CASE 3' ) ;
164
173
splitLine . push ( line . substring ( stateVariables . startIndex , index ) ) ;
165
174
stateVariables . startIndex = index + 2 ;
166
175
stateVariables . insideWrapDelimiter = false ;
@@ -169,29 +178,32 @@ var splitLine = function (line) {
169
178
}
170
179
// If we reached a wrap delimiter with a field delimiter after it (ie. ,"*)
171
180
else if ( character === options . DELIMITER . WRAP && charBefore === options . DELIMITER . FIELD ) {
172
- console . log ( 'CASE 4' ) ;
173
181
if ( stateVariables . parsingValue ) {
174
182
splitLine . push ( line . substring ( stateVariables . startIndex , index - 1 ) ) ;
175
183
}
176
184
stateVariables . insideWrapDelimiter = true ;
177
185
stateVariables . parsingValue = true ;
178
186
stateVariables . startIndex = ++ index ;
179
187
}
180
- // TODO: if index === 0 and comma, then first field is empty
181
- // TODO: if character === '"' and index === 0
182
188
// If we reached a field delimiter and are not inside the wrap delimiters (ie. *,*)
183
189
else if ( character === options . DELIMITER . FIELD && charBefore !== options . DELIMITER . WRAP
184
190
&& charAfter !== options . DELIMITER . WRAP && ! stateVariables . insideWrapDelimiter
185
191
&& stateVariables . parsingValue ) {
186
- console . log ( 'CASE 5' ) ;
187
192
splitLine . push ( line . substring ( stateVariables . startIndex , index ) ) ;
188
193
stateVariables . insideWrapDelimiter = false ;
189
194
stateVariables . parsingValue = true ;
190
195
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 {
192
205
index ++ ;
193
206
}
194
- console . log ( splitLine ) ;
195
207
}
196
208
197
209
return splitLine ;
0 commit comments