Skip to content

Commit 6e91d6d

Browse files
authored
Fix: handle null and undefined values (#2)
* Fix: handle null and undefined * Test: update fixtures and test for new fix
1 parent 5120f43 commit 6e91d6d

File tree

6 files changed

+70
-7
lines changed

6 files changed

+70
-7
lines changed

src/modules/convert-array-of-arrays-to-csv.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ export const convertArrayOfArraysToCSV = (data, { header, separator }) => {
66

77
if (header) {
88
header.forEach((headerEl, i) => {
9-
const includesSpecials = checkSpecialCharsAndEmpty(headerEl);
9+
const thisHeaderEl = headerEl || '';
10+
const includesSpecials = checkSpecialCharsAndEmpty(thisHeaderEl);
1011
csv +=
11-
(includesSpecials ? `"${headerEl}"` : headerEl) +
12+
(includesSpecials ? `"${thisHeaderEl}"` : thisHeaderEl) +
1213
(Object.entries(header).length - 1 === i ? '' : separator) +
1314
(Object.entries(header).length - 1 === i ? '\n' : '');
1415
});
1516
}
1617

1718
array.forEach((row) => {
1819
row.forEach((value, i) => {
19-
const includesSpecials = checkSpecialCharsAndEmpty(value);
20+
const thisValue = value || '';
21+
const includesSpecials = checkSpecialCharsAndEmpty(thisValue);
2022
csv +=
21-
(includesSpecials ? `"${value}"` : value) +
23+
(includesSpecials ? `"${thisValue}"` : thisValue) +
2224
(row.length - 1 === i ? '' : separator) +
2325
(row.length - 1 === i ? '\n' : '');
2426
});

src/modules/convert-array-of-objects-to-csv.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const convertArrayOfObjectsToCSV = (data, { header, separator }) => {
1717
array.forEach((row, idx) => {
1818
if (!header && idx === 0) {
1919
Object.entries(row).forEach((entry, i) => {
20-
const key = entry[0];
20+
const key = entry[0] ? entry[0] : '';
2121
const includesSpecials = checkSpecialCharsAndEmpty(key);
2222

2323
csv +=
@@ -27,7 +27,7 @@ export const convertArrayOfObjectsToCSV = (data, { header, separator }) => {
2727
});
2828
}
2929
Object.entries(row).forEach((entry, i) => {
30-
const value = entry[1];
30+
const value = entry[1] ? entry[1] : '';
3131
const includesSpecials = checkSpecialCharsAndEmpty(value);
3232

3333
csv +=

test/fixtures/data.js

Lines changed: 36 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 dataArrayWithNullAndUndefined = [
12+
[1, 'Mark', null, '@mdo'],
13+
[2, 'Jacob', 'Thornton', undefined],
14+
[3, 'Larry', 'the Bird', '@twitter'],
15+
];
16+
1117
const data = [
1218
{
1319
number: 1,
@@ -29,6 +35,27 @@ const data = [
2935
},
3036
];
3137

38+
const dataWithNullAndUndefined = [
39+
{
40+
number: 1,
41+
first: 'Mark',
42+
last: null,
43+
handle: '@mdo',
44+
},
45+
{
46+
number: 2,
47+
first: 'Jacob',
48+
last: 'Thornton',
49+
handle: undefined,
50+
},
51+
{
52+
number: 3,
53+
first: 'Larry',
54+
last: 'the Bird',
55+
handle: '@twitter',
56+
},
57+
];
58+
3259
export const dataArrayWithHeader = [
3360
...headerArray,
3461
...dataArray,
@@ -41,3 +68,12 @@ export const dataArrayWithoutHeader = [
4168
export const dataObject = [
4269
...data,
4370
];
71+
72+
export const dataObjectWithNullAndUndefined = [
73+
...dataWithNullAndUndefined,
74+
];
75+
76+
export const dataArrayWithHeaderAndNullAndUndefined = [
77+
...headerArray,
78+
...dataArrayWithNullAndUndefined,
79+
];

test/fixtures/expected-results.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ export const expectedResultObjectNoOptions = 'number,first,last,handle\n1,Mark,O
44

55
export const expectedResultObjectOnlyHeader = 'Number,First,Last,Handle\n1,Mark,Otto,@mdo\n2,Jacob,Thornton,@fat\n3,Larry,the Bird,@twitter\n';
66

7+
export const expectedResultObjectNullAndUndefined = 'Number,First,Last,Handle\n1,Mark,"",@mdo\n2,Jacob,Thornton,""\n3,Larry,the Bird,@twitter\n';
8+
9+
export const expectedResultArrayNullAndUndefined = 'number,first,last,handle\n1,Mark,"",@mdo\n2,Jacob,Thornton,""\n3,Larry,the Bird,@twitter\n';
10+
711
export const expectedResultObjectHeaderSeparatorSemicolon = 'Number;First;Last;Handle\n1;Mark;Otto;@mdo\n2;Jacob;Thornton;@fat\n3;Larry;the Bird;@twitter\n';
812

913
export const expectedResultObjecOnlySeparatorTab = 'number\tfirst\tlast\thandle\n1\tMark\tOtto\t@mdo\n2\tJacob\tThornton\t@fat\n3\tLarry\tthe Bird\t@twitter\n';

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { convertArrayOfArraysToCSV } from '../../lib/modules/convert-array-of-ar
33
import {
44
dataArrayWithHeader,
55
dataArrayWithoutHeader,
6+
dataArrayWithHeaderAndNullAndUndefined,
67
} from '../fixtures/data';
78
import {
89
optionsHeaderSeperatorSemicolon,
@@ -17,6 +18,7 @@ import {
1718
expectedResultArrayOnlyHeader,
1819
expectedResultArrayHeaderSeparatorSemicolon,
1920
expectedResultArrayOnlySeparatorTab,
21+
expectedResultArrayNullAndUndefined,
2022
} from '../fixtures/expected-results';
2123

2224
test('convertArrayOfArraysToCSV | array of arrays | with default options', () => {
@@ -54,3 +56,9 @@ test('convertArrayOfArraysToCSV | array of arrays | options: default header + se
5456

5557
expect(result).toBe(expectedResultArrayOnlySeparatorTab);
5658
});
59+
60+
test('convertArrayOfArraysToCSV | array of arrays with values of null and undefined | options: header + default separator', () => {
61+
const result = convertArrayOfArraysToCSV(dataArrayWithHeaderAndNullAndUndefined, optionsDefault);
62+
63+
expect(result).toBe(expectedResultArrayNullAndUndefined);
64+
});

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { convertArrayOfObjectsToCSV } from '../../lib/modules/convert-array-of-objects-to-csv';
22

3-
import { dataObject } from '../fixtures/data';
3+
import {
4+
dataObject,
5+
dataObjectWithNullAndUndefined,
6+
} from '../fixtures/data';
47
import {
58
optionsHeaderSeperatorSemicolon,
69
optionsHeaderSeparatorDefault,
@@ -13,6 +16,7 @@ import {
1316
expectedResultObjectHeaderSeparatorSemicolon,
1417
expectedResultObjectOnlyHeader,
1518
expectedResultObjecOnlySeparatorTab,
19+
expectedResultObjectNullAndUndefined,
1620
} from '../fixtures/expected-results';
1721

1822
test('convertArrayOfObjectsToCSV | array of objects | with default options', () => {
@@ -38,3 +42,12 @@ test('convertArrayOfObjectsToCSV | array of objects | options: default header +
3842

3943
expect(result).toBe(expectedResultObjecOnlySeparatorTab);
4044
});
45+
46+
test('convertArrayOfObjectsToCSV | array of objects with values of null and undefined | options: header + default separator', () => {
47+
const result = convertArrayOfObjectsToCSV(
48+
dataObjectWithNullAndUndefined,
49+
optionsHeaderSeparatorDefault,
50+
);
51+
52+
expect(result).toBe(expectedResultObjectNullAndUndefined);
53+
});

0 commit comments

Comments
 (0)