Skip to content

Commit 48cd02c

Browse files
authored
Fix: pass options from index to modules (#1)
* Fix: pass options from index * Test: update test for new options fix
1 parent ff5adcb commit 48cd02c

File tree

6 files changed

+37
-28
lines changed

6 files changed

+37
-28
lines changed

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { checkIfValid } from './helpers/check-if-valid';
22
import { convertArrayOfArraysToCSV } from './modules/convert-array-of-arrays-to-csv';
33
import { convertArrayOfObjectsToCSV } from './modules/convert-array-of-objects-to-csv';
44

5-
export const convertArrayToCSV = (data, { header, separator }) => {
5+
export const convertArrayToCSV = (data, { header, separator } = {}) => {
66
checkIfValid(data);
77

88
const thisOptions = {
9-
header: header || undefined,
9+
header,
1010
separator: separator || ',',
1111
};
1212

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { checkSpecialCharsAndEmpty } from '../helpers/check-special-chars-and-empty';
22

3-
export const convertArrayOfArraysToCSV = (data, { header, separator } = {}) => {
3+
export const convertArrayOfArraysToCSV = (data, { header, separator }) => {
44
const array = [...data];
55
let csv = '';
66

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { checkSpecialCharsAndEmpty } from '../helpers/check-special-chars-and-empty';
22

3-
export const convertArrayOfObjectsToCSV = (data, { header, separator } = {}) => {
3+
export const convertArrayOfObjectsToCSV = (data, { header, separator }) => {
44
const array = [...data];
55
let csv = '';
66

test/fixtures/options.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
export const optionsOnlyHeader = {
1+
export const optionsHeaderSeparatorDefault = {
22
header: ['Number', 'First', 'Last', 'Handle'],
3+
separator: ',',
34
};
45

56
export const optionsHeaderSeperatorSemicolon = {
67
header: ['Number', 'First', 'Last', 'Handle'],
78
separator: ';',
89
};
910

10-
export const optionsOnlySeperatorTab = {
11+
export const optionsHeaderDefaultSeperatorTab = {
12+
header: undefined,
1113
separator: '\t',
1214
};
15+
16+
export const optionsDefault = {
17+
header: undefined,
18+
separator: ',',
19+
};

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import {
66
} from '../fixtures/data';
77
import {
88
optionsHeaderSeperatorSemicolon,
9-
optionsOnlyHeader,
10-
optionsOnlySeperatorTab,
9+
optionsHeaderSeparatorDefault,
10+
optionsHeaderDefaultSeperatorTab,
11+
optionsDefault,
1112
} from '../fixtures/options';
1213

1314
import {
@@ -18,38 +19,38 @@ import {
1819
expectedResultArrayOnlySeparatorTab,
1920
} from '../fixtures/expected-results';
2021

21-
test('convertArrayOfArraysToCSV | array of arrays | with no options', () => {
22-
const result = convertArrayOfArraysToCSV(dataArrayWithHeader);
22+
test('convertArrayOfArraysToCSV | array of arrays | with default options', () => {
23+
const result = convertArrayOfArraysToCSV(dataArrayWithHeader, optionsDefault);
2324

2425
expect(result).toBe(expectedResultArrayWithHeaderNoOptions);
2526
});
2627

27-
test('convertArrayOfArraysToCSV | array of arrays | with no options and no header', () => {
28-
const result = convertArrayOfArraysToCSV(dataArrayWithoutHeader);
28+
test('convertArrayOfArraysToCSV | array of arrays | with default options and no header', () => {
29+
const result = convertArrayOfArraysToCSV(dataArrayWithoutHeader, optionsDefault);
2930

3031
expect(result).toBe(expectedResultArrayNoHeaderNoOptions);
3132
});
3233

33-
test('convertArrayOfArraysToCSV | array of arrays | with no options and no header', () => {
34-
const result = convertArrayOfArraysToCSV(dataArrayWithoutHeader);
34+
test('convertArrayOfArraysToCSV | array of arrays | with default options and no header', () => {
35+
const result = convertArrayOfArraysToCSV(dataArrayWithoutHeader, optionsDefault);
3536

3637
expect(result).toBe(expectedResultArrayNoHeaderNoOptions);
3738
});
3839

39-
test('convertArrayOfArraysToCSV | array of arrays | options: header + seperator semicolon', () => {
40+
test('convertArrayOfArraysToCSV | array of arrays | options: header + separator semicolon', () => {
4041
const result = convertArrayOfArraysToCSV(dataArrayWithoutHeader, optionsHeaderSeperatorSemicolon);
4142

4243
expect(result).toBe(expectedResultArrayHeaderSeparatorSemicolon);
4344
});
4445

45-
test('convertArrayOfArraysToCSV | array of arrays | options: header ', () => {
46-
const result = convertArrayOfArraysToCSV(dataArrayWithoutHeader, optionsOnlyHeader);
46+
test('convertArrayOfArraysToCSV | array of arrays | options: header + default separator', () => {
47+
const result = convertArrayOfArraysToCSV(dataArrayWithoutHeader, optionsHeaderSeparatorDefault);
4748

4849
expect(result).toBe(expectedResultArrayOnlyHeader);
4950
});
5051

51-
test('convertArrayOfArraysToCSV | array of arrays | options: separator tab ', () => {
52-
const result = convertArrayOfArraysToCSV(dataArrayWithHeader, optionsOnlySeperatorTab);
52+
test('convertArrayOfArraysToCSV | array of arrays | options: default header + separator tab', () => {
53+
const result = convertArrayOfArraysToCSV(dataArrayWithHeader, optionsHeaderDefaultSeperatorTab);
5354

5455
expect(result).toBe(expectedResultArrayOnlySeparatorTab);
5556
});

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { convertArrayOfObjectsToCSV } from '../../lib/modules/convert-array-of-o
33
import { dataObject } from '../fixtures/data';
44
import {
55
optionsHeaderSeperatorSemicolon,
6-
optionsOnlyHeader,
7-
optionsOnlySeperatorTab,
6+
optionsHeaderSeparatorDefault,
7+
optionsHeaderDefaultSeperatorTab,
8+
optionsDefault,
89
} from '../fixtures/options';
910

1011
import {
@@ -14,26 +15,26 @@ import {
1415
expectedResultObjecOnlySeparatorTab,
1516
} from '../fixtures/expected-results';
1617

17-
test('convertArrayOfObjectsToCSV | array of objects | with no options', () => {
18-
const result = convertArrayOfObjectsToCSV(dataObject);
18+
test('convertArrayOfObjectsToCSV | array of objects | with default options', () => {
19+
const result = convertArrayOfObjectsToCSV(dataObject, optionsDefault);
1920

2021
expect(result).toBe(expectedResultObjectNoOptions);
2122
});
2223

23-
test('convertArrayOfObjectsToCSV | array of objects | options: header + seperator semicolon', () => {
24+
test('convertArrayOfObjectsToCSV | array of objects | options: header + separator semicolon', () => {
2425
const result = convertArrayOfObjectsToCSV(dataObject, optionsHeaderSeperatorSemicolon);
2526

2627
expect(result).toBe(expectedResultObjectHeaderSeparatorSemicolon);
2728
});
2829

29-
test('convertArrayOfObjectsToCSV | array of objects | options: header', () => {
30-
const result = convertArrayOfObjectsToCSV(dataObject, optionsOnlyHeader);
30+
test('convertArrayOfObjectsToCSV | array of objects | options: header + default separator', () => {
31+
const result = convertArrayOfObjectsToCSV(dataObject, optionsHeaderSeparatorDefault);
3132

3233
expect(result).toBe(expectedResultObjectOnlyHeader);
3334
});
3435

35-
test('convertArrayOfObjectsToCSV | array of objects | options: separator tab', () => {
36-
const result = convertArrayOfObjectsToCSV(dataObject, optionsOnlySeperatorTab);
36+
test('convertArrayOfObjectsToCSV | array of objects | options: default header + separator tab', () => {
37+
const result = convertArrayOfObjectsToCSV(dataObject, optionsHeaderDefaultSeperatorTab);
3738

3839
expect(result).toBe(expectedResultObjecOnlySeparatorTab);
3940
});

0 commit comments

Comments
 (0)