Added support for inline parameters like: new GeoPoint(-34, 150)
This commit is contained in:
parent
b990afc910
commit
e1a60f146e
13
lib/geo.js
13
lib/geo.js
|
@ -112,6 +112,10 @@ exports.GeoPoint = GeoPoint;
|
|||
* @options {Object} Options Object with two Number properties: lat and long.
|
||||
* @property {Number} lat The latitude point in degrees. Range: -90 to 90.
|
||||
* @property {Number} lng The longitude point in degrees. Range: -180 to 180.
|
||||
*
|
||||
* @options {Array} Options Array with two Number entries: [lat,long].
|
||||
* @property {Number} lat The latitude point in degrees. Range: -90 to 90.
|
||||
* @property {Number} lng The longitude point in degrees. Range: -180 to 180.
|
||||
*/
|
||||
|
||||
function GeoPoint(data) {
|
||||
|
@ -119,7 +123,14 @@ function GeoPoint(data) {
|
|||
return new GeoPoint(data);
|
||||
}
|
||||
|
||||
assert(typeof data === 'object' || typeof data === 'string', 'must provide valid geo-coordinates array [lat, lng] or object or a "lat, lng" string');
|
||||
if(arguments.length === 2) {
|
||||
data = {
|
||||
lat: arguments[0],
|
||||
lng: arguments[1]
|
||||
};
|
||||
}
|
||||
|
||||
assert(Array.isArray(data) || typeof data === 'object' || typeof data === 'string', 'must provide valid geo-coordinates array [lat, lng] or object or a "lat, lng" string');
|
||||
|
||||
if (typeof data === 'string') {
|
||||
data = data.split(/,\s*/);
|
||||
|
|
|
@ -30,6 +30,13 @@ describe('GeoPoint', function () {
|
|||
point.lng.should.equal(150);
|
||||
});
|
||||
|
||||
it('should support coordinates as inline parameters', function () {
|
||||
var point = new GeoPoint(-34, 150);
|
||||
|
||||
point.lat.should.equal(-34);
|
||||
point.lng.should.equal(150);
|
||||
});
|
||||
|
||||
it('should reject invalid parameters', function () {
|
||||
/*jshint -W024 */
|
||||
var fn = function() {
|
||||
|
@ -38,7 +45,25 @@ describe('GeoPoint', function () {
|
|||
fn.should.throw();
|
||||
|
||||
fn = function() {
|
||||
new GeoPoint('150,-34');
|
||||
new GeoPoint('invalid_string');
|
||||
};
|
||||
fn.should.throw();
|
||||
|
||||
fn = function() {
|
||||
new GeoPoint([150, -34]);
|
||||
};
|
||||
fn.should.throw();
|
||||
|
||||
fn = function() {
|
||||
new GeoPoint({
|
||||
lat: 150,
|
||||
lng: null
|
||||
});
|
||||
};
|
||||
fn.should.throw();
|
||||
|
||||
fn = function() {
|
||||
new GeoPoint(150, -34);
|
||||
};
|
||||
fn.should.throw();
|
||||
|
||||
|
|
Loading…
Reference in New Issue