diff --git a/lib/field-stringifier.js b/lib/field-stringifier.js index a8bf9c3..d4404c2 100644 --- a/lib/field-stringifier.js +++ b/lib/field-stringifier.js @@ -6,8 +6,11 @@ class FieldStringifier { stringify(value) { if (typeof value === 'undefined' || value === null) return ''; const str = String(value).trim(); - const needsQuotes = str.includes(',') || str.startsWith('"') || str.endsWith('"'); - return needsQuotes ? `"${str.replace(/"/g, '""')}"` : str; + return this._needsQuote(str) ? `"${str.replace(/"/g, '""')}"` : str; + } + + _needsQuote(str) { + return str.includes(',') || str.includes('\n') || str.startsWith('"') || str.endsWith('"'); } } diff --git a/test/lib/field-stringifier.test.js b/test/lib/field-stringifier.test.js index 4478192..d5863c7 100644 --- a/test/lib/field-stringifier.test.js +++ b/test/lib/field-stringifier.test.js @@ -13,6 +13,11 @@ describe('FieldStringifier', () => { expect(stringifier.stringify('VALUE,A')).to.eql('"VALUE,A"'); }); + it('wraps a field value with double quotes if the field contains newline', () => { + const stringifier = new FieldStringifier(); + expect(stringifier.stringify('VALUE\nA')).to.eql('"VALUE\nA"'); + }); + it('escapes double quotes if it is used on the edge of the field value', () => { const stringifier = new FieldStringifier(); expect(stringifier.stringify('"VALUE')).to.eql('"""VALUE"');