Skip to content

Commit 50e6fdd

Browse files
committed
Fix: Use custom max and min computations for large arrays.
1 parent 94d95f6 commit 50e6fdd

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

lib/math_helpers.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ export const haversineDistance = (point1: Point, point2: Point): number => {
5858
* @returns A duration object
5959
*/
6060

61-
6261
export const calculateDuration = (
6362
points: Point[],
6463
distance: Distance,
@@ -166,9 +165,17 @@ export const calculateElevation = (points: Point[]): Elevation => {
166165
}
167166
}
168167

168+
// Find the maximum and minimum elevation
169+
let max = elevation[0] ?? null
170+
let min = elevation[0] ?? null
171+
for (let i = 1; i < elevation.length; i++) {
172+
if (elevation[i] > max) max = elevation[i]
173+
if (elevation[i] < min) min = elevation[i]
174+
}
175+
169176
return {
170-
maximum: elevation.length ? Math.max(...elevation) : null,
171-
minimum: elevation.length ? Math.min(...elevation) : null,
177+
maximum: max,
178+
minimum: min,
172179
positive: Math.abs(dp) || null,
173180
negative: Math.abs(dn) || null,
174181
average: elevation.length ? sum / elevation.length : null,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@we-gold/gpxjs",
33
"author": "Weaver Goldman <we.goldm@gmail.com>",
44
"description": "GPX.js is a modern library for parsing GPX files and converting them to GeoJSON.",
5-
"version": "1.0.13",
5+
"version": "1.0.14",
66
"type": "module",
77
"license": "MIT",
88
"repository": {

0 commit comments

Comments
 (0)