@@ -93,40 +93,65 @@ const Csv2Json = function(options) {
9393 // Next character
9494 charAfter = index < lastCharacterIndex ? line [ index + 1 ] : '' ;
9595
96- if ( index === lastCharacterIndex ) {
97- // If we reached the end of the line, add the remaining value
96+ if ( index === lastCharacterIndex && character === options . delimiter . field ) {
97+ // If we reached the end of the line and the current character is a field delimiter...
98+
99+ // Push the value for the field that we were parsing
100+ splitLine . push (
101+ // If the start index is the current index (and since the character is a comma),
102+ // then the value being parsed is an empty value accordingly, add an empty string
103+ stateVariables . startIndex === index
104+ ? ''
105+ // Otherwise there is a valid value, but we do not want to include the current character (field delimiter)
106+ : line . substring ( stateVariables . startIndex , index )
107+ ) ;
108+
109+ // Since the last character is a comma, there's still an additional implied field value trailing the comma.
110+ // Since this value is empty, we push an extra empty value
111+ splitLine . push ( '' ) ;
112+ } else if ( index === lastCharacterIndex ) {
113+ // Otherwise if we reached the end of the line (and current character is not a field delimiter)
114+
115+ // Retrieve the remaining value and add it to the split line list of values
98116 splitLine . push ( line . substring ( stateVariables . startIndex ) ) ;
99117 } else if ( character === options . delimiter . wrap && index === 0 ) {
100- // If the line starts with a wrap delimiter
118+ // If the line starts with a wrap delimiter (ie. "*)
119+
101120 stateVariables . insideWrapDelimiter = true ;
102121 stateVariables . parsingValue = true ;
103122 stateVariables . startIndex = index ;
104123 } else if ( character === options . delimiter . wrap && charAfter === options . delimiter . field ) {
105124 // If we reached a wrap delimiter with a field delimiter after it (ie. *",)
125+
106126 splitLine . push ( line . substring ( stateVariables . startIndex , index + 1 ) ) ;
107127 stateVariables . startIndex = index + 2 ; // next value starts after the field delimiter
108128 stateVariables . insideWrapDelimiter = false ;
109129 stateVariables . parsingValue = false ;
110- } else if ( character === options . delimiter . wrap && charBefore === options . delimiter . field && ! stateVariables . insideWrapDelimiter && stateVariables . parsingValue ) {
130+ } else if ( character === options . delimiter . wrap && charBefore === options . delimiter . field &&
131+ ! stateVariables . insideWrapDelimiter && stateVariables . parsingValue ) {
111132 // If we reached a wrap delimiter with a field delimiter after it (ie. ,"*)
133+
112134 splitLine . push ( line . substring ( stateVariables . startIndex , index - 1 ) ) ;
113135 stateVariables . insideWrapDelimiter = true ;
114136 stateVariables . parsingValue = true ;
115137 stateVariables . startIndex = index ;
116138 } else if ( character === options . delimiter . wrap && charAfter === options . delimiter . wrap ) {
117- // If we run into an escaped quote
139+ // If we run into an escaped quote (ie. "") skip past the second quote
140+
118141 index += 2 ;
119142 continue ;
120143 } else if ( character === options . delimiter . field && charBefore !== options . delimiter . wrap &&
121- // If we reached a field delimiter and are not inside the wrap delimiters (ie. *,*)
122144 charAfter !== options . delimiter . wrap && ! stateVariables . insideWrapDelimiter &&
123145 stateVariables . parsingValue ) {
124146 // If we reached a field delimiter and are not inside the wrap delimiters (ie. *,*)
147+
125148 splitLine . push ( line . substring ( stateVariables . startIndex , index ) ) ;
126149 stateVariables . startIndex = index + 1 ;
127150 } else if ( character === options . delimiter . field && charBefore === options . delimiter . wrap &&
128- // If we reached a field delimiter, the previous character was a wrap delimiter, and the next character is not a wrap delimiter (ie. ",*)
129151 charAfter !== options . delimiter . wrap && ! stateVariables . parsingValue ) {
152+ // If we reached a field delimiter, the previous character was a wrap delimiter, and the
153+ // next character is not a wrap delimiter (ie. ",*)
154+
130155 stateVariables . insideWrapDelimiter = false ;
131156 stateVariables . parsingValue = true ;
132157 stateVariables . startIndex = index + 1 ;
0 commit comments