Skip to content

Commit 2ea0633

Browse files
authored
Refactor: add append element and separator or line break functions (closes #8) (#11)
1 parent c237523 commit 2ea0633

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

src/helpers/append-element.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
import { checkSpecialCharsAndEmpty } from './check-special-chars-and-empty';
3+
4+
const separatorOrLineBreak = (length, elementIdx, separator) => (
5+
length - 1 === elementIdx ? '\n' : separator
6+
);
7+
8+
const appendElement = (element, lineLength, elementIdx, separator) => {
9+
const includesSpecials = checkSpecialCharsAndEmpty(element);
10+
11+
return (
12+
includesSpecials
13+
? `"${element}"${separatorOrLineBreak(lineLength, elementIdx, separator)}`
14+
: `${element}${separatorOrLineBreak(lineLength, elementIdx, separator)}`
15+
);
16+
};
17+
18+
export { appendElement };

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { checkSpecialCharsAndEmpty } from '../helpers/check-special-chars-and-empty';
1+
// import { checkSpecialCharsAndEmpty } from '../helpers/check-special-chars-and-empty';
2+
3+
import { appendElement } from '../helpers/append-element';
24

35
export const convertArrayOfArraysToCSV = (data, { header, separator }) => {
46
const array = [...data];
@@ -7,22 +9,16 @@ export const convertArrayOfArraysToCSV = (data, { header, separator }) => {
79
if (header) {
810
header.forEach((headerEl, i) => {
911
const thisHeaderEl = headerEl || '';
10-
const includesSpecials = checkSpecialCharsAndEmpty(thisHeaderEl);
11-
csv
12-
+= (includesSpecials ? `"${thisHeaderEl}"` : thisHeaderEl)
13-
+ (Object.keys(header).length - 1 === i ? '' : separator)
14-
+ (Object.keys(header).length - 1 === i ? '\n' : '');
12+
13+
csv += appendElement(thisHeaderEl, header.length, i, separator);
1514
});
1615
}
1716

1817
array.forEach((row) => {
1918
row.forEach((value, i) => {
2019
const thisValue = value || (value === 0 ? 0 : '');
21-
const includesSpecials = checkSpecialCharsAndEmpty(thisValue);
22-
csv
23-
+= (includesSpecials ? `"${thisValue}"` : thisValue)
24-
+ (row.length - 1 === i ? '' : separator)
25-
+ (row.length - 1 === i ? '\n' : '');
20+
21+
csv += appendElement(thisValue, row.length, i, separator);
2622
});
2723
});
2824

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,31 @@
1-
import { checkSpecialCharsAndEmpty } from '../helpers/check-special-chars-and-empty';
1+
import { appendElement } from '../helpers/append-element';
22

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

77
if (header) {
88
header.forEach((headerEl, i) => {
9-
const includesSpecials = checkSpecialCharsAndEmpty(headerEl);
10-
csv
11-
+= (includesSpecials ? `"${headerEl}"` : headerEl)
12-
+ (Object.keys(header).length - 1 === i ? '' : separator)
13-
+ (Object.keys(header).length - 1 === i ? '\n' : '');
9+
const thisHeaderEl = headerEl || '';
10+
11+
csv += appendElement(thisHeaderEl, header.length, i, separator);
1412
});
1513
}
1614

1715
array.forEach((row, idx) => {
16+
const thisRow = Object.keys(row);
1817
if (!header && idx === 0) {
19-
Object.keys(row).forEach((key, i) => {
18+
thisRow.forEach((key, i) => {
2019
const value = key || '';
21-
const includesSpecials = checkSpecialCharsAndEmpty(value);
2220

23-
csv
24-
+= (includesSpecials ? `"${value}"` : value)
25-
+ (Object.keys(row).length - 1 === i ? '' : separator)
26-
+ (Object.keys(row).length - 1 === i ? '\n' : '');
21+
csv += appendElement(value, thisRow.length, i, separator);
2722
});
2823
}
29-
Object.keys(row).forEach((key, i) => {
24+
25+
thisRow.forEach((key, i) => {
3026
const value = row[key] || '';
31-
const includesSpecials = checkSpecialCharsAndEmpty(value);
3227

33-
csv
34-
+= (includesSpecials ? `"${value}"` : value)
35-
+ (Object.keys(row).length - 1 === i ? '' : separator)
36-
+ (Object.keys(row).length - 1 === i ? '\n' : '');
28+
csv += appendElement(value, thisRow.length, i, separator);
3729
});
3830
});
3931

0 commit comments

Comments
 (0)