Skip to content

Commit 17a7f6e

Browse files
authored
Fix: handle numbers correct (#17) (closes #16)
* Fix: handle floats correct * Test: add test case for floats
1 parent 9e0cdb8 commit 17a7f6e

File tree

6 files changed

+61
-3
lines changed

6 files changed

+61
-3
lines changed

src/helpers/check-special-chars-and-empty.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
export const checkSpecialCharsAndEmpty = (value) => {
22
const thisValue = value.toString().toLowerCase();
3-
4-
const hasSpecialChars = thisValue.includes('\n')
3+
let hasSpecialChars = false;
4+
if (typeof value === 'string') {
5+
hasSpecialChars = thisValue.includes('\n')
56
|| thisValue.includes('\t')
67
|| thisValue.includes(',')
78
|| thisValue.includes(';')
@@ -12,6 +13,7 @@ export const checkSpecialCharsAndEmpty = (value) => {
1213
|| thisValue.includes('´')
1314
|| thisValue.includes(' ')
1415
|| thisValue.length === 0;
16+
}
1517

1618
return hasSpecialChars;
1719
};

test/fixtures/data.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ const dataArrayWithZero = [
2626
[2, 'Larry', 'the Bird', '@twitter'],
2727
];
2828

29+
const dataArrayWithFloat = [
30+
[1.001, 'Mark', 'Otto', '@mdo'],
31+
[2.002, 'Jacob', 'Thornton', '@fat'],
32+
[3.33, 'Larry', 'the Bird', '@twitter'],
33+
];
34+
2935
const data = [
3036
{
3137
number: 1,
@@ -89,6 +95,27 @@ const dataWithNullAndUndefined = [
8995
},
9096
];
9197

98+
const dataWithFloat = [
99+
{
100+
number: 1.001,
101+
first: 'Mark',
102+
last: 'Otto',
103+
handle: '@mdo',
104+
},
105+
{
106+
number: 2.002,
107+
first: 'Jacob',
108+
last: 'Thornton',
109+
handle: '@fat',
110+
},
111+
{
112+
number: 3.33,
113+
first: 'Larry',
114+
last: 'the Bird',
115+
handle: '@twitter',
116+
},
117+
];
118+
92119
export const dataArrayWithHeader = [
93120
...headerArray,
94121
...dataArray,
@@ -114,11 +141,20 @@ export const dataObjectWithNullAndUndefined = [
114141
...dataWithNullAndUndefined,
115142
];
116143

144+
export const dataObjectWithFloat = [
145+
...dataWithFloat,
146+
];
147+
117148
export const dataArrayWithHeaderAndNullAndUndefined = [
118149
...headerArray,
119150
...dataArrayWithNullAndUndefined,
120151
];
121152

153+
export const dataArrayWithHeaderAndFloats = [
154+
...headerArray,
155+
...dataArrayWithFloat,
156+
];
157+
122158
export const dataArrayWithHeaderAndZero = [
123159
...headerArray,
124160
...dataArrayWithZero,

test/fixtures/expected-results.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ 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 expectedResultArrayWithFloats = 'number,first,last,handle\n1.001,Mark,Otto,@mdo\n2.002,Jacob,Thornton,@fat\n3.33,Larry,"the Bird",@twitter\n';
12+
13+
export const expectedResultObjectWithFloats = 'number,first,last,handle\n1.001,Mark,Otto,@mdo\n2.002,Jacob,Thornton,@fat\n3.33,Larry,"the Bird",@twitter\n';
14+
1115
export const expectedResultArrayZero = 'number,first,last,handle\n0,Mark,Otto,@mdo\n1,Jacob,Thornton,@fat\n2,Larry,"the Bird",@twitter\n';
1216

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

test/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import {
99
expectedResultArrayNoHeaderNoOptions,
1010
expectedResultObjectNoOptions,
11-
expectedResultArrayHeaderWithSpaces,gco
11+
expectedResultArrayHeaderWithSpaces,
1212
} from './fixtures/expected-results';
1313
import { optionsHeaderWithSpacesSeparatorDefault } from './fixtures/options';
1414

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
dataArrayWithHeaderAndNullAndUndefined,
77
dataArrayWithHeaderAndZero,
88
dataArrayWithDoubleQuotesInsideElement,
9+
dataArrayWithHeaderAndFloats,
910
} from '../fixtures/data';
1011
import {
1112
optionsHeaderSeperatorSemicolon,
@@ -23,6 +24,7 @@ import {
2324
expectedResultArrayNullAndUndefined,
2425
expectedResultArrayZero,
2526
expectedResultArrayWithDoubleQoutesInsideElement,
27+
expectedResultArrayWithFloats,
2628
} from '../fixtures/expected-results';
2729

2830
test('convertArrayOfArraysToCSV | array of arrays | with default options', () => {
@@ -78,3 +80,9 @@ test('convertArrayOfArraysToCSV | array of arrays with value of zero | with defa
7880

7981
expect(result).toBe(expectedResultArrayZero);
8082
});
83+
84+
test('convertArrayOfArraysToCSV | array of arrays with floats | with default options and header', () => {
85+
const result = convertArrayOfArraysToCSV(dataArrayWithHeaderAndFloats, optionsDefault);
86+
87+
expect(result).toBe(expectedResultArrayWithFloats);
88+
});

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
dataObject,
55
dataObjectWithNullAndUndefined,
66
dataObjectWithDoubleQuotesInsideElement,
7+
dataObjectWithFloat,
78
} from '../fixtures/data';
89
import {
910
optionsHeaderSeperatorSemicolon,
@@ -19,6 +20,7 @@ import {
1920
expectedResultObjecOnlySeparatorTab,
2021
expectedResultObjectNullAndUndefined,
2122
expectedResultObjectWithDoubleQoutesInsideElement,
23+
expectedResultObjectWithFloats,
2224
} from '../fixtures/expected-results';
2325

2426
test('convertArrayOfObjectsToCSV | array of objects | with default options', () => {
@@ -59,3 +61,9 @@ test('convertArrayOfObjectsToCSV | array of objects with values of null and unde
5961

6062
expect(result).toBe(expectedResultObjectNullAndUndefined);
6163
});
64+
65+
test('convertArrayOfObjectsToCSV | array of objects with float | options: default header', () => {
66+
const result = convertArrayOfObjectsToCSV(dataObjectWithFloat, optionsDefault);
67+
68+
expect(result).toBe(expectedResultObjectWithFloats);
69+
});

0 commit comments

Comments
 (0)