You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
846 B
22 lines
846 B
class DistanceResolver {
|
|
getDistance (A, B) {
|
|
A = this._sanitizeCoords(A)
|
|
B = this._sanitizeCoords(B)
|
|
}
|
|
|
|
_sanitizeCoords (coords) {
|
|
if (!Array.isArray(coords)) throw new Error('Coordinates are not an array!')
|
|
if (coords.length !== 2) throw new Error('Coordinates are not in form of [lon, lat] array!')
|
|
let lon = coords[0]
|
|
let lat = coords[1]
|
|
if (typeof lon !== 'number') lon = Number(lon)
|
|
if (typeof lat !== 'number') lat = Number(lat)
|
|
if (Number.isNaN(lon)) throw new Error('Invalid longitude!')
|
|
if (Number.isNaN(lat)) throw new Error('Invalid latitude!')
|
|
if (Math.abs(lat) > 90) throw new Error('Latitude must be within [-90;90] degrees')
|
|
if (Math.abs(lon) > 180) throw new Error('Longitude must be within [-180;180] degrees')
|
|
return [lon, lat]
|
|
}
|
|
}
|
|
|
|
module.exports = DistanceResolver
|
|
|