mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-18 21:16:47 +00:00
fix(shared): the tour metrics pass the strict shared typecheck
computeTourMetrics indexed points[i - 1] and points[i], which the shared package's noUncheckedIndexedAccess reads as possibly undefined, so the shared typecheck in CI failed since the metrics moved there. It walks the points pairwise from the first one now; the numbers are the same.
This commit is contained in:
@@ -23,7 +23,8 @@ export interface TourMetrics {
|
|||||||
* ones for a tour saved offline, so both read them from here.
|
* ones for a tour saved offline, so both read them from here.
|
||||||
*/
|
*/
|
||||||
export function computeTourMetrics(points: readonly GeometryPoint[]): TourMetrics {
|
export function computeTourMetrics(points: readonly GeometryPoint[]): TourMetrics {
|
||||||
if (points.length < 2) {
|
const [first, ...rest] = points;
|
||||||
|
if (!first || rest.length === 0) {
|
||||||
return { distanceKm: 0, elevationGainM: null, elevationLossM: null, hasElevation: false };
|
return { distanceKm: 0, elevationGainM: null, elevationLossM: null, hasElevation: false };
|
||||||
}
|
}
|
||||||
const hasElevation = points.every((p) => p.length === 3 && Number.isFinite(p[2]));
|
const hasElevation = points.every((p) => p.length === 3 && Number.isFinite(p[2]));
|
||||||
@@ -31,13 +32,15 @@ export function computeTourMetrics(points: readonly GeometryPoint[]): TourMetric
|
|||||||
let distanceKm = 0;
|
let distanceKm = 0;
|
||||||
let gain = 0;
|
let gain = 0;
|
||||||
let loss = 0;
|
let loss = 0;
|
||||||
for (let i = 1; i < points.length; i++) {
|
let prev = first;
|
||||||
distanceKm += haversineMetres(points[i - 1][0], points[i - 1][1], points[i][0], points[i][1]) / 1000;
|
for (const point of rest) {
|
||||||
|
distanceKm += haversineMetres(prev[0], prev[1], point[0], point[1]) / 1000;
|
||||||
if (hasElevation) {
|
if (hasElevation) {
|
||||||
const delta = (points[i][2] as number) - (points[i - 1][2] as number);
|
const delta = (point[2] as number) - (prev[2] as number);
|
||||||
if (delta > 0) gain += delta;
|
if (delta > 0) gain += delta;
|
||||||
else loss += -delta;
|
else loss += -delta;
|
||||||
}
|
}
|
||||||
|
prev = point;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user