refactor(logic/tracks): clean up code

This commit is contained in:
Paul Bienkowski 2020-11-17 18:24:49 +01:00
parent 8f5237a182
commit 214da0c9f8

View file

@ -1,8 +1,23 @@
function _parseFloat(token) {
var f = parseFloat(token);
if (isNaN(f)) {
f = parseFloat(token.substring(0, 10));
}
if (isNaN(f)) {
f = 0.0;
}
return f;
}
module.exports.addPointsToTrack = function addPointsToTrack(track, body) { module.exports.addPointsToTrack = function addPointsToTrack(track, body) {
var num = 0; var num = 0;
var start = 0; var start = 0;
var end = 0; var end = 0;
//console.log("len"+body.length);
// reference to the array we will mutate
var points = track.trackData.points;
var currentPoint;
while (end < body.length) { while (end < body.length) {
start = end; start = end;
while (body[end] != ';' && body[end] != '$' && end < body.length) { while (body[end] != ';' && body[end] != '$' && end < body.length) {
@ -15,11 +30,8 @@ module.exports.addPointsToTrack = function addPointsToTrack(track, body) {
if (end < body.length) { if (end < body.length) {
var token = body.substr(start, end - start); var token = body.substr(start, end - start);
end++; end++;
if (token.length > 0) {
//console.log(token);
//console.log("num:"+num);
//console.log("end:"+end);
if (token.length > 0) {
if (num == 0 && token == 'Date') { if (num == 0 && token == 'Date') {
// we have a header line, ignore it for now, TODO parse it // we have a header line, ignore it for now, TODO parse it
if (end < body.length) { if (end < body.length) {
@ -30,77 +42,65 @@ module.exports.addPointsToTrack = function addPointsToTrack(track, body) {
num = 100; num = 100;
} }
} }
if (num == 0) {
track.trackData.points.push({ switch (num) {
date: 'dummy', case 0:
time: '', currentPoint = {
latitude: '', date: 'dummy',
longitude: '', time: '',
course: '', latitude: '',
speed: '', longitude: '',
d1: '', course: '',
d2: '', speed: '',
flag: '', d1: '',
private: '', d2: '',
}); flag: '',
track.trackData.points[track.trackData.points.length - 1].date = token; private: '',
num++; };
} else if (num == 1) {
track.trackData.points[track.trackData.points.length - 1].time = token; points.push(currentPoint);
num++;
} else if (num == 2) { currentPoint.date = token;
var f = parseFloat(token); break;
if (isNaN(f)) {
f = parseFloat(token.substring(0, 10)); case 1:
} currentPoint.time = token;
if (isNaN(f)) { break;
f = 0.0;
} case 2:
track.trackData.points[track.trackData.points.length - 1].latitude = f; currentPoint.latitude = _parseFloat(token);
num++; break;
} else if (num == 3) {
var f = parseFloat(token); case 3:
if (isNaN(f)) { currentPoint.longitude = _parseFloat(token);
f = parseFloat(token.substring(0, 10)); break;
}
if (isNaN(f)) { case 4:
f = 0.0; currentPoint.course = _parseFloat(token);
} break;
track.trackData.points[track.trackData.points.length - 1].longitude = f;
num++; case 5:
} else if (num == 4) { currentPoint.speed = _parseFloat(token);
var f = parseFloat(token); break;
if (isNaN(f)) {
f = parseFloat(token.substring(0, 10)); case 6:
} currentPoint.d1 = token;
if (isNaN(f)) { break;
f = 0.0;
} case 7:
track.trackData.points[track.trackData.points.length - 1].course = f; currentPoint.d2 = token;
num++; break;
} else if (num == 5) {
var f = parseFloat(token); case 8:
if (isNaN(f)) { currentPoint.flag = token;
f = parseFloat(token.substring(0, 10)); break;
}
if (isNaN(f)) { case 9:
f = 0.0; currentPoint.private = token;
} break;
track.trackData.points[track.trackData.points.length - 1].speed = f;
num++;
} else if (num == 6) {
track.trackData.points[track.trackData.points.length - 1].d1 = token;
num++;
} else if (num == 7) {
track.trackData.points[track.trackData.points.length - 1].d2 = token;
num++;
} else if (num == 8) {
track.trackData.points[track.trackData.points.length - 1].flag = token;
num++;
} else if (num == 9) {
track.trackData.points[track.trackData.points.length - 1].private = token;
num++;
} }
num++;
} }
} }
} }