Skip to content

Commit

Permalink
fix: resolve bug when a wrapped field value starts with an EOL delimiter
Browse files Browse the repository at this point in the history
fixes #265
  • Loading branch information
mrodrig committed Nov 24, 2024
1 parent 9551140 commit d253b84
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/csv2json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { excelBOM } from './constants';
import type { Csv2JsonParams, FullCsv2JsonOptions, HeaderField } from './types';
import * as utils from './utils';

export const Csv2Json = function(options: FullCsv2JsonOptions) {
export const Csv2Json = function (options: FullCsv2JsonOptions) {
const escapedWrapDelimiterRegex = new RegExp(options.delimiter.wrap + options.delimiter.wrap, 'g'),
excelBOMRegex = new RegExp('^' + excelBOM),
valueParserFn = options.parseValue && typeof options.parseValue === 'function' ? options.parseValue : JSON.parse;
Expand Down Expand Up @@ -166,9 +166,18 @@ export const Csv2Json = function(options: FullCsv2JsonOptions) {
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) &&
} else if (charBefore === options.delimiter.field && character === options.delimiter.wrap && charAfter === options.delimiter.eol) {
// We reached the start of a wrapped new field that begins with an EOL delimiter

// Retrieve the remaining value and add it to the split line list of values
splitLine.push(csv.substring(stateVariables.startIndex, index - 1));

stateVariables.startIndex = index;
stateVariables.parsingValue = true;
stateVariables.insideWrapDelimiter = true;
stateVariables.justParsedDoubleQuote = true;
index += 1;
} 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 Down

0 comments on commit d253b84

Please sign in to comment.