Skip to content

Commit bd592e5

Browse files
authored
Fix: zero (0) in header and body elements (#19)
1 parent 3915ab4 commit bd592e5

File tree

7 files changed

+54
-7
lines changed

7 files changed

+54
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const convertArrayOfArraysToCSV = (data, { header, separator }) => {
66

77
if (header) {
88
header.forEach((headerEl, i) => {
9-
const thisHeaderEl = headerEl || '';
9+
const thisHeaderEl = headerEl || (headerEl === 0 ? 0 : '');
1010

1111
csv += appendElement(thisHeaderEl, header.length, i, separator);
1212
});

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

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

77
if (header) {
88
header.forEach((headerEl, i) => {
9-
const thisHeaderEl = headerEl || '';
9+
const thisHeaderEl = headerEl || (headerEl === 0 ? 0 : '');
1010

1111
csv += appendElement(thisHeaderEl, header.length, i, separator);
1212
});
@@ -16,14 +16,14 @@ export const convertArrayOfObjectsToCSV = (data, { header, separator }) => {
1616
const thisRow = Object.keys(row);
1717
if (!header && idx === 0) {
1818
thisRow.forEach((key, i) => {
19-
const value = key || '';
19+
const value = key || (key === 0 ? 0 : '');
2020

2121
csv += appendElement(value, thisRow.length, i, separator);
2222
});
2323
}
2424

2525
thisRow.forEach((key, i) => {
26-
const value = row[key] || '';
26+
const value = row[key] || (row[key] === 0 ? 0 : '');
2727

2828
csv += appendElement(value, thisRow.length, i, separator);
2929
});

test/fixtures/data.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,27 @@ const dataWithFloat = [
116116
},
117117
];
118118

119+
const dataWith0 = [
120+
{
121+
0: 0,
122+
first: 'Mark',
123+
last: 'Otto',
124+
'': '@mdo',
125+
},
126+
{
127+
0: 1,
128+
first: 'Jacob',
129+
last: 'Thornton',
130+
'': '@fat',
131+
},
132+
{
133+
0: 2,
134+
first: 'Larry',
135+
last: 'the Bird',
136+
'': '@twitter',
137+
},
138+
];
139+
119140
export const dataArrayWithHeader = [
120141
...headerArray,
121142
...dataArray,
@@ -145,6 +166,10 @@ export const dataObjectWithFloat = [
145166
...dataWithFloat,
146167
];
147168

169+
export const dataObjectWith0 = [
170+
...dataWith0,
171+
];
172+
148173
export const dataArrayWithHeaderAndNullAndUndefined = [
149174
...headerArray,
150175
...dataArrayWithNullAndUndefined,
@@ -156,6 +181,5 @@ export const dataArrayWithHeaderAndFloats = [
156181
];
157182

158183
export const dataArrayWithHeaderAndZero = [
159-
...headerArray,
160184
...dataArrayWithZero,
161185
];

test/fixtures/expected-results.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ export const expectedResultArrayWithFloats = 'number,first,last,handle\n1.001,Ma
1212

1313
export const expectedResultObjectWithFloats = 'number,first,last,handle\n1.001,Mark,Otto,@mdo\n2.002,Jacob,Thornton,@fat\n3.33,Larry,"the Bird",@twitter\n';
1414

15-
export const expectedResultArrayZero = 'number,first,last,handle\n0,Mark,Otto,@mdo\n1,Jacob,Thornton,@fat\n2,Larry,"the Bird",@twitter\n';
15+
export const expectedResultArrayZero = '0,first,last,""\n0,Mark,Otto,@mdo\n1,Jacob,Thornton,@fat\n2,Larry,"the Bird",@twitter\n';
16+
17+
export const expectedResultObjectZero = '0,first,last,""\n0,Mark,Otto,@mdo\n1,Jacob,Thornton,@fat\n2,Larry,"the Bird",@twitter\n';
1618

1719
export const expectedResultObjectHeaderSeparatorSemicolon = 'Number;First;Last;Handle\n1;Mark;Otto;@mdo\n2;Jacob;Thornton;@fat\n3;Larry;"the Bird";@twitter\n';
1820

test/fixtures/options.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ export const optionsDefault = {
2222
header: undefined,
2323
separator: ',',
2424
};
25+
26+
export const optionsHeaderZero = {
27+
header: [0, 'first', 'last', ''],
28+
separator: ',',
29+
};

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
optionsHeaderSeparatorDefault,
1414
optionsHeaderDefaultSeperatorTab,
1515
optionsDefault,
16+
optionsHeaderZero,
1617
} from '../fixtures/options';
1718

1819
import {
@@ -76,7 +77,7 @@ test('convertArrayOfArraysToCSV | array of arrays with values of null and undefi
7677
});
7778

7879
test('convertArrayOfArraysToCSV | array of arrays with value of zero | with default options and header', () => {
79-
const result = convertArrayOfArraysToCSV(dataArrayWithHeaderAndZero, optionsDefault);
80+
const result = convertArrayOfArraysToCSV(dataArrayWithHeaderAndZero, optionsHeaderZero);
8081

8182
expect(result).toBe(expectedResultArrayZero);
8283
});

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import {
55
dataObjectWithNullAndUndefined,
66
dataObjectWithDoubleQuotesInsideElement,
77
dataObjectWithFloat,
8+
dataObjectWith0,
89
} from '../fixtures/data';
910
import {
1011
optionsHeaderSeperatorSemicolon,
1112
optionsHeaderSeparatorDefault,
1213
optionsHeaderDefaultSeperatorTab,
1314
optionsDefault,
15+
optionsHeaderZero,
1416
} from '../fixtures/options';
1517

1618
import {
@@ -21,6 +23,7 @@ import {
2123
expectedResultObjectNullAndUndefined,
2224
expectedResultObjectWithDoubleQoutesInsideElement,
2325
expectedResultObjectWithFloats,
26+
expectedResultObjectZero,
2427
} from '../fixtures/expected-results';
2528

2629
test('convertArrayOfObjectsToCSV | array of objects | with default options', () => {
@@ -67,3 +70,15 @@ test('convertArrayOfObjectsToCSV | array of objects with float | options: defaul
6770

6871
expect(result).toBe(expectedResultObjectWithFloats);
6972
});
73+
74+
test('convertArrayOfObjectsToCSV | array of objects with value of zero | options: header with zero and "" + default separator', () => {
75+
const result = convertArrayOfObjectsToCSV(dataObjectWith0, optionsHeaderZero);
76+
77+
expect(result).toBe(expectedResultObjectZero);
78+
});
79+
80+
test('convertArrayOfObjectsToCSV | array of objects with value of zero | options: default header + default separator', () => {
81+
const result = convertArrayOfObjectsToCSV(dataObjectWith0, optionsDefault);
82+
83+
expect(result).toBe(expectedResultObjectZero);
84+
});

0 commit comments

Comments
 (0)