Skip to content

Commit 4e024bd

Browse files
aRandomKiwiaichbauer
authored andcommitted
Fix : handle zero values correctly (array of arrays) (#4)
Fix : handle zero values correctly (array of arrays)
1 parent d55ee8a commit 4e024bd

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
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
@@ -17,7 +17,7 @@ export const convertArrayOfArraysToCSV = (data, { header, separator }) => {
1717

1818
array.forEach((row) => {
1919
row.forEach((value, i) => {
20-
const thisValue = value || '';
20+
const thisValue = value || (value === 0 ? 0 : '');
2121
const includesSpecials = checkSpecialCharsAndEmpty(thisValue);
2222
csv +=
2323
(includesSpecials ? `"${thisValue}"` : thisValue) +

test/fixtures/data.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ const dataArrayWithNullAndUndefined = [
1414
[3, 'Larry', 'the Bird', '@twitter'],
1515
];
1616

17+
const dataArrayWithZero = [
18+
[0, 'Mark', 'Otto', '@mdo'],
19+
[1, 'Jacob', 'Thornton', '@fat'],
20+
[2, 'Larry', 'the Bird', '@twitter'],
21+
]
22+
1723
const data = [
1824
{
1925
number: 1,
@@ -77,3 +83,8 @@ export const dataArrayWithHeaderAndNullAndUndefined = [
7783
...headerArray,
7884
...dataArrayWithNullAndUndefined,
7985
];
86+
87+
export const dataArrayWithHeaderAndZero = [
88+
...headerArray,
89+
...dataArrayWithZero,
90+
];

test/fixtures/expected-results.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ export const expectedResultObjectNullAndUndefined = 'Number,First,Last,Handle\n1
88

99
export const expectedResultArrayNullAndUndefined = 'number,first,last,handle\n1,Mark,"",@mdo\n2,Jacob,Thornton,""\n3,Larry,the Bird,@twitter\n';
1010

11+
export const expectedResultArrayZero = 'number,first,last,handle\n0,Mark,Otto,@mdo\n1,Jacob,Thornton,@fat\n2,Larry,the Bird,@twitter\n';
12+
1113
export const expectedResultObjectHeaderSeparatorSemicolon = 'Number;First;Last;Handle\n1;Mark;Otto;@mdo\n2;Jacob;Thornton;@fat\n3;Larry;the Bird;@twitter\n';
1214

1315
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
@@ -4,6 +4,7 @@ import {
44
dataArrayWithHeader,
55
dataArrayWithoutHeader,
66
dataArrayWithHeaderAndNullAndUndefined,
7+
dataArrayWithHeaderAndZero,
78
} from '../fixtures/data';
89
import {
910
optionsHeaderSeperatorSemicolon,
@@ -19,6 +20,7 @@ import {
1920
expectedResultArrayHeaderSeparatorSemicolon,
2021
expectedResultArrayOnlySeparatorTab,
2122
expectedResultArrayNullAndUndefined,
23+
expectedResultArrayZero,
2224
} from '../fixtures/expected-results';
2325

2426
test('convertArrayOfArraysToCSV | array of arrays | with default options', () => {
@@ -62,3 +64,9 @@ test('convertArrayOfArraysToCSV | array of arrays with values of null and undefi
6264

6365
expect(result).toBe(expectedResultArrayNullAndUndefined);
6466
});
67+
68+
test('convertArrayOfArraysToCSV | array of arrays with value of zero | with default options and header', () => {
69+
const result = convertArrayOfArraysToCSV(dataArrayWithHeaderAndZero, optionsDefault);
70+
71+
expect(result).toBe(expectedResultArrayZero);
72+
});

0 commit comments

Comments
 (0)