From 2db6dba73e9ccbc486275fc01b96ed931dfbc1ea Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Sun, 3 Apr 2022 17:22:57 +0530 Subject: [PATCH] fix: use \n splitter - fallback behaviour for when - file is not spec conformant --- src/csvParser.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/csvParser.ts b/src/csvParser.ts index 8859efb8..3df54758 100644 --- a/src/csvParser.ts +++ b/src/csvParser.ts @@ -8,9 +8,9 @@ function unwrapDq(item: string): string { return item; } -function splitCsvBlock(text: string): string[] { - if (!text.endsWith('\r\n')) { - text += '\r\n'; +function splitCsvBlock(text: string, splitter: string = '\r\n'): string[] { + if (!text.endsWith(splitter)) { + text += splitter; } const lines = []; let line = ''; @@ -26,10 +26,15 @@ function splitCsvBlock(text: string): string[] { inDq = !inDq; } - if (!inDq && c === '\r' && text[i + 1] === '\n') { + const isEnd = [...splitter] + .slice(1) + .map((s, j) => text[i + j + 1] === s) + .every(Boolean); + + if (!inDq && c === splitter[0] && isEnd) { lines.push(line); line = ''; - i = i + 1; + i = i + splitter.length - 1; continue; } @@ -75,6 +80,9 @@ function splitCsvLine(line: string): string[] { export function parseCSV(text: string): string[][] { // Works on RFC 4180 - const rows = splitCsvBlock(text); + let rows = splitCsvBlock(text); + if (rows.length === 1) { + rows = splitCsvBlock(text, '\n'); + } return rows.map(splitCsvLine); }