Added support for inline parameters like: new GeoPoint(-34, 150)

This commit is contained in:
Simo Moujami 2014-12-18 10:47:06 -05:00
parent b990afc910
commit e1a60f146e
2 changed files with 38 additions and 2 deletions

View File

@ -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*/);

View File

@ -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();