Skip to content

Commit

Permalink
fix: resolve issue with newline causing csv parser to reset state and…
Browse files Browse the repository at this point in the history
… parse partial field

resolves the issue reported in #240
  • Loading branch information
mrodrig committed Oct 29, 2023
1 parent d636bff commit bba8369
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/csv2json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,21 @@ export const Csv2Json = function(options: FullCsv2JsonOptions) {
stateVariables.startIndex = index + eolDelimiterLength;
stateVariables.parsingValue = true;
stateVariables.insideWrapDelimiter = charAfter === options.delimiter.wrap;
} else if ((charBefore !== options.delimiter.wrap || stateVariables.justParsedDoubleQuote && charBefore === options.delimiter.wrap) &&
} else if (character === options.delimiter.wrap && charBefore === options.delimiter.field &&
!stateVariables.insideWrapDelimiter && !stateVariables.parsingValue) {
// If we reached a wrap delimiter after a comma and we aren't inside a wrap delimiter

stateVariables.startIndex = index;
stateVariables.insideWrapDelimiter = true;
stateVariables.parsingValue = true;

// If the next character(s) are an EOL delimiter, then skip them so we don't parse what we've seen as another value
if (utils.getNCharacters(csv, index + 1, eolDelimiterLength) === options.delimiter.eol) {
index += options.delimiter.eol.length + 1; // Skip past EOL
}
}

else if ((charBefore !== options.delimiter.wrap || stateVariables.justParsedDoubleQuote && charBefore === options.delimiter.wrap) &&
character === options.delimiter.wrap && utils.getNCharacters(csv, index + 1, eolDelimiterLength) === options.delimiter.eol) {
// If we reach a wrap which is not preceded by a wrap delim and the next character is an EOL delim (ie. *"\n)

Expand All @@ -174,13 +188,6 @@ export const Csv2Json = function(options: FullCsv2JsonOptions) {
stateVariables.startIndex = index + 2; // next value starts after the field delimiter
stateVariables.insideWrapDelimiter = false;
stateVariables.parsingValue = false;
} else if (character === options.delimiter.wrap && charBefore === options.delimiter.field &&
!stateVariables.insideWrapDelimiter && !stateVariables.parsingValue) {
// If we reached a wrap delimiter after a comma and we aren't inside a wrap delimiter

stateVariables.startIndex = index;
stateVariables.insideWrapDelimiter = true;
stateVariables.parsingValue = true;
} else if (character === options.delimiter.wrap && charBefore === options.delimiter.field &&
!stateVariables.insideWrapDelimiter && stateVariables.parsingValue) {
// If we reached a wrap delimiter with a field delimiter after it (ie. ,"*)
Expand Down

0 comments on commit bba8369

Please sign in to comment.