@sports-alliance/sports-lib
Version:
A Library to for importing / exporting and processing GPX, TCX, FIT and JSON files from services such as Strava, Movescount, Garmin, Polar etc
73 lines (72 loc) • 2.31 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RouteFile = void 0;
const creator_1 = require("../creators/creator");
const file_type_enum_1 = require("../events/adapters/file-type.enum");
const stats_class_abstract_1 = require("../stats/stats.class.abstract");
const stats_utilities_1 = require("../stats/stats.utilities");
class RouteFile extends stats_class_abstract_1.StatsClassAbstract {
constructor(name = 'New Route File', srcFileType = file_type_enum_1.FileType.GPX, creator = new creator_1.Creator('Unknown Device'), routes = [], waypoints = [], createdAt = null) {
super();
this.routes = [];
this.waypoints = [];
this.name = name;
this.srcFileType = srcFileType;
this.creator = creator;
this.createdAt = createdAt;
this.routes = routes;
this.waypoints = waypoints;
}
addRoute(route) {
this.routes.push(route);
return this;
}
addRoutes(routes) {
routes.forEach(route => this.addRoute(route));
return this;
}
clearRoutes() {
this.routes = [];
return this;
}
getRoutes() {
return this.routes;
}
getFirstRoute() {
return this.getRoutes()[0];
}
hasRoutes() {
return this.routes.length > 0;
}
addWaypoint(waypoint) {
this.waypoints.push(waypoint);
return this;
}
addWaypoints(waypoints) {
waypoints.forEach(waypoint => this.addWaypoint(waypoint));
return this;
}
getWaypoints() {
return this.waypoints;
}
toJSON() {
const routeFileJSON = {
name: this.name,
srcFileType: this.srcFileType,
createdAt: this.createdAt ? this.createdAt.getTime() : null,
creator: this.creator.toJSON(),
stats: stats_utilities_1.StatsUtilities.serializeStats(this.getStats()),
routes: this.getRoutes().reduce((routes, route) => {
routes.push(route.toJSON());
return routes;
}, []),
waypoints: this.getWaypoints()
};
const id = this.getID();
if (id) {
routeFileJSON.id = id;
}
return routeFileJSON;
}
}
exports.RouteFile = RouteFile;