Skip to content

Commit bba8369

Browse files
committed
fix: resolve issue with newline causing csv parser to reset state and parse partial field
resolves the issue reported in #240
1 parent d636bff commit bba8369

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/csv2json.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,21 @@ export const Csv2Json = function(options: FullCsv2JsonOptions) {
154154
stateVariables.startIndex = index + eolDelimiterLength;
155155
stateVariables.parsingValue = true;
156156
stateVariables.insideWrapDelimiter = charAfter === options.delimiter.wrap;
157-
} else if ((charBefore !== options.delimiter.wrap || stateVariables.justParsedDoubleQuote && charBefore === options.delimiter.wrap) &&
157+
} else if (character === options.delimiter.wrap && charBefore === options.delimiter.field &&
158+
!stateVariables.insideWrapDelimiter && !stateVariables.parsingValue) {
159+
// If we reached a wrap delimiter after a comma and we aren't inside a wrap delimiter
160+
161+
stateVariables.startIndex = index;
162+
stateVariables.insideWrapDelimiter = true;
163+
stateVariables.parsingValue = true;
164+
165+
// If the next character(s) are an EOL delimiter, then skip them so we don't parse what we've seen as another value
166+
if (utils.getNCharacters(csv, index + 1, eolDelimiterLength) === options.delimiter.eol) {
167+
index += options.delimiter.eol.length + 1; // Skip past EOL
168+
}
169+
}
170+
171+
else if ((charBefore !== options.delimiter.wrap || stateVariables.justParsedDoubleQuote && charBefore === options.delimiter.wrap) &&
158172
character === options.delimiter.wrap && utils.getNCharacters(csv, index + 1, eolDelimiterLength) === options.delimiter.eol) {
159173
// If we reach a wrap which is not preceded by a wrap delim and the next character is an EOL delim (ie. *"\n)
160174

@@ -174,13 +188,6 @@ export const Csv2Json = function(options: FullCsv2JsonOptions) {
174188
stateVariables.startIndex = index + 2; // next value starts after the field delimiter
175189
stateVariables.insideWrapDelimiter = false;
176190
stateVariables.parsingValue = false;
177-
} else if (character === options.delimiter.wrap && charBefore === options.delimiter.field &&
178-
!stateVariables.insideWrapDelimiter && !stateVariables.parsingValue) {
179-
// If we reached a wrap delimiter after a comma and we aren't inside a wrap delimiter
180-
181-
stateVariables.startIndex = index;
182-
stateVariables.insideWrapDelimiter = true;
183-
stateVariables.parsingValue = true;
184191
} else if (character === options.delimiter.wrap && charBefore === options.delimiter.field &&
185192
!stateVariables.insideWrapDelimiter && stateVariables.parsingValue) {
186193
// If we reached a wrap delimiter with a field delimiter after it (ie. ,"*)

0 commit comments

Comments
 (0)