From b3614c77267d59a4e0d4285d2e83d34e86734fbe Mon Sep 17 00:00:00 2001 From: rickyplouis Date: Tue, 6 Feb 2024 14:08:11 -0600 Subject: [PATCH 1/2] version update --- README.md | 28 ++++++++++++++---------- __tests__/distance.spec.js | 40 +++++++++++++++++++++++---------- lib/Places.ts | 45 ++++++++++++++++++++++++++++++++++++++ package.json | 2 +- tsconfig.json | 2 +- 5 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 lib/Places.ts diff --git a/README.md b/README.md index 4fee136..66d0b89 100644 --- a/README.md +++ b/README.md @@ -5,33 +5,37 @@ 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% ``` - #### 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. diff --git a/__tests__/distance.spec.js b/__tests__/distance.spec.js index 17efdea..6d915b1 100644 --- a/__tests__/distance.spec.js +++ b/__tests__/distance.spec.js @@ -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', () => { @@ -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', () => { @@ -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(() => { diff --git a/lib/Places.ts b/lib/Places.ts new file mode 100644 index 0000000..542eeb2 --- /dev/null +++ b/lib/Places.ts @@ -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 +} diff --git a/package.json b/package.json index a8d48d6..6248afa 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tsconfig.json b/tsconfig.json index 8569e74..e4638bc 100755 --- a/tsconfig.json +++ b/tsconfig.json @@ -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__"] } From 19340876f3a272ad30683c982097f6a957a24174 Mon Sep 17 00:00:00 2001 From: rickyplouis Date: Tue, 6 Feb 2024 14:11:38 -0600 Subject: [PATCH 2/2] readme updates --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 66d0b89..d5b2132 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,19 @@ const distFrom = require('distance-from') // defaults to kilometers if no units put in 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