Skip to content

Commit

Permalink
Add flowtype definitions for LngLat. (mapbox#4624)
Browse files Browse the repository at this point in the history
  • Loading branch information
vicapow authored and mourner committed Apr 24, 2017
1 parent d7945c3 commit f0dafd0
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions src/geo/lng_lat.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
// @flow

const wrap = require('../util/util').wrap;

Expand All @@ -21,7 +22,9 @@ const wrap = require('../util/util').wrap;
* @see [Create a timeline animation](https://www.mapbox.com/mapbox-gl-js/example/timeline-animation/)
*/
class LngLat {
constructor(lng, lat) {
lng: number;
lat: number;
constructor(lng: number, lat: number) {
if (isNaN(lng) || isNaN(lat)) {
throw new Error(`Invalid LngLat object: (${lng}, ${lat})`);
}
Expand Down Expand Up @@ -61,7 +64,7 @@ class LngLat {
* var snapped = ll.wrapToBestWorld(mapCenter);
* snapped; // = { lng: -190, lat: 0 }
*/
wrapToBestWorld(center) {
wrapToBestWorld(center: LngLat) {
const wrapped = new LngLat(this.lng, this.lat);

if (Math.abs(this.lng - center.lng) > 180) {
Expand Down Expand Up @@ -98,30 +101,33 @@ class LngLat {
toString() {
return `LngLat(${this.lng}, ${this.lat})`;
}
}

/**
* Converts an array of two numbers 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.
* @returns {LngLat} A new `LngLat` object, if a conversion occurred, or the original `LngLat` object.
* @example
* var arr = [-73.9749, 40.7736];
* var ll = mapboxgl.LngLat.convert(arr);
* ll; // = LngLat {lng: -73.9749, lat: 40.7736}
*/
LngLat.convert = function (input) {
if (input instanceof LngLat) {
return input;
} else if (input && input.hasOwnProperty('lng') && input.hasOwnProperty('lat')) {
return new LngLat(input.lng, input.lat);
} else if (Array.isArray(input) && input.length === 2) {
return new LngLat(input[0], input[1]);
} else {
/**
* Converts an array of two numbers 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.
* @returns {LngLat} A new `LngLat` object, if a conversion occurred, or the original `LngLat` object.
* @example
* var arr = [-73.9749, 40.7736];
* var ll = mapboxgl.LngLat.convert(arr);
* ll; // = LngLat {lng: -73.9749, lat: 40.7736}
*/
static convert(input: mixed): LngLat {
if (input instanceof LngLat) {
return input;
}
if (Array.isArray(input) && input.length === 2) {
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));
}
throw new Error("`LngLatLike` argument must be specified as a LngLat instance, an object {lng: <lng>, lat: <lat>}, or an array of [<lng>, <lat>]");
}
};
}

/*:: export type LngLatLike = LngLat | {lng: number, lat: number} | [number, number]; */

module.exports = LngLat;

0 comments on commit f0dafd0

Please sign in to comment.