-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataReader.js
44 lines (36 loc) · 1.33 KB
/
DataReader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*meant to read the data from the data generator csv then place into each day*/
const myForm = document.getElementById("myForm");
const csvFile = document.getElementById("csvFile");
const file = new XMLHttpRequest();
function csvToArray(str, delimiter = ",") {
//creates an array of the data
const headers = str.slice(0, str.indexOf("\n")).split(delimiter);
const rows = str.slice(str.indexOf("\n")+1).split("\n");
const arr = rows.map(function(row){
//when delimiting, only oin commas not in quotes
const values = row.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/);
const el = headers.reduce(function (object, header, index) {
object[header] = values[index];
return object;
}, {});
return el
});
return arr;
}
var text;
var purchaseData;
file.onload = function() {
//loading in the data from data generator
text = this.responseText;
//fixes instances where \r is used instead of \n
text = text.replace(/(?:\r\n|\r|\n)/g, '\n');
purchaseData = csvToArray(text);
purchaseData.forEach(element => {
element.Day = parseInt(element.Day);
element.Month = parseInt(element.Month);
element.Year = parseInt(element.Year);
element.Amount = parseFloat(element.Amount);
});
}
file.open("GET", "FakeData.csv");
file.send();