refactor: change addPointsToTrack -> parseTrackPoints generator
This commit is contained in:
parent
b31d3e0e7d
commit
6b98655c92
|
@ -1,8 +0,0 @@
|
|||
class TrackInfo {
|
||||
constructor(track, trackData) {
|
||||
this.track = track;
|
||||
this.trackData = trackData;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TrackInfo;
|
|
@ -61,7 +61,7 @@ function replaceDollarNewlinesHack(body) {
|
|||
return body;
|
||||
}
|
||||
|
||||
function addPointsToTrack(trackInfo, body, format = null) {
|
||||
function* parseTrackPoints(body, format = null) {
|
||||
body = replaceDollarNewlinesHack(body);
|
||||
|
||||
const detectedFormat = format != null ? format : detectFormat(body);
|
||||
|
@ -80,10 +80,7 @@ function addPointsToTrack(trackInfo, body, format = null) {
|
|||
break;
|
||||
}
|
||||
|
||||
const points = trackInfo.trackData.points;
|
||||
for (const newPoint of parser(body)) {
|
||||
points.push(newPoint);
|
||||
}
|
||||
yield* parser(body);
|
||||
}
|
||||
|
||||
function detectFormat(body) {
|
||||
|
@ -257,4 +254,4 @@ function* parseObsver2(body) {
|
|||
}
|
||||
}
|
||||
|
||||
module.exports = { addPointsToTrack, detectFormat, parseObsver1, parseObsver2, replaceDollarNewlinesHack };
|
||||
module.exports = { parseTrackPoints, detectFormat, parseObsver1, parseObsver2, replaceDollarNewlinesHack };
|
||||
|
|
|
@ -1,17 +1,14 @@
|
|||
const { addPointsToTrack, parseObsver1, detectFormat, parseObsver2, replaceDollarNewlinesHack } = require('./tracks');
|
||||
const TrackInfo = require('./TrackInfo');
|
||||
const { parseTrackPoints, parseObsver1, detectFormat, parseObsver2, replaceDollarNewlinesHack } = require('./tracks');
|
||||
|
||||
const { test1, test2, test3 } = require('./_tracks_testdata');
|
||||
|
||||
describe('addPointsToTrack', () => {
|
||||
describe('parseTrackPoints', () => {
|
||||
it('is a function', () => {
|
||||
expect(typeof addPointsToTrack).toBe('function');
|
||||
expect(typeof parseTrackPoints).toBe('function');
|
||||
});
|
||||
|
||||
it('works on the sample data with an empty track', () => {
|
||||
const trackInfo = new TrackInfo({}, { points: [] });
|
||||
addPointsToTrack(trackInfo, test1);
|
||||
const points = trackInfo.trackData.points;
|
||||
const points = Array.from(parseTrackPoints(test1));
|
||||
expect(points).toHaveLength(324);
|
||||
expect(points[0]).toEqual({
|
||||
date: '12.07.2020',
|
||||
|
|
|
@ -6,7 +6,7 @@ const Comment = mongoose.model('Comment');
|
|||
const User = mongoose.model('User');
|
||||
const auth = require('../auth');
|
||||
const currentTracks = new Map();
|
||||
const { addPointsToTrack } = require('../../logic/tracks');
|
||||
const { parseTrackPoints } = require('../../logic/tracks');
|
||||
const wrapRoute = require('../../_helpers/wrapRoute');
|
||||
|
||||
// Preload track objects on routes with ':track'
|
||||
|
@ -155,8 +155,7 @@ router.post(
|
|||
track.trackData = trackData._id;
|
||||
|
||||
if (req.body.track.body && req.body.track.body.trim()) {
|
||||
trackData.points = [];
|
||||
addPointsToTrack({ trackData }, track.body);
|
||||
trackData.points = Array.from(parseTrackPoints(track.body));
|
||||
}
|
||||
|
||||
track.author = user;
|
||||
|
@ -242,7 +241,7 @@ router.post(
|
|||
}
|
||||
|
||||
const trackData = await TrackData.findById(track.trackData);
|
||||
addPointsToTrack({ trackData }, track.body);
|
||||
trackData.points = Array.from(parseTrackPoints(track.body));
|
||||
await trackData.save();
|
||||
|
||||
// We are done with this track, it is complete.
|
||||
|
@ -292,8 +291,7 @@ router.put(
|
|||
trackData = new TrackData();
|
||||
req.track.trackData = trackData._id;
|
||||
}
|
||||
trackData.points = [];
|
||||
addPointsToTrack({ trackData }, req.track.body);
|
||||
trackData.points = Array.from(parseTrackPoints(req.track.body));
|
||||
await trackData.save();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue