Skip to content

Commit

Permalink
Letting LngLatBounds.extend accepts LngLatLike and LngLatBoundsLike…
Browse files Browse the repository at this point in the history
… as well. (mapbox#9293)

* Changing the input type of the extend function to LngLatLike and LngLatBoundsLike and fixing a bug identified in the function

* Updating the function's comment to show the correct parameter type.

* -Adding more test cases:
	- Extend with an array of 3 elements
	- Extend with an empty array
-Removing unnecessary comments.

* Adding a better documentation to be more clear on what  can be inputted in extend function.

* -Resolving the flow failures which was causing CI pipeline failure.
-Correcting the comments for the extend function.
  • Loading branch information
sgolbabaei authored and mike-unearth committed Mar 18, 2020
1 parent 7ac4b76 commit 5453f7e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/geo/lng_lat_bounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ class LngLatBounds {
}

/**
* Extend the bounds to include a given LngLat or LngLatBounds.
* Extend the bounds to include a given LngLatLike or LngLatBoundsLike.
*
* @param {LngLat|LngLatBounds} obj object to extend to
* @param {LngLatLike|LngLatBoundsLike} obj object to extend to
* @returns {LngLatBounds} `this`
*/
extend(obj: LngLat | LngLatBounds) {
extend(obj: LngLatLike | LngLatBoundsLike) {
const sw = this._sw,
ne = this._ne;
let sw2, ne2;
Expand All @@ -83,10 +83,12 @@ class LngLatBounds {

} else {
if (Array.isArray(obj)) {
if (obj.every(Array.isArray)) {
return this.extend(LngLatBounds.convert(obj));
if (obj.length === 4 || obj.every(Array.isArray)) {
const lngLatBoundsObj = ((obj: any): LngLatBoundsLike);
return this.extend(LngLatBounds.convert(lngLatBoundsObj));
} else {
return this.extend(LngLat.convert(obj));
const lngLatObj = ((obj: any): LngLatLike);
return this.extend(LngLat.convert(lngLatObj));
}
}
return this;
Expand Down
27 changes: 27 additions & 0 deletions test/unit/geo/lng_lat_bounds.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,20 @@ test('LngLatBounds', (t) => {
t.equal(bounds.getNorth(), 10);
t.equal(bounds.getEast(), 10);

bounds.extend([-20, -20, 100]);

t.equal(bounds.getSouth(), -20);
t.equal(bounds.getWest(), -20);
t.equal(bounds.getNorth(), 10);
t.equal(bounds.getEast(), 10);

t.end();
});

t.test('#extend with bounds', (t) => {
const bounds1 = new LngLatBounds([0, 0], [10, 10]);
const bounds2 = new LngLatBounds([-10, -10], [10, 10]);

bounds1.extend(bounds2);

t.equal(bounds1.getSouth(), -10);
Expand All @@ -81,6 +89,14 @@ test('LngLatBounds', (t) => {
t.equal(bounds1.getNorth(), 15);
t.equal(bounds1.getEast(), 15);

const bounds4 = new LngLatBounds([-20, -20, 20, 20]);
bounds1.extend(bounds4);

t.equal(bounds1.getSouth(), -20);
t.equal(bounds1.getWest(), -20);
t.equal(bounds1.getNorth(), 20);
t.equal(bounds1.getEast(), 20);

t.end();
});

Expand Down Expand Up @@ -125,6 +141,17 @@ test('LngLatBounds', (t) => {
t.end();
});

t.test('#extend with empty array', (t) => {
const point = new LngLat(0, 0);
const bounds = new LngLatBounds(point, point);

t.throws(() => {
bounds.extend([]);
}, "`LngLatLike` argument must be specified as a LngLat instance, an object {lng: <lng>, lat: <lat>}, an object {lon: <lng>, lat: <lat>}, or an array of [<lng>, <lat>]", 'detects and throws on invalid input');

t.end();
});

t.test('accessors', (t) => {
const sw = new LngLat(0, 0);
const ne = new LngLat(-10, -20);
Expand Down

0 comments on commit 5453f7e

Please sign in to comment.