Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

haversine

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

haversine - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

2

bower.json
{
"name": "haversine",
"description": "A simple haversine module",
"version": "1.0.2",
"version": "1.1.0",
"keywords": [

@@ -6,0 +6,0 @@ "haversine",

var haversine = (function () {
var RADII = {
km: 6371,
mile: 3960,
meter: 6371000,
nmi: 3440
}

@@ -8,15 +14,27 @@ // convert to radians

return function haversine (start, end, options) {
// convert coordinates to standard format based on the passed format option
var convertCoordinates = function (format, coordinates) {
switch (format) {
case '[lat,lon]':
return { latitude: coordinates[0], longitude: coordinates[1] }
case '[lon,lat]':
return { latitude: coordinates[1], longitude: coordinates[0] }
case '{lon,lat}':
return { latitude: coordinates.lat, longitude: coordinates.lon }
case 'geojson':
return { latitude: coordinates.geometry.coordinates[1], longitude: coordinates.geometry.coordinates[0] }
default:
return coordinates
}
}
return function haversine (startCoordinates, endCoordinates, options) {
options = options || {}
var radii = {
km: 6371,
mile: 3960,
meter: 6371000,
nmi: 3440
}
var R = options.unit in RADII
? RADII[options.unit]
: RADII.km
var R = options.unit in radii
? radii[options.unit]
: radii.km
var start = convertCoordinates(options.format, startCoordinates)
var end = convertCoordinates(options.format, endCoordinates)

@@ -23,0 +41,0 @@ var dLat = toRad(end.latitude - start.latitude)

{
"name": "haversine",
"version": "1.0.2",
"version": "1.1.0",
"description": "A simple haversine module",

@@ -5,0 +5,0 @@ "main": "haversine.js",

@@ -10,9 +10,10 @@ # Haversine

var haversine = require('haversine')
const haversine = require('haversine')
start = {
const start = {
latitude: 30.849635,
longitude: -83.24559
}
end = {
const end = {
latitude: 27.950575,

@@ -31,6 +32,15 @@ longitude: -82.457178

#### api
- `options.unit` - Unit of measurement applied to result (default `km`)
- `options.unit` - Unit of measurement applied to result (default `km`, available `km, mile, meter, nmi`)
- `options.threshold` - If passed, will result in library returning `boolean` value of whether or not the start and end points are within that supplied threshold. (default `null`)
- `options.format` - The format of start and end coordinate arguments. See table below for available values. (default `null`)
| Format | Example
| ------------- |--------------------------|
| `undefined` (default) | `{ latitude: 30.849635, longitude: -83.24559] }`
| `[lat,lon]` | `[30.849635, -83.24559]`
| `[lon,lat]` | `[-83.24559, 30.849635]`
| `{lat,lon}` | `{ lat: 30.849635, lon: -83.24559] }`
| `geojson` | `{ type: 'Feature', geometry: { coordinates: [-83.24559, 30.849635] } }`
[MIT License](http://opensource.org/licenses/MIT)

@@ -10,2 +10,13 @@ var haversine = require('../haversine')

}
var startLatLon = [38.898556, -77.037852]
var startLonLat = [-77.037852, 38.898556]
var startLatLonObject = {
lat: 38.898556,
lon: -77.037852
}
var startGeoJson = {
geometry: {
coordinates: [-77.037852, 38.898556]
}
}

@@ -16,2 +27,13 @@ var end = {

}
var endLatLon = [38.897147, -77.043934]
var endLonLat = [-77.043934, 38.897147]
var endLatLonObject = {
lat: 38.897147,
lon: -77.043934
}
var endGeoJson = {
geometry: {
coordinates: [-77.043934, 38.897147]
}
}

@@ -22,13 +44,21 @@ // All tests are rounded for sanity.

[start, end, 0.341],
[start, end, 0.549]
[start, end, 0.549],
[startLatLon, endLatLon, 0.341, { format: '[lat,lon]' }],
[startLatLon, endLatLon, 0.549, { format: '[lat,lon]' }],
[startLonLat, endLonLat, 0.341, { format: '[lon,lat]' }],
[startLonLat, endLonLat, 0.549, { format: '[lon,lat]' }],
[startLatLonObject, endLatLonObject, 0.341, { format: '{lon,lat}' }],
[startLatLonObject, endLatLonObject, 0.549, { format: '{lon,lat}' }],
[startGeoJson, endGeoJson, 0.341, { format: 'geojson' }],
[startGeoJson, endGeoJson, 0.549, { format: 'geojson' }],
]
tests.forEach(function(t, i) {
if (i === 0) {
if (i % 2 === 0) {
test('it should return ' + t[2] + ' mi for ' + t[0] + ' .. ' + t[1], function(){
assert.equal(Math.abs((haversine(t[0],t[1], {unit: 'mile'})-t[2])/t[2]).toFixed(2), "0.00")
assert.equal(Math.abs((haversine(t[0], t[1], Object.assign({unit: 'mile'}, t[3]))-t[2])/t[2]).toFixed(2), "0.00")
})
} else {
test('it should return ' + t[2] + ' km for ' + t[0] + ' .. ' + t[1], function(){
assert.equal(Math.abs((haversine(t[0],t[1])-t[2])/t[2]).toFixed(2), "0.00")
assert.equal(Math.abs((haversine(t[0], t[1], Object.assign({}, t[3]))-t[2])/t[2]).toFixed(2), "0.00")
})

@@ -35,0 +65,0 @@ }

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc