Skip to content

Commit 0a43346

Browse files
committed
Add almost-fully-function .csv function
1 parent 6d70263 commit 0a43346

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed

.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
export default class Split {
22
it(str: string): string[];
33
onDelimiter(str: string, del?: string): string[];
4+
csv(data: string, options?: Options): string[];
45
}
6+
7+
type Options = {
8+
headings?: boolean;
9+
splitOnColumns?: boolean;
10+
};

index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,32 @@ class Split {
2727
return str.split(/\s/g);
2828
}
2929
};
30+
31+
/**
32+
*
33+
* @param {string} data the csv data to be manipulated
34+
* @param options an object containing boolean values for "headings" and "splitOnColumns"
35+
* @return {string[]}
36+
*/
37+
csv = (data, { headings = true, splitOnColumns = false } = {}) => {
38+
let rows = data.split(/\r\n|\n|\r/g).map((row) => row.split(/,\s+/g));
39+
let returnData = [];
40+
41+
if (splitOnColumns) {
42+
rows.forEach((row, index) => {
43+
// TO DO: FIGURE OUT HOW TO ROTATE THIS SHIT
44+
});
45+
} else {
46+
returnData = rows;
47+
}
48+
49+
if (!headings) {
50+
returnData.shift();
51+
returnData.forEach((row) => row.shift());
52+
}
53+
54+
return returnData;
55+
};
3056
}
3157

3258
module.exports = new Split();

test.csv

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"Month", "1958", "1959", "1960"
2+
"JAN", 340, 360, 417
3+
"FEB", 318, 342, 391
4+
"MAR", 362, 406, 419
5+
"APR", 348, 396, 461
6+
"MAY", 363, 420, 472
7+
"JUN", 435, 472, 535
8+
"JUL", 491, 548, 622
9+
"AUG", 505, 559, 606
10+
"SEP", 404, 463, 508
11+
"OCT", 359, 407, 461
12+
"NOV", 310, 362, 390
13+
"DEC", 337, 405, 432

test/index.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const Split = require('../index');
2+
const fs = require('fs');
23

34
const expected = ['hello', 'from', 'split-it'];
45

@@ -66,4 +67,51 @@ describe('Split', () => {
6667
expect(Split.onDelimiter(str3, '*')).toEqual(expected);
6768
});
6869
});
70+
71+
describe('.csv', () => {
72+
const DATA = fs.readFileSync('airtravel.csv', { encoding: 'utf-8' });
73+
74+
test('splits csv data into a multi-array array', () => {
75+
let parsed = Split.csv(DATA);
76+
77+
expect(parsed.length).toEqual(13);
78+
79+
parsed.forEach((arr) => {
80+
expect(arr.length).toEqual(4);
81+
});
82+
});
83+
84+
test('removes headings when option is passed as false', () => {
85+
let parsed = Split.csv(DATA, { headings: false });
86+
87+
expect(parsed.length).toEqual(12);
88+
89+
parsed.forEach((arr) => {
90+
expect(arr.length).toEqual(3);
91+
});
92+
});
93+
94+
test('rotates data to make arrays of columns when option is passed', () => {
95+
let parsed = Split.csv(DATA, { splitOnColumns: true });
96+
let expectedHeadings = [
97+
'Month',
98+
'JAN',
99+
'FEB',
100+
'MAR',
101+
'APR',
102+
'MAY',
103+
'JUN',
104+
'JUL',
105+
'AUG',
106+
'SEP',
107+
'OCT',
108+
'NOV',
109+
'DEC',
110+
];
111+
112+
expect(parsed.length).toEqual(4);
113+
expect(parsed[0].length).toEqual(13);
114+
expect(parsed[0]).toEqual(expectedHeadings);
115+
});
116+
});
69117
});

0 commit comments

Comments
 (0)