@@ -6,21 +6,6 @@ var _ = require('underscore'),
6
6
7
7
var options = { } ; // Initialize the options - this will be populated when the csv2json function is called.
8
8
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
-
24
9
/**
25
10
* Generate the JSON heading from the CSV
26
11
* @param lines
@@ -138,43 +123,36 @@ var splitLine = function (line) {
138
123
} ,
139
124
index = 0 ;
140
125
126
+ // Loop through each character in the line to identify where to split the values
141
127
while ( index < line . length ) {
142
- character = line [ index ] ;
128
+ // Current character
129
+ character = line [ index ] ;
130
+ // Previous character
143
131
charBefore = index ? line [ index - 1 ] : '' ;
132
+ // Next character
144
133
charAfter = index < lastCharacterIndex ? line [ index + 1 ] : '' ;
145
134
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 ) ) ;
157
138
}
158
139
// If the line starts with a wrap delimiter
159
140
else if ( character === options . DELIMITER . WRAP && index === 0 ) {
160
- stateVariables . startIndex = index + 1 ;
161
141
stateVariables . insideWrapDelimiter = true ;
162
142
stateVariables . parsingValue = true ;
163
- index ++ ;
143
+ stateVariables . startIndex = index + 1 ;
164
144
}
165
- // If the first field is empty
145
+ // If the first field is empty (ie. line starts with a field delimiter)
166
146
else if ( character === options . DELIMITER . FIELD && index == 0 ) {
167
147
splitLine . push ( '' ) ;
168
148
stateVariables . parsingValue = false ;
169
- index ++ ;
170
149
}
171
150
// If we reached a wrap delimiter with a field delimiter after it (ie. *",)
172
151
else if ( character === options . DELIMITER . WRAP && charAfter === options . DELIMITER . FIELD ) {
173
152
splitLine . push ( line . substring ( stateVariables . startIndex , index ) ) ;
174
- stateVariables . startIndex = index + 2 ;
153
+ stateVariables . startIndex = index + 2 ; // next value starts after the field delimiter
175
154
stateVariables . insideWrapDelimiter = false ;
176
155
stateVariables . parsingValue = false ;
177
- index ++ ;
178
156
}
179
157
// If we reached a wrap delimiter with a field delimiter after it (ie. ,"*)
180
158
else if ( character === options . DELIMITER . WRAP && charBefore === options . DELIMITER . FIELD ) {
@@ -183,27 +161,23 @@ var splitLine = function (line) {
183
161
}
184
162
stateVariables . insideWrapDelimiter = true ;
185
163
stateVariables . parsingValue = true ;
186
- stateVariables . startIndex = ++ index ;
164
+ stateVariables . startIndex = index + 1 ;
187
165
}
188
166
// If we reached a field delimiter and are not inside the wrap delimiters (ie. *,*)
189
167
else if ( character === options . DELIMITER . FIELD && charBefore !== options . DELIMITER . WRAP
190
168
&& charAfter !== options . DELIMITER . WRAP && ! stateVariables . insideWrapDelimiter
191
169
&& stateVariables . parsingValue ) {
192
170
splitLine . push ( line . substring ( stateVariables . startIndex , index ) ) ;
193
- stateVariables . insideWrapDelimiter = false ;
194
- stateVariables . parsingValue = true ;
195
- stateVariables . startIndex = ++ index ;
171
+ stateVariables . startIndex = index + 1 ;
196
172
}
197
173
else if ( character === options . DELIMITER . FIELD && charBefore === options . DELIMITER . WRAP
198
174
&& charAfter !== options . DELIMITER . WRAP ) {
199
175
stateVariables . insideWrapDelimiter = false ;
200
176
stateVariables . parsingValue = true ;
201
- stateVariables . startIndex = ++ index ;
202
- }
203
- // Otherwise...
204
- else {
205
- index ++ ;
177
+ stateVariables . startIndex = index + 1 ;
206
178
}
179
+ // Otherwise increment to the next character
180
+ index ++ ;
207
181
}
208
182
209
183
return splitLine ;
0 commit comments