improved csv parsing
This commit is contained in:
parent
65a25ce564
commit
b2549234e2
58
main.js
58
main.js
|
@ -121,6 +121,46 @@ map.on("pointermove", e => {
|
|||
}
|
||||
});
|
||||
|
||||
// this function should take a csv line and parse it to an array
|
||||
function parseLine(line) {
|
||||
out = [];
|
||||
|
||||
while(line.length > 0) {
|
||||
//line starts with , -> empty
|
||||
if(line[0] === ',') {
|
||||
//empty string
|
||||
line = line.substr(1);
|
||||
out.push("");
|
||||
continue;
|
||||
}
|
||||
|
||||
var end = line.length;
|
||||
var start = 0;
|
||||
var closing_len = 1; //length ofclosing string
|
||||
// starts with "
|
||||
if(line[0] === '"') {
|
||||
start = 1;
|
||||
|
||||
//find second "
|
||||
end = line.indexOf('"', 1)-1;
|
||||
closing_len = 3; //warum 3 ??? ich verstehs nich ich hätte 2 gedacht
|
||||
} else {
|
||||
//use next , as end
|
||||
var newend = line.indexOf(',');
|
||||
if(newend > 0)
|
||||
end = newend;
|
||||
}
|
||||
|
||||
//getarg
|
||||
out.push(line.substr(start, end));
|
||||
|
||||
//remove arg from line
|
||||
line = line.substr(end+closing_len);
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
function showError(error) {
|
||||
var span = document.createElement('p');
|
||||
span.classList.add('error');
|
||||
|
@ -158,20 +198,26 @@ function addElement(elem, features) {
|
|||
}
|
||||
|
||||
function loadedFile(text, filename) {
|
||||
var lines = text.split("\n");
|
||||
//remove \r and split
|
||||
var lines = text.replaceAll("\r", "").split("\n");
|
||||
|
||||
var featuresLTE = [];
|
||||
var features = [];
|
||||
|
||||
//check for IOS
|
||||
if(lines[0] === "Date,ConnType,ConnDetails,Lat,Lon,Download,DownloadBytes,Upload,UploadBytes,Latency,ServerName,InternalIp,ExternalIp,Is SpeedTest VPN") {
|
||||
showError("IOS files are not supported. Only Android for now.");
|
||||
return;
|
||||
}
|
||||
|
||||
for(var i = 1; i < lines.length; ++i) {
|
||||
var line = lines[i];
|
||||
if(line.length < 10) continue;
|
||||
|
||||
var date = line.substr(1, 17);
|
||||
line = line.substr(20);
|
||||
var lineSplit = line.split(",");
|
||||
lineSplit = [date ,... lineSplit];
|
||||
var lineSplit = parseLine(line);
|
||||
|
||||
var elem = new Entry(lineSplit);
|
||||
if(elem.conntype == "LTE") {
|
||||
if(elem.conntype === "LTE") {
|
||||
addElement(elem, featuresLTE);
|
||||
} else {
|
||||
addElement(elem, features);
|
||||
|
|
Loading…
Reference in New Issue