Skip to content

Commit 8b04e3d

Browse files
committed
fixed bug where bbox-array would return NaN if first point has NaN
1 parent bde3b27 commit 8b04e3d

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

bbox-array.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,20 @@
77
*/
88
function bboxArray(points, { nan_strategy = "throw" } = { nan_strategy: "throw" }) {
99
const count = points.length;
10-
const [x, y] = points[0];
11-
let xmin = x;
12-
let xmax = x;
13-
let ymin = y;
14-
let ymax = y;
15-
for (let i = 1; i < count; i++) {
10+
let xmin = null;
11+
let xmax = null;
12+
13+
let ymin = null;
14+
let ymax = null;
15+
for (let i = 0; i < count; i++) {
1616
const [x, y] = points[i];
1717
if (isNaN(x)) {
1818
if (nan_strategy === "throw") {
1919
throw new Error("[bbox-fns/bbox-array] encountered point with a NaN value: [" + x + ", " + y + "]");
2020
}
21+
} else if (xmin === null) {
22+
xmin = x;
23+
xmax = x;
2124
} else {
2225
if (x < xmin) xmin = x;
2326
else if (x > xmax) xmax = x;
@@ -26,6 +29,9 @@ function bboxArray(points, { nan_strategy = "throw" } = { nan_strategy: "throw"
2629
if (nan_strategy === "throw") {
2730
throw new Error("[bbox-fns/bbox-array] encountered point with a NaN value: [" + x + ", " + y + "]");
2831
}
32+
} else if (ymin === null) {
33+
ymin = y;
34+
ymax = y;
2935
} else {
3036
if (y < ymin) ymin = y;
3137
else if (y > ymax) ymax = y;

0 commit comments

Comments
 (0)