Skip to content

Commit a2da258

Browse files
committed
Preserve space characters, escape whenever double quotes are in the field
1 parent 1ba8f30 commit a2da258

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

lib/field-stringifier.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ class FieldStringifier {
55

66
stringify(value) {
77
if (typeof value === 'undefined' || value === null) return '';
8-
const str = String(value).trim();
8+
const str = String(value);
99
return this._needsQuote(str) ? `"${str.replace(/"/g, '""')}"` : str;
1010
}
1111

1212
_needsQuote(str) {
13-
return str.includes(',') || str.includes('\n') || str.startsWith('"') || str.endsWith('"');
13+
return str.includes(',') || str.includes('\n') || str.includes('"');
1414
}
1515

1616
}

test/lib/field-stringifier.test.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ describe('FieldStringifier', () => {
88
expect(stringifier.stringify('VALUE')).to.eql('VALUE');
99
});
1010

11+
it('preserves the whitespace characters', () => {
12+
const stringifier = new FieldStringifier();
13+
expect(stringifier.stringify(' VALUE\tA ')).to.eql(' VALUE\tA ');
14+
});
15+
1116
it('wraps a field value with double quotes if the field contains comma', () => {
1217
const stringifier = new FieldStringifier();
1318
expect(stringifier.stringify('VALUE,A')).to.eql('"VALUE,A"');
@@ -18,14 +23,14 @@ describe('FieldStringifier', () => {
1823
expect(stringifier.stringify('VALUE\nA')).to.eql('"VALUE\nA"');
1924
});
2025

21-
it('escapes double quotes if it is used on the edge of the field value', () => {
26+
it('wraps a field value with double quotes and escape the double quotes if they are used in the field', () => {
2227
const stringifier = new FieldStringifier();
23-
expect(stringifier.stringify('"VALUE')).to.eql('"""VALUE"');
28+
expect(stringifier.stringify('VALUE"A')).to.eql('"VALUE""A"');
2429
});
2530

26-
it('does not double quote or escape double quotes if it is used not on the edge of the field value', () => {
31+
it('escapes double quotes even if double quotes are only on the both edges of the field', () => {
2732
const stringifier = new FieldStringifier();
28-
expect(stringifier.stringify('VALUE"A')).to.eql('VALUE"A');
33+
expect(stringifier.stringify('"VALUE"')).to.eql('"""VALUE"""');
2934
});
3035

3136
it('converts a number into a string', () => {
@@ -61,7 +66,7 @@ describe('FieldStringifier', () => {
6166
expect(stringifier.stringify(obj)).to.eql('"Name: OBJECT,NAME"');
6267
});
6368

64-
it('escapes double quotes in a toString-ed field value if the value has double quotes on the edge', () => {
69+
it('escapes double quotes in a toString-ed field value if the value has double quotes', () => {
6570
const stringifier = new FieldStringifier();
6671
const obj = {
6772
name: 'OBJECT_NAME"',

0 commit comments

Comments
 (0)