Skip to content

Commit

Permalink
fix: prevent crash when float is passed as zoom value (#199)
Browse files Browse the repository at this point in the history
* fix: prevent crash when float  is passed as `zoom` value
`zoom` is internally used to index into `trees`. some leaflet functions (e.g. `.flyTo()`) interpolate zoom values in which case calling methods like `.getClusters` causes a crash

* Update index.js

Co-authored-by: Vladimir Agafonkin <[email protected]>

Co-authored-by: Vladimir Agafonkin <[email protected]>
  • Loading branch information
fspoettel and mourner authored Apr 5, 2022
1 parent 4242dfd commit 2edab24
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export default class Supercluster {
}

_limitZoom(z) {
return Math.max(this.options.minZoom, Math.min(+z, this.options.maxZoom + 1));
return Math.max(this.options.minZoom, Math.min(Math.floor(+z), this.options.maxZoom + 1));
}

_cluster(points, zoom) {
Expand Down
6 changes: 6 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ test('does not crash on weird bbox values', (t) => {
t.end();
});

test('does not crash on non-integer zoom values', (t) => {
const index = new Supercluster().load(places.features);
t.ok(index.getClusters([179, -10, -177, 10], 1.25));
t.end();
});

test('makes sure same-location points are clustered', (t) => {
const index = new Supercluster({
maxZoom: 20,
Expand Down

0 comments on commit 2edab24

Please sign in to comment.