Skip to content

Commit

Permalink
Fix: Use custom max and min computations for large arrays.
Browse files Browse the repository at this point in the history
  • Loading branch information
We-Gold committed Dec 31, 2024
1 parent 94d95f6 commit 50e6fdd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/math_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export const haversineDistance = (point1: Point, point2: Point): number => {
* @returns A duration object
*/


export const calculateDuration = (
points: Point[],
distance: Distance,
Expand Down Expand Up @@ -166,9 +165,17 @@ export const calculateElevation = (points: Point[]): Elevation => {
}
}

// Find the maximum and minimum elevation
let max = elevation[0] ?? null
let min = elevation[0] ?? null
for (let i = 1; i < elevation.length; i++) {
if (elevation[i] > max) max = elevation[i]
if (elevation[i] < min) min = elevation[i]
}

return {
maximum: elevation.length ? Math.max(...elevation) : null,
minimum: elevation.length ? Math.min(...elevation) : null,
maximum: max,
minimum: min,
positive: Math.abs(dp) || null,
negative: Math.abs(dn) || null,
average: elevation.length ? sum / elevation.length : null,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@we-gold/gpxjs",
"author": "Weaver Goldman <[email protected]>",
"description": "GPX.js is a modern library for parsing GPX files and converting them to GeoJSON.",
"version": "1.0.13",
"version": "1.0.14",
"type": "module",
"license": "MIT",
"repository": {
Expand Down

0 comments on commit 50e6fdd

Please sign in to comment.