Skip to content

Commit 58ec063

Browse files
authored
Fix: double quotes inside elements (closes #9) (#12)
* Fix: double quotes inside elements * Test: update tests for new fix
1 parent 2ea0633 commit 58ec063

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

src/helpers/append-element.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@ const separatorOrLineBreak = (length, elementIdx, separator) => (
55
length - 1 === elementIdx ? '\n' : separator
66
);
77

8+
const escapeDoubleQuotesInsideElement = (element) => {
9+
const thisElement = element.replace('"', '""');
10+
11+
return thisElement;
12+
};
13+
814
const appendElement = (element, lineLength, elementIdx, separator) => {
915
const includesSpecials = checkSpecialCharsAndEmpty(element);
1016

17+
let thisElement = element;
18+
19+
if (includesSpecials) {
20+
thisElement = escapeDoubleQuotesInsideElement(thisElement);
21+
}
22+
1123
return (
1224
includesSpecials
13-
? `"${element}"${separatorOrLineBreak(lineLength, elementIdx, separator)}`
14-
: `${element}${separatorOrLineBreak(lineLength, elementIdx, separator)}`
25+
? `"${thisElement}"${separatorOrLineBreak(lineLength, elementIdx, separator)}`
26+
: `${thisElement}${separatorOrLineBreak(lineLength, elementIdx, separator)}`
1527
);
1628
};
1729

test/fixtures/data.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ const dataArray = [
88
[3, 'Larry', 'the Bird', '@twitter'],
99
];
1010

11+
const dataArrayWithDoubleQuotes = [
12+
[1, 'Mark', 'Ot"to', '@mdo'],
13+
[2, 'Jacob', 'Thornton', '@fat'],
14+
[3, 'Larry', 'the Bird', '@twitter'],
15+
];
16+
1117
const dataArrayWithNullAndUndefined = [
1218
[1, 'Mark', null, '@mdo'],
1319
[2, 'Jacob', 'Thornton', undefined],
@@ -41,6 +47,27 @@ const data = [
4147
},
4248
];
4349

50+
const dataObjectWithDoubleQuotes = [
51+
{
52+
number: 1,
53+
first: 'Mark',
54+
last: 'Ot"to',
55+
handle: '@mdo',
56+
},
57+
{
58+
number: 2,
59+
first: 'Jacob',
60+
last: 'Thornton',
61+
handle: '@fat',
62+
},
63+
{
64+
number: 3,
65+
first: 'Larry',
66+
last: 'the Bird',
67+
handle: '@twitter',
68+
},
69+
];
70+
4471
const dataWithNullAndUndefined = [
4572
{
4673
number: 1,
@@ -67,6 +94,10 @@ export const dataArrayWithHeader = [
6794
...dataArray,
6895
];
6996

97+
export const dataArrayWithDoubleQuotesInsideElement = [
98+
...dataArrayWithDoubleQuotes,
99+
];
100+
70101
export const dataArrayWithoutHeader = [
71102
...dataArray,
72103
];
@@ -75,6 +106,10 @@ export const dataObject = [
75106
...data,
76107
];
77108

109+
export const dataObjectWithDoubleQuotesInsideElement = [
110+
...dataObjectWithDoubleQuotes,
111+
];
112+
78113
export const dataObjectWithNullAndUndefined = [
79114
...dataWithNullAndUndefined,
80115
];

test/fixtures/expected-results.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ export const expectedResultObjecOnlySeparatorTab = 'number\tfirst\tlast\thandle\
1616

1717
export const expectedResultArrayHeaderWithSpaces = '"number number",first,last,handle\n1,Mark,Otto,@mdo\n2,Jacob,Thornton,@fat\n3,Larry,"the Bird",@twitter\n';
1818

19+
export const expectedResultArrayWithDoubleQoutesInsideElement = '1,Mark,"Ot""to",@mdo\n2,Jacob,Thornton,@fat\n3,Larry,"the Bird",@twitter\n';
20+
1921
export const expectedResultArrayWithHeaderNoOptions = expectedResultObjectNoOptions;
2022

2123
export const expectedResultArrayOnlyHeader = expectedResultObjectOnlyHeader;
2224

2325
export const expectedResultArrayHeaderSeparatorSemicolon = expectedResultObjectHeaderSeparatorSemicolon; // eslint-disable-line
2426

2527
export const expectedResultArrayOnlySeparatorTab = expectedResultObjecOnlySeparatorTab;
28+
29+
export const expectedResultObjectWithDoubleQoutesInsideElement = `number,first,last,handle\n${expectedResultArrayWithDoubleQoutesInsideElement}`; // eslint-disable-line

test/modules/convert-array-of-arrays-to-csv.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
dataArrayWithoutHeader,
66
dataArrayWithHeaderAndNullAndUndefined,
77
dataArrayWithHeaderAndZero,
8+
dataArrayWithDoubleQuotesInsideElement,
89
} from '../fixtures/data';
910
import {
1011
optionsHeaderSeperatorSemicolon,
@@ -21,6 +22,7 @@ import {
2122
expectedResultArrayOnlySeparatorTab,
2223
expectedResultArrayNullAndUndefined,
2324
expectedResultArrayZero,
25+
expectedResultArrayWithDoubleQoutesInsideElement,
2426
} from '../fixtures/expected-results';
2527

2628
test('convertArrayOfArraysToCSV | array of arrays | with default options', () => {
@@ -29,6 +31,12 @@ test('convertArrayOfArraysToCSV | array of arrays | with default options', () =>
2931
expect(result).toBe(expectedResultArrayWithHeaderNoOptions);
3032
});
3133

34+
test('convertArrayOfArraysToCSV | array of arrays | with default options and double quotes in element', () => {
35+
const result = convertArrayOfArraysToCSV(dataArrayWithDoubleQuotesInsideElement, optionsDefault);
36+
37+
expect(result).toBe(expectedResultArrayWithDoubleQoutesInsideElement);
38+
});
39+
3240
test('convertArrayOfArraysToCSV | array of arrays | with default options and no header', () => {
3341
const result = convertArrayOfArraysToCSV(dataArrayWithoutHeader, optionsDefault);
3442

test/modules/convert-array-of-objects-to-csv.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { convertArrayOfObjectsToCSV } from '../../src/modules/convert-array-of-o
33
import {
44
dataObject,
55
dataObjectWithNullAndUndefined,
6+
dataObjectWithDoubleQuotesInsideElement,
67
} from '../fixtures/data';
78
import {
89
optionsHeaderSeperatorSemicolon,
@@ -17,9 +18,16 @@ import {
1718
expectedResultObjectOnlyHeader,
1819
expectedResultObjecOnlySeparatorTab,
1920
expectedResultObjectNullAndUndefined,
21+
expectedResultObjectWithDoubleQoutesInsideElement,
2022
} from '../fixtures/expected-results';
2123

2224
test('convertArrayOfObjectsToCSV | array of objects | with default options', () => {
25+
const result = convertArrayOfObjectsToCSV(dataObjectWithDoubleQuotesInsideElement, optionsDefault); // eslint-disable-line
26+
27+
expect(result).toBe(expectedResultObjectWithDoubleQoutesInsideElement);
28+
});
29+
30+
test('convertArrayOfObjectsToCSV | array of objects | with default options and double quotes in element', () => {
2331
const result = convertArrayOfObjectsToCSV(dataObject, optionsDefault);
2432

2533
expect(result).toBe(expectedResultObjectNoOptions);

0 commit comments

Comments
 (0)