Fixed constructor parameters and added bdd tests for constructor validation
This commit is contained in:
parent
87027b49f8
commit
beaa97f6f4
26
lib/geo.js
26
lib/geo.js
|
@ -111,7 +111,7 @@ 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: -90 to 90.
|
||||
* @property {Number} lng The longitude point in degrees. Range: -180 to 180.
|
||||
*/
|
||||
|
||||
function GeoPoint(data) {
|
||||
|
@ -119,14 +119,17 @@ 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 (typeof data === 'string') {
|
||||
data = data.split(/,\s*/);
|
||||
assert(data.length === 2, 'must provide a string "lng,lat" creating a GeoPoint with a string');
|
||||
assert(data.length === 2, 'must provide a string "lat,lng" creating a GeoPoint with a string');
|
||||
}
|
||||
if (Array.isArray(data)) {
|
||||
data = {
|
||||
lng: Number(data[0]),
|
||||
lat: Number(data[1])
|
||||
lat: Number(data[0]),
|
||||
lng: Number(data[1])
|
||||
};
|
||||
} else {
|
||||
data.lng = Number(data.lng);
|
||||
|
@ -177,7 +180,7 @@ GeoPoint.distanceBetween = function distanceBetween(a, b, options) {
|
|||
var y2 = b.lng;
|
||||
|
||||
return geoDistance(x1, y1, x2, y2, options);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Determine the spherical distance to the given point.
|
||||
|
@ -202,15 +205,15 @@ GeoPoint.distanceBetween = function distanceBetween(a, b, options) {
|
|||
|
||||
GeoPoint.prototype.distanceTo = function (point, options) {
|
||||
return GeoPoint.distanceBetween(this, point, options);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Simple serialization.
|
||||
*/
|
||||
|
||||
GeoPoint.prototype.toString = function () {
|
||||
return this.lng + ',' + this.lat;
|
||||
}
|
||||
return this.lat + ',' + this.lng;
|
||||
};
|
||||
|
||||
/**
|
||||
* @property {Number} PI - Ratio of a circle's circumference to its diameter.
|
||||
|
@ -219,9 +222,6 @@ GeoPoint.prototype.toString = function () {
|
|||
* @property {Object} EARTH_RADIUS - Radius of the earth.
|
||||
*/
|
||||
|
||||
// ratio of a circle's circumference to its diameter
|
||||
var PI = 3.1415926535897932384626433832795;
|
||||
|
||||
// factor to convert degrees to radians
|
||||
var DEG2RAD = 0.01745329252;
|
||||
|
||||
|
@ -251,8 +251,8 @@ function geoDistance(x1, y1, x2, y2, options) {
|
|||
// use the haversine formula to calculate distance for any 2 points on a sphere.
|
||||
// ref http://en.wikipedia.org/wiki/Haversine_formula
|
||||
var haversine = function(a) {
|
||||
return Math.pow(Math.sin(a / 2.0), 2)
|
||||
}
|
||||
return Math.pow(Math.sin(a / 2.0), 2);
|
||||
};
|
||||
|
||||
var f = Math.sqrt(haversine(x2 - x1) + Math.cos(x2) * Math.cos(x1) * haversine(y2 - y1));
|
||||
|
||||
|
|
|
@ -1,11 +1,55 @@
|
|||
/*global describe,it*/
|
||||
/*jshint expr:true*/
|
||||
/*jshint expr:true */
|
||||
|
||||
require('should');
|
||||
|
||||
var GeoPoint = require('../lib/geo').GeoPoint;
|
||||
|
||||
describe('GeoPoint', function () {
|
||||
|
||||
describe('constructor', function() {
|
||||
|
||||
it('should support a valid array', function () {
|
||||
var point = new GeoPoint([-34, 150]);
|
||||
|
||||
point.lat.should.equal(-34);
|
||||
point.lng.should.equal(150);
|
||||
});
|
||||
|
||||
it('should support a valid object', function () {
|
||||
var point = new GeoPoint({ lat: -34, lng: 150 });
|
||||
|
||||
point.lat.should.equal(-34);
|
||||
point.lng.should.equal(150);
|
||||
});
|
||||
|
||||
it('should support valid string geo coordinates', 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() {
|
||||
new GeoPoint('150,-34');
|
||||
};
|
||||
fn.should.throw();
|
||||
|
||||
fn = function() {
|
||||
new GeoPoint('150,-34');
|
||||
};
|
||||
fn.should.throw();
|
||||
|
||||
fn = function() {
|
||||
new GeoPoint();
|
||||
};
|
||||
fn.should.throw();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('distance calculation between two points', function () {
|
||||
|
||||
var here = new GeoPoint({ lat: 40.77492964101182, lng: -73.90950187151662 });
|
||||
|
|
Loading…
Reference in New Issue