Skip to content

Commit

Permalink
typings for places
Browse files Browse the repository at this point in the history
  • Loading branch information
rickyplouis committed Feb 6, 2024
1 parent 4eb3fed commit 15027dd
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 99 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ distFrom(chicago).to(ny).in('mi')
// returns distance using haversine formula with margin of error +/- 0.03%

// Additionally we also have a hardcoded list of places from the US you can use
const places = require('../dist/Places')
const chitown = distFrom().places().usa.il.chicago
const nyc = distFrom().places().usa.ny.newYorkCity

distFrom(places().usa().il.chicago).to(places().usa().ny.newYorkCity).in('mi')
distFrom(chitown).to(nyc).in('mi')

// To see a list of all supported states you can use
places().listOfSupportedStates()
distFrom().listOfSupportedStates()

// Or all the cities you can use
places().listOfSupportedCities()
distFrom().listOfSupportedCities()

// If you don't see a state/city in the list then feel free to open a PR
```
Expand Down
11 changes: 5 additions & 6 deletions __tests__/distance.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const distFrom = require('../dist/index')
const places = require('../dist/places')

const ny = [40.79500901101372, -74.12456877495657]
const chitown = places().usa().il.chicago
const listOfCities = places().listOfSupportedCities()
const listOfStates = places().listOfSupportedStates()
const chitown = distFrom().places().usa.il.chicago
const london = [51.53269844455333, -0.07741875190006414]

describe('distFrom().from().to()', () => {
Expand Down Expand Up @@ -114,10 +111,12 @@ describe('places.usa...', () => {
expect(() => chitown).toBeTruthy()
})
test('get list of supported states and expect them to match the hardcoded list', () => {
expect(listOfStates).toEqual(['il', 'ny', 'ca', 'mo', 'mi'].sort())
expect(distFrom().listOfSupportedStates()).toEqual(
['il', 'ny', 'ca', 'mo', 'mi'].sort(),
)
})
test('get list of supported cities and expect them to exist', () => {
expect(listOfCities).toBeTruthy()
expect(distFrom().listOfSupportedCities).toBeTruthy()
})
})

Expand Down
86 changes: 0 additions & 86 deletions lib/Places.ts

This file was deleted.

76 changes: 75 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,35 @@ namespace distanceFrom {
| 'yard'
| 'yards'
export type DistanceFrom = Distance
export type Lat = number
export type Lng = number
export type City = [Lat, Lng]
export type ListOfSupportedStates = string[]
export type ListOfSupportedCities = string[]
export type Places = {
usa: {
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
}
}
}
}

const unitList: distanceFrom.Units[] = [
Expand Down Expand Up @@ -131,7 +160,7 @@ class DistanceFrom {
}

if (!this.validUnits(units)) {
throw new Error('Need to use valid units, run distFrom.unitList() to see list')
throw new Error('Need to use valid units, run distFrom().unitList() to see list')
}

const { distance } = this
Expand All @@ -158,6 +187,51 @@ class DistanceFrom {
unitList() {
return unitList.slice()
}

places(): distanceFrom.Places {
return {
usa: {
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],
},
},
}
}

listOfSupportedStates(): distanceFrom.ListOfSupportedStates {
return Object.keys(this.places().usa).sort()
}

listOfSupportedCities(): distanceFrom.ListOfSupportedCities {
const states = this.listOfSupportedStates()
let cities: string[] = []
const { usa } = this.places()

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
}
}

function distanceFrom(val: distanceFrom.Position) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "distance-from",
"version": "2.0.4",
"version": "2.0.5",
"description": "Calculate distance between two coordinates",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
},
"compileOnSave": true,
"buildOnSave": true,
"files": ["./lib/index.ts", "lib/places.ts"],
"files": ["./lib/index.ts"],
"exclude": ["**/*.spec.js", "dist", "node_modules", "__tests__"]
}

0 comments on commit 15027dd

Please sign in to comment.