Skip to content

Commit

Permalink
convert geo classes to ES6 class syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Oct 19, 2016
1 parent b52185b commit 440889f
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 181 deletions.
33 changes: 16 additions & 17 deletions js/geo/coordinate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

module.exports = Coordinate;

/**
* A coordinate is a column, row, zoom combination, often used
* as the data component of a tile.
Expand All @@ -11,13 +9,12 @@ module.exports = Coordinate;
* @param {number} zoom
* @private
*/
function Coordinate(column, row, zoom) {
this.column = column;
this.row = row;
this.zoom = zoom;
}

Coordinate.prototype = {
class Coordinate {
constructor(column, row, zoom) {
this.column = column;
this.row = row;
this.zoom = zoom;
}

/**
* Create a clone of this coordinate that can be mutated without
Expand All @@ -31,9 +28,9 @@ Coordinate.prototype = {
* // not modify it.
* c2.zoom = 2;
*/
clone: function() {
clone() {
return new Coordinate(this.column, this.row, this.zoom);
},
}

/**
* Zoom this coordinate to a given zoom level. This returns a new
Expand All @@ -47,7 +44,7 @@ Coordinate.prototype = {
* var c2 = coord.zoomTo(1);
* c2 // equals new Coordinate(0, 0, 1);
*/
zoomTo: function(zoom) { return this.clone()._zoomTo(zoom); },
zoomTo(zoom) { return this.clone()._zoomTo(zoom); }

/**
* Subtract the column and row values of this coordinate from those
Expand All @@ -58,20 +55,22 @@ Coordinate.prototype = {
* @returns {Coordinate} result
* @private
*/
sub: function(c) { return this.clone()._sub(c); },
sub(c) { return this.clone()._sub(c); }

_zoomTo: function(zoom) {
_zoomTo(zoom) {
const scale = Math.pow(2, zoom - this.zoom);
this.column *= scale;
this.row *= scale;
this.zoom = zoom;
return this;
},
}

_sub: function(c) {
_sub(c) {
c = c.zoomTo(this.zoom);
this.column -= c.column;
this.row -= c.row;
return this;
}
};
}

module.exports = Coordinate;
93 changes: 47 additions & 46 deletions js/geo/lng_lat.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

module.exports = LngLat;

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

/**
Expand All @@ -13,7 +11,6 @@ const wrap = require('../util/util').wrap;
* can also accept an `Array` of two numbers and will perform an implicit conversion.
* This flexible type is documented as [`LngLatLike`](#LngLatLike).
*
* @class LngLat
* @param {number} lng Longitude, measured in degrees.
* @param {number} lat Latitude, measured in degrees.
* @example
Expand All @@ -23,53 +20,55 @@ const wrap = require('../util/util').wrap;
* @see [Highlight features within a bounding box](https://www.mapbox.com/mapbox-gl-js/example/using-box-queryrenderedfeatures/)
* @see [Create a timeline animation](https://www.mapbox.com/mapbox-gl-js/example/timeline-animation/)
*/
function LngLat(lng, lat) {
if (isNaN(lng) || isNaN(lat)) {
throw new Error(`Invalid LngLat object: (${lng}, ${lat})`);
class LngLat {
constructor(lng, lat) {
if (isNaN(lng) || isNaN(lat)) {
throw new Error(`Invalid LngLat object: (${lng}, ${lat})`);
}
this.lng = +lng;
this.lat = +lat;
if (this.lat > 90 || this.lat < -90) {
throw new Error('Invalid LngLat latitude value: must be between -90 and 90');
}
}
this.lng = +lng;
this.lat = +lat;
if (this.lat > 90 || this.lat < -90) {
throw new Error('Invalid LngLat latitude value: must be between -90 and 90');
}
}

/**
* Returns a new `LngLat` object whose longitude is wrapped to the range (-180, 180).
*
* @returns {LngLat} The wrapped `LngLat` object.
* @example
* var ll = new mapboxgl.LngLat(286.0251, 40.7736);
* var wrapped = ll.wrap();
* wrapped.lng; // = -73.9749
*/
LngLat.prototype.wrap = function () {
return new LngLat(wrap(this.lng, -180, 180), this.lat);
};
/**
* Returns a new `LngLat` object whose longitude is wrapped to the range (-180, 180).
*
* @returns {LngLat} The wrapped `LngLat` object.
* @example
* var ll = new mapboxgl.LngLat(286.0251, 40.7736);
* var wrapped = ll.wrap();
* wrapped.lng; // = -73.9749
*/
wrap() {
return new LngLat(wrap(this.lng, -180, 180), this.lat);
}

/**
* Returns the coordinates represented as an array of two numbers.
*
* @returns {Array<number>} The coordinates represeted as an array of longitude and latitude.
* @example
* var ll = new mapboxgl.LngLat(-73.9749, 40.7736);
* ll.toArray(); // = [-73.9749, 40.7736]
*/
LngLat.prototype.toArray = function () {
return [this.lng, this.lat];
};
/**
* Returns the coordinates represented as an array of two numbers.
*
* @returns {Array<number>} The coordinates represeted as an array of longitude and latitude.
* @example
* var ll = new mapboxgl.LngLat(-73.9749, 40.7736);
* ll.toArray(); // = [-73.9749, 40.7736]
*/
toArray() {
return [this.lng, this.lat];
}

/**
* Returns the coordinates represent as a string.
*
* @returns {string} The coordinates represented as a string of the format `'LngLat(lng, lat)'`.
* @example
* var ll = new mapboxgl.LngLat(-73.9749, 40.7736);
* ll.toString(); // = "LngLat(-73.9749, 40.7736)"
*/
LngLat.prototype.toString = function () {
return `LngLat(${this.lng}, ${this.lat})`;
};
/**
* Returns the coordinates represent as a string.
*
* @returns {string} The coordinates represented as a string of the format `'LngLat(lng, lat)'`.
* @example
* var ll = new mapboxgl.LngLat(-73.9749, 40.7736);
* ll.toString(); // = "LngLat(-73.9749, 40.7736)"
*/
toString() {
return `LngLat(${this.lng}, ${this.lat})`;
}
}

/**
* Converts an array of two numbers to a `LngLat` object.
Expand All @@ -94,3 +93,5 @@ LngLat.convert = function (input) {
throw new Error("`LngLatLike` argument must be specified as a LngLat instance, an object {lng: <lng>, lat: <lat>}, or an array of [<lng>, <lat>]");
}
};

module.exports = LngLat;
68 changes: 33 additions & 35 deletions js/geo/lng_lat_bounds.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

module.exports = LngLatBounds;

const LngLat = require('./lng_lat');

/**
Expand All @@ -14,57 +12,55 @@ const LngLat = require('./lng_lat');
* can also accept an `Array` of two [`LngLatLike`](#LngLatLike) constructs and will perform an implicit conversion.
* This flexible type is documented as [`LngLatBoundsLike`](#LngLatBoundsLike).
*
* @class LngLatBounds
* @param {LngLatLike} [sw] The southwest corner of the bounding box.
* @param {LngLatLike} [ne] The northeast corner of the bounding box.
* @example
* var sw = new mapboxgl.LngLat(-73.9876, 40.7661);
* var ne = new mapboxgl.LngLat(-73.9397, 40.8002);
* var llb = new mapboxgl.LngLatBounds(sw, ne);
*/
function LngLatBounds(sw, ne) {
if (!sw) {
return;
} else if (ne) {
this.setSouthWest(sw).setNorthEast(ne);
} else if (sw.length === 4) {
this.setSouthWest([sw[0], sw[1]]).setNorthEast([sw[2], sw[3]]);
} else {
this.setSouthWest(sw[0]).setNorthEast(sw[1]);
class LngLatBounds {
constructor(sw, ne) {
if (!sw) {
return;
} else if (ne) {
this.setSouthWest(sw).setNorthEast(ne);
} else if (sw.length === 4) {
this.setSouthWest([sw[0], sw[1]]).setNorthEast([sw[2], sw[3]]);
} else {
this.setSouthWest(sw[0]).setNorthEast(sw[1]);
}
}
}

LngLatBounds.prototype = {

/**
* Set the northeast corner of the bounding box
*
* @param {LngLatLike} ne
* @returns {LngLatBounds} `this`
*/
setNorthEast: function(ne) {
setNorthEast(ne) {
this._ne = LngLat.convert(ne);
return this;
},
}

/**
* Set the southwest corner of the bounding box
*
* @param {LngLatLike} sw
* @returns {LngLatBounds} `this`
*/
setSouthWest: function(sw) {
setSouthWest(sw) {
this._sw = LngLat.convert(sw);
return this;
},
}

/**
* Extend the bounds to include a given LngLat or LngLatBounds.
*
* @param {LngLat|LngLatBounds} obj object to extend to
* @returns {LngLatBounds} `this`
*/
extend: function(obj) {
extend(obj) {
const sw = this._sw,
ne = this._ne;
let sw2, ne2;
Expand Down Expand Up @@ -102,7 +98,7 @@ LngLatBounds.prototype = {
}

return this;
},
}

/**
* Returns the geographical coordinate equidistant from the bounding box's corners.
Expand All @@ -112,65 +108,65 @@ LngLatBounds.prototype = {
* var llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
* llb.getCenter(); // = LngLat {lng: -73.96365, lat: 40.78315}
*/
getCenter: function() {
getCenter() {
return new LngLat((this._sw.lng + this._ne.lng) / 2, (this._sw.lat + this._ne.lat) / 2);
},
}

/**
* Returns the southwest corner of the bounding box.
*
* @returns {LngLat} The southwest corner of the bounding box.
*/
getSouthWest: function() { return this._sw; },
getSouthWest() { return this._sw; }

/**
* Returns the northeast corner of the bounding box.
*
* @returns {LngLat} The northeast corner of the bounding box.
*/
getNorthEast: function() { return this._ne; },
getNorthEast() { return this._ne; }

/**
* Returns the northwest corner of the bounding box.
*
* @returns {LngLat} The northwest corner of the bounding box.
*/
getNorthWest: function() { return new LngLat(this.getWest(), this.getNorth()); },
getNorthWest() { return new LngLat(this.getWest(), this.getNorth()); }

/**
* Returns the southeast corner of the bounding box.
*
* @returns {LngLat} The southeast corner of the bounding box.
*/
getSouthEast: function() { return new LngLat(this.getEast(), this.getSouth()); },
getSouthEast() { return new LngLat(this.getEast(), this.getSouth()); }

/**
* Returns the west edge of the bounding box.
*
* @returns {number} The west edge of the bounding box.
*/
getWest: function() { return this._sw.lng; },
getWest() { return this._sw.lng; }

/**
* Returns the south edge of the bounding box.
*
* @returns {number} The south edge of the bounding box.
*/
getSouth: function() { return this._sw.lat; },
getSouth() { return this._sw.lat; }

/**
* Returns the east edge of the bounding box.
*
* @returns {number} The east edge of the bounding box.
*/
getEast: function() { return this._ne.lng; },
getEast() { return this._ne.lng; }

/**
* Returns the north edge of the bounding box.
*
* @returns {number} The north edge of the bounding box.
*/
getNorth: function() { return this._ne.lat; },
getNorth() { return this._ne.lat; }

/**
* Returns the bounding box represented as an array.
Expand All @@ -181,9 +177,9 @@ LngLatBounds.prototype = {
* var llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
* llb.toArray(); // = [[-73.9876, 40.7661], [-73.9397, 40.8002]]
*/
toArray: function () {
toArray () {
return [this._sw.toArray(), this._ne.toArray()];
},
}

/**
* Return the bounding box represented as a string.
Expand All @@ -194,10 +190,10 @@ LngLatBounds.prototype = {
* var llb = new mapboxgl.LngLatBounds([-73.9876, 40.7661], [-73.9397, 40.8002]);
* llb.toString(); // = "LngLatBounds(LngLat(-73.9876, 40.7661), LngLat(-73.9397, 40.8002))"
*/
toString: function () {
toString () {
return `LngLatBounds(${this._sw.toString()}, ${this._ne.toString()})`;
}
};
}

/**
* Converts an array to a `LngLatBounds` object.
Expand All @@ -217,3 +213,5 @@ LngLatBounds.convert = function (input) {
if (!input || input instanceof LngLatBounds) return input;
return new LngLatBounds(input);
};

module.exports = LngLatBounds;
Loading

0 comments on commit 440889f

Please sign in to comment.