-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetCSV.js
More file actions
150 lines (121 loc) · 4.47 KB
/
getCSV.js
File metadata and controls
150 lines (121 loc) · 4.47 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*jslint plusplus: true, browser: true, devel: true */
/*global $, d3*/
window.getCSV = (function () {
"use strict";
function getFileNameFromURL() {
//This assumes that there will only be one parameter and it doesn't matter what it is
var fileName = window.location.search
.substr(1)
.split("=")[1];
return fileName;
}
function ajaxFile(fileName, ajaxCallback) {
var extension = ".csv";
if (fileName.length < 4 || fileName.substr(fileName.length - extension.length) !== extension) {
fileName += extension;
}
$.ajax('./csvs/' + fileName, {
dataType: 'text',
success: function (fileText) {
ajaxCallback(null, fileText);
},
error: function (jqXHR, textStatus, errorThrown) {
var errorText = "Ajax Error " + textStatus + ':' + errorThrown;
ajaxCallback(errorText, null);
}
});
}
function parseNewCSV(rawCSV) {
function camelCase(string) {
return string.toLowerCase().replace(/ \w/g, function (x) {
return x.slice(-1).toUpperCase();
}).replace(/ |s$/g, "");
}
function toNum(stringIn) {
var tempNum = parseInt(stringIn, 10);
if (isNaN(tempNum)) {
return 0;
}
return tempNum;
}
var transcribed = [];
var data = d3.csvParse(rawCSV, function (d) {
var row = {};
for (var col in d) {
row[camelCase(col)] = d[col];
}
return row;
});
// for some reason they didn't already do this
data.columns = data.columns.map(camelCase);
// Our main loop
data.forEach(function (row, index) {
// assuming that the time secions start with a number
var section = row.rowHeading.match(/^\d|initial/);
var currentSection = transcribed.length - 1;
// for the 'forcer'
if (index === 0) {
row.underwaterVolcano = row.volcano;
transcribed.push(row);
return;
}
// setup the skeleton if this is a new section
if (section) {
var timeZone = data.columns.reduce(function (timeZone, column, i) {
var timingDefault = 0;
if (column === "other") {
return timeZone;
}
if (i === 0) {
timeZone[column] = section.input;
} else {
//the ones are so that the initial values don't get weeded out later
if (row.rowHeading === "initial") {
timingDefault = 1;
}
//set it
timeZone[column] = {
value: +row[column] || 0,
timing: timingDefault,
text: ""
};
}
return timeZone;
}, {});
// underwaterVolcano is special because it needs to copy volcano
timeZone.underwaterVolcano = timeZone.volcano;
transcribed.push(timeZone);
} else {
//for the 'timing' and 'text' rows
for (var element in row) {
var valOut = row[element];
if (element !== "rowHeading" && element !== "other") {
//timing has to be a number
if (row.rowHeading === "timing") {
valOut = toNum(row[element]);
}
transcribed[currentSection][element][row.rowHeading] = valOut;
}
}
}
});
return transcribed;
}
return function (callBack) {
var fileName;
fileName = getFileNameFromURL();
if (!fileName) {
callBack("No filename in URL.", null);
return;
}
ajaxFile(fileName, function (error, fileText) {
var fileDataRaw;
if (error) {
callBack(error, null);
return;
}
fileDataRaw = parseNewCSV(fileText);
callBack(null, fileDataRaw);
});
};
}());