distance-from
Advanced tools
Comparing version
const distFrom = require('../dist/index') | ||
const Places = require('../dist/Places') | ||
const places = require('../dist/places') | ||
const ny = [40.79500901101372, -74.12456877495657] | ||
const chitown = Places.usa.il.chicago | ||
const chitown = places().usa().il.chicago | ||
const listOfCities = places().listOfSupportedCities() | ||
const listOfStates = places().listOfSupportedStates() | ||
const london = [51.53269844455333, -0.07741875190006414] | ||
@@ -115,6 +117,6 @@ | ||
test('get list of supported states and expect them to match the hardcoded list', () => { | ||
expect(Places.listOfSupportedStates()).toEqual(['il', 'ny', 'ca', 'mo', 'mi'].sort()) | ||
expect(listOfStates).toEqual(['il', 'ny', 'ca', 'mo', 'mi'].sort()) | ||
}) | ||
test('get list of supported cities and expect them to exist', () => { | ||
expect(Places.listOfSupportedCities()).toBeTruthy() | ||
expect(listOfCities).toBeTruthy() | ||
}) | ||
@@ -121,0 +123,0 @@ }) |
@@ -6,67 +6,82 @@ // List of popular hardcoded places with their coordinates | ||
// If a place is missing that you'd like to add feel free to open a PR | ||
export type Lat = number | ||
export type Lng = number | ||
export type City = [Lat, Lng] | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace places { | ||
export type Lat = number | ||
export type Lng = number | ||
export type City = [Lat, Lng] | ||
export type UnitedStates = { | ||
il: { | ||
naperville: City | ||
chicago: City | ||
streamwood: City | ||
} | ||
ny: { | ||
newYorkCity: City | ||
} | ||
mo: { | ||
kansasCity: City | ||
} | ||
mi: { | ||
detroit: City | ||
troy: City | ||
} | ||
ca: { | ||
losAngeles: City | ||
sanJose: City | ||
sanFrancisco: City | ||
} | ||
} | ||
export type UnitedStates = { | ||
il: { | ||
naperville: City | ||
chicago: City | ||
streamwood: City | ||
export type Places = ListOfPlaces | ||
} | ||
class ListOfPlaces { | ||
usa(): places.UnitedStates { | ||
return { | ||
il: { | ||
naperville: [41.81023757023769, -88.2282830073452], | ||
chicago: [42.01682819245601, -87.3011661732315], | ||
streamwood: [42.026002193961084, -88.17517375314642], | ||
}, | ||
ny: { | ||
newYorkCity: [40.79500901101372, -74.12456877495657], | ||
}, | ||
mo: { | ||
kansasCity: [39.16961900570103, -94.64656902327792], | ||
}, | ||
mi: { | ||
detroit: [42.39148243702238, -83.05589747705353], | ||
troy: [42.57794777862195, -83.16465929309503], | ||
}, | ||
ca: { | ||
losAngeles: [34.25460303876844, -118.8961867571036], | ||
sanJose: [37.38919954624826, -121.88193375335521], | ||
sanFrancisco: [37.81499641384617, -122.59176494681763], | ||
}, | ||
} | ||
} | ||
ny: { | ||
newYorkCity: City | ||
listOfSupportedStates(): string[] { | ||
return Object.keys(this.usa()).sort() | ||
} | ||
mo: { | ||
kansasCity: City | ||
} | ||
mi: { | ||
detroit: City | ||
troy: City | ||
} | ||
ca: { | ||
losAngeles: City | ||
sanJose: City | ||
sanFrancisco: City | ||
} | ||
} | ||
export const usa: UnitedStates = { | ||
il: { | ||
naperville: [41.81023757023769, -88.2282830073452], | ||
chicago: [42.01682819245601, -87.3011661732315], | ||
streamwood: [42.026002193961084, -88.17517375314642], | ||
}, | ||
ny: { | ||
newYorkCity: [40.79500901101372, -74.12456877495657], | ||
}, | ||
mo: { | ||
kansasCity: [39.16961900570103, -94.64656902327792], | ||
}, | ||
mi: { | ||
detroit: [42.39148243702238, -83.05589747705353], | ||
troy: [42.57794777862195, -83.16465929309503], | ||
}, | ||
ca: { | ||
losAngeles: [34.25460303876844, -118.8961867571036], | ||
sanJose: [37.38919954624826, -121.88193375335521], | ||
sanFrancisco: [37.81499641384617, -122.59176494681763], | ||
}, | ||
} | ||
listOfSupportedCities(): string[] { | ||
const states = this.listOfSupportedStates() | ||
let cities: string[] = [] | ||
const usa = this.usa() | ||
export function listOfSupportedStates(): string[] { | ||
return Object.keys(usa).sort() | ||
} | ||
for (const state of states) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
const citiesInTheState = Object.keys(usa[state]) | ||
cities = cities.concat(citiesInTheState) | ||
} | ||
export function listOfSupportedCities(): string[] { | ||
const states = listOfSupportedStates() | ||
let cities: string[] = [] | ||
for (const state of states) { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
const citiesInTheState = Object.keys(usa[state]) | ||
cities = cities.concat(citiesInTheState) | ||
return cities | ||
} | ||
} | ||
return cities | ||
function places() { | ||
return new ListOfPlaces() | ||
} | ||
export = places |
{ | ||
"name": "distance-from", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "Calculate distance between two coordinates", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -27,11 +27,11 @@ ## distance-from [](https://travis-ci.com/rickyplouis/distance-from) [](https://coveralls.io/github/rickyplouis/distance-from?branch=master) [](https://badge.fury.io/js/distance-from) | ||
// Additionally we also have a hardcoded list of places from the US you can use | ||
const Places = require('../dist/Places') | ||
const places = require('../dist/Places') | ||
distFrom(Places.usa.il.chicago).to(Places.usa.ny.newYorkCity).in('mi') | ||
distFrom(places().usa().il.chicago).to(places().usa().ny.newYorkCity).in('mi') | ||
// To see a list of all supported states you can use | ||
Places.listOfSupportedStates() | ||
places().listOfSupportedStates() | ||
// Or all the cities you can use | ||
Places.listOfSupportedCities() | ||
places().listOfSupportedCities() | ||
@@ -38,0 +38,0 @@ // If you don't see a state/city in the list then feel free to open a PR |
26615
3.45%543
5.23%