Skip to content

Commit

Permalink
Add support for LngLat conversion from {lat, lon} (#7090)(#7507)
Browse files Browse the repository at this point in the history
  • Loading branch information
bfrengley authored and ryanhamley committed Oct 30, 2018
1 parent cedefd6 commit 5549988
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
20 changes: 13 additions & 7 deletions src/geo/lng_lat.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ class LngLat {
}

/**
* Converts an array of two numbers to a `LngLat` object.
* Converts an array of two numbers or an object with `lng` and `lat` or `lon` and `lat` properties
* to a `LngLat` object.
*
* If a `LngLat` object is passed in, the function returns it unchanged.
*
* @param {LngLatLike} input An array of two numbers to convert, or a `LngLat` object to return.
* @param {LngLatLike} input An array of two numbers or object to convert, or a `LngLat` object to return.
* @returns {LngLat} A new `LngLat` object, if a conversion occurred, or the original `LngLat` object.
* @example
* var arr = [-73.9749, 40.7736];
Expand All @@ -111,21 +112,26 @@ class LngLat {
return new LngLat(Number(input[0]), Number(input[1]));
}
if (!Array.isArray(input) && typeof input === 'object' && input !== null) {
return new LngLat(Number(input.lng), Number(input.lat));
return new LngLat(
// flow can't refine this to have one of lng or lat, so we have to cast to any
Number('lng' in input ? (input: any).lng : (input: any).lon),
Number(input.lat)
);
}
throw new Error("`LngLatLike` argument must be specified as a LngLat instance, an object {lng: <lng>, lat: <lat>}, or an array of [<lng>, <lat>]");
throw new Error("`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>]");
}
}

/**
* A {@link LngLat} object, an array of two numbers representing longitude and latitude,
* or an object with `lng` and `lat` properties.
* or an object with `lng` and `lat` or `lon` and `lat` properties.
*
* @typedef {LngLat | {lng: number, lat: number} | [number, number]} LngLatLike
* @typedef {LngLat | {lng: number, lat: number} | {lon: number, lat: number} | [number, number]} LngLatLike
* @example
* var v1 = new mapboxgl.LngLat(-122.420679, 37.772537);
* var v2 = [-122.420679, 37.772537];
* var v3 = {lon: -122.420679, lat: 37.772537};
*/
export type LngLatLike = LngLat | {lng: number, lat: number} | [number, number];
export type LngLatLike = LngLat | {lng: number, lat: number} | {lon: number, lat: number} | [number, number];

export default LngLat;
5 changes: 4 additions & 1 deletion test/unit/geo/lng_lat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ test('LngLat', (t) => {
t.ok(LngLat.convert({lng: 0, lat: 10}) instanceof LngLat, 'convert creates a LngLat instance');
t.ok(LngLat.convert({lng: 0, lat: 0}) instanceof LngLat, 'convert creates a LngLat instance');
t.ok(LngLat.convert({lng: 0, lat: 0, elev: 0}) instanceof LngLat, 'convert creates a LngLat instance');
t.ok(LngLat.convert({lon: 0, lat: 10}) instanceof LngLat, 'convert creates a LngLat instance');
t.ok(LngLat.convert({lon: 0, lat: 0}) instanceof LngLat, 'convert creates a LngLat instance');
t.ok(LngLat.convert({lon: 0, lat: 0, elev: 0}) instanceof LngLat, 'convert creates a LngLat instance');
t.ok(LngLat.convert(new LngLat(0, 0)) instanceof LngLat, 'convert creates a LngLat instance');
t.throws(() => {
LngLat.convert(0, 10);
}, "`LngLatLike` argument must be specified as a LngLat instance, an object {lng: <lng>, lat: <lat>}, or an array of [<lng>, <lat>]", 'detects and throws on invalid input');
}, "`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();
});

Expand Down

0 comments on commit 5549988

Please sign in to comment.