Skip to content

Commit

Permalink
Merge pull request #9 from rickyplouis/new-places
Browse files Browse the repository at this point in the history
New places
  • Loading branch information
rickyplouis authored Feb 6, 2024
2 parents 6209ae5 + 1934087 commit 5e8ad8e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 25 deletions.
41 changes: 29 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,50 @@
Simple API for quickly calculating distance between two coordinates

### Installation

```js
$ npm i distance-from
```

### Example

```js
// Use format of [lat, lng]
const chicago = [41.977226, -87.836723];
const ny = [40.730610, -73.935242];
const chicago = [42.01682819245601, -87.3011661732315]
const ny = [40.79500901101372, -74.12456877495657]

const distFrom = require('distance-from');
const distFrom = require('distance-from')

// defaults to kilometers if no units put in
distFrom(chicago).to(ny).in('mi');
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')

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()

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

// If you don't see a state/city in the list then feel free to open a PR
```

#### Supported units
* km || kilometer || kilometers
* m || meter || meters
* cm || centimeter || centimeters
* mi || mile || miles
* ft || feet
* in || inch || inches
* yd || yard || yards

- km || kilometer || kilometers
- m || meter || meters
- cm || centimeter || centimeters
- mi || mile || miles
- ft || feet
- in || inch || inches
- yd || yard || yards

### License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
Expand Down
40 changes: 29 additions & 11 deletions __tests__/distance.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const distFrom = require('../dist/index')
const Places = require('../dist/Places')

const ny = [40.71278, -74.00594]
const london = [51.50853, -0.12574]
const ny = [40.79500901101372, -74.12456877495657]
const chitown = Places.usa.il.chicago
const london = [51.53269844455333, -0.07741875190006414]

describe('distFrom().from().to()', () => {
test('throws for no origin', () => {
Expand Down Expand Up @@ -39,37 +41,41 @@ describe('distFrom().in()', () => {

describe('distFrom().from().to().in()', () => {
test('gets distance from ny to london in kilometers', () => {
expect(parseInt(distFrom(ny).to(london).in('km'), 10)).toBe(5570)
expect(parseInt(distFrom(ny).to(london).in('km'), 10)).toBe(5574)
})

test('gets distance from ny to london in meters', () => {
expect(parseInt(distFrom(ny).to(london).in('m'), 10)).toBe(5570315)
expect(parseInt(distFrom(ny).to(london).in('m'), 10)).toBe(5574697)
})

test('gets distance from ny to london in centimeters', () => {
expect(parseInt(distFrom(ny).to(london).in('cm'), 10)).toBe(557031545)
expect(parseInt(distFrom(ny).to(london).in('cm'), 10)).toBe(557469709)
})

test('gets distance from ny to london in miles', () => {
expect(parseInt(distFrom(ny).to(london).in('miles'), 10)).toBe(3461)
expect(parseInt(distFrom(ny).to(london).in('miles'), 10)).toBe(3463)
})

test('gets distance from ny to london in yards', () => {
expect(parseInt(distFrom(ny).to(london).in('yards'), 10)).toBe(6091752)
expect(parseInt(distFrom(ny).to(london).in('yards'), 10)).toBe(6096544)
})

test('gets distance from ny to london in kilometers', () => {
expect(parseInt(distFrom(ny).to(london).in('inches'), 10)).toBe(219303876)
expect(parseInt(distFrom(ny).to(london).in('inches'), 10)).toBe(219476381)
})

test('gets distance from ny to london in feet', () => {
expect(parseInt(distFrom(ny).to(london).in('ft'), 10)).toBe(18275313)
expect(parseInt(distFrom(ny).to(london).in('ft'), 10)).toBe(18289689)
})

test('gets distance from ny to london in two twice', () => {
const dist = distFrom(ny).to(london)
expect(parseInt(dist.in('m'), 10)).toBe(5570315)
expect(parseInt(dist.in('m'), 10)).toBe(5570315)
expect(parseInt(dist.in('m'), 10)).toBe(5574697)
expect(parseInt(dist.in('m'), 10)).toBe(5574697)
})

test('gets distance from chicago to ny in km using hardcoded places', () => {
expect(parseInt(distFrom(chitown).to(ny).in('km'), 10)).toBe(1106)
})

test('test invalid units', () => {
Expand Down Expand Up @@ -101,6 +107,18 @@ describe('distFrom().validUnits()', () => {
})
})

describe('places.usa...', () => {
test('expect chicago to exist', () => {
expect(() => chitown).toBeTruthy()
})
test('get list of supported states and expect them to match the hardcoded list', () => {
expect(Places.listOfSupportedStates()).toEqual(['il', 'ny', 'ca', 'mo', 'mi'].sort())
})
test('get list of supported cities and expect them to exist', () => {
expect(Places.listOfSupportedCities()).toBeTruthy()
})
})

describe('distFrom().degreeToRadians', () => {
test('degree to radians with no input', () => {
expect(() => {
Expand Down
45 changes: 45 additions & 0 deletions lib/Places.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// List of popular hardcoded places with their coordinates
// Coordinates come from google maps which are measured using the Mercator projector's origin
// More info here: https://developers.google.com/maps/documentation/javascript/coordinates#:~:text=World%20coordinates%20in%20Google%20Maps,towards%20the%20south%20(down).

// If a place is missing that you'd like to add feel free to open a PR

export const 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],
},
}

export function listOfSupportedStates() {
return Object.keys(usa).sort()
}

export function listOfSupportedCities() {
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
}
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.1",
"version": "2.0.2",
"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"],
"files": ["./lib/index.ts", "./lib/Places.ts"],
"exclude": ["**/*.spec.js", "dist", "node_modules", "__tests__"]
}

0 comments on commit 5e8ad8e

Please sign in to comment.