Skip to content

Commit d253b84

Browse files
committed
fix: resolve bug when a wrapped field value starts with an EOL delimiter
fixes #265
1 parent 9551140 commit d253b84

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/csv2json.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { excelBOM } from './constants';
55
import type { Csv2JsonParams, FullCsv2JsonOptions, HeaderField } from './types';
66
import * as utils from './utils';
77

8-
export const Csv2Json = function(options: FullCsv2JsonOptions) {
8+
export const Csv2Json = function (options: FullCsv2JsonOptions) {
99
const escapedWrapDelimiterRegex = new RegExp(options.delimiter.wrap + options.delimiter.wrap, 'g'),
1010
excelBOMRegex = new RegExp('^' + excelBOM),
1111
valueParserFn = options.parseValue && typeof options.parseValue === 'function' ? options.parseValue : JSON.parse;
@@ -166,9 +166,18 @@ export const Csv2Json = function(options: FullCsv2JsonOptions) {
166166
if (utils.getNCharacters(csv, index + 1, eolDelimiterLength) === options.delimiter.eol) {
167167
index += options.delimiter.eol.length + 1; // Skip past EOL
168168
}
169-
}
170-
171-
else if ((charBefore !== options.delimiter.wrap || stateVariables.justParsedDoubleQuote && charBefore === options.delimiter.wrap) &&
169+
} else if (charBefore === options.delimiter.field && character === options.delimiter.wrap && charAfter === options.delimiter.eol) {
170+
// We reached the start of a wrapped new field that begins with an EOL delimiter
171+
172+
// Retrieve the remaining value and add it to the split line list of values
173+
splitLine.push(csv.substring(stateVariables.startIndex, index - 1));
174+
175+
stateVariables.startIndex = index;
176+
stateVariables.parsingValue = true;
177+
stateVariables.insideWrapDelimiter = true;
178+
stateVariables.justParsedDoubleQuote = true;
179+
index += 1;
180+
} else if ((charBefore !== options.delimiter.wrap || stateVariables.justParsedDoubleQuote && charBefore === options.delimiter.wrap) &&
172181
character === options.delimiter.wrap && utils.getNCharacters(csv, index + 1, eolDelimiterLength) === options.delimiter.eol) {
173182
// If we reach a wrap which is not preceded by a wrap delim and the next character is an EOL delim (ie. *"\n)
174183

0 commit comments

Comments
 (0)