From 65cb099c71cf3211f1c546176f2d10af23e47a11 Mon Sep 17 00:00:00 2001 From: papb Date: Thu, 22 Oct 2020 00:25:17 -0300 Subject: [PATCH] Handle carriage returns properly --- source/tsv2json.ts | 8 +++++++- test/test.ts | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/source/tsv2json.ts b/source/tsv2json.ts index 52de025..98fedf4 100644 --- a/source/tsv2json.ts +++ b/source/tsv2json.ts @@ -24,10 +24,11 @@ function extractFirstCell(tsvCharacters: NonemptyArray): CellExtractionR while (index < tsvCharacters.length) { const char = tsvCharacters[index]; + const nextChar = tsvCharacters[index + 1]; if (escapedMode) { if (char === '"') { - if (tsvCharacters[index + 1] === '"') { + if (nextChar === '"') { result.push('"'); index++; } else { @@ -37,6 +38,11 @@ function extractFirstCell(tsvCharacters: NonemptyArray): CellExtractionR result.push(char); } } else { + if (char === '\r' && nextChar === '\n') { + index++; + return done(true); + } + if (char === '\n') return done(true); if (char === '\t') return done(false); result.push(char); diff --git a/test/test.ts b/test/test.ts index 1e3959b..93648b5 100644 --- a/test/test.ts +++ b/test/test.ts @@ -44,6 +44,9 @@ test('tsv2json', t => { const complexJsonWithPreparedEmptyRow = complexJson.map(x => x.length === 0 ? [''] : x); t.deepEqual(tsv2json(complexFullyQuotedTsv), complexJsonWithPreparedEmptyRow); t.deepEqual(tsv2json(complexQuotedAsNeededTsv), complexJsonWithPreparedEmptyRow); + + t.deepEqual(tsv2json('"aa\r\nbb"'), [['aa\r\nbb']]); + t.deepEqual(tsv2json('aa\r\nbb'), [['aa'], ['bb']]); }); test('tsv2json throws on invalid input', t => {