From 7627c93a2500ccfdbd75172478313cac33b2faad Mon Sep 17 00:00:00 2001 From: Daniel Hritzkiv Date: Tue, 14 Nov 2017 20:56:58 -0500 Subject: [PATCH 1/5] haversine: add format support haversine now supports supplying coordinates in a variety of formats --- types/haversine/index.d.ts | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/types/haversine/index.d.ts b/types/haversine/index.d.ts index 6fed860d8b..97d6292c59 100644 --- a/types/haversine/index.d.ts +++ b/types/haversine/index.d.ts @@ -4,10 +4,28 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare namespace haversine { - interface Coordinate { + interface CoordinateLongNames { longitude: number; latitude: number; } + + interface CoordinateShortNames { + lat: number; + lon: number; + } + + interface LatLonTuple extends Array { + 0: number; + 1: number; + } + + interface GeoJSON { + geometry: { + coordinates: LatLonTuple + } + } + + type coordinate = (CoordinateLongNames | CoordinateShortNames | LatLonTuple | GeoJSON); interface Options { /** @@ -18,6 +36,10 @@ declare namespace haversine { * 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. */ threshold?: number; + /** + * Format of coordinate arguments. + */ + format?: '[lat,lon]' | '[lon,lat]' | '{lon,lat}' | 'geojson' } } @@ -25,8 +47,8 @@ declare namespace haversine { * Determines the great-circle distance between two points on a sphere given their longitudes and latitudes */ declare function haversine( - start: haversine.Coordinate, - end: haversine.Coordinate, + start: coordinate, + end: coordinate, options?: haversine.Options ): number; From 545db03e0c534a6531d1635cec450907d21aa6e3 Mon Sep 17 00:00:00 2001 From: Daniel Hritzkiv Date: Tue, 14 Nov 2017 21:02:53 -0500 Subject: [PATCH 2/5] haversine: update module version --- types/haversine/index.d.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/types/haversine/index.d.ts b/types/haversine/index.d.ts index 97d6292c59..e5ddb1273c 100644 --- a/types/haversine/index.d.ts +++ b/types/haversine/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for haversine 1.0 +// Type definitions for haversine 1.1 // Project: https://github.com/njj/haversine // Definitions by: Christian Rackerseder // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped @@ -8,12 +8,12 @@ declare namespace haversine { longitude: number; latitude: number; } - + interface CoordinateShortNames { lat: number; lon: number; } - + interface LatLonTuple extends Array { 0: number; 1: number; @@ -22,10 +22,10 @@ declare namespace haversine { interface GeoJSON { geometry: { coordinates: LatLonTuple - } + }; } - type coordinate = (CoordinateLongNames | CoordinateShortNames | LatLonTuple | GeoJSON); + type Coordinate = (CoordinateLongNames | CoordinateShortNames | LatLonTuple | GeoJSON); interface Options { /** @@ -39,7 +39,7 @@ declare namespace haversine { /** * Format of coordinate arguments. */ - format?: '[lat,lon]' | '[lon,lat]' | '{lon,lat}' | 'geojson' + format?: '[lat,lon]' | '[lon,lat]' | '{lon,lat}' | 'geojson'; } } @@ -47,8 +47,8 @@ declare namespace haversine { * Determines the great-circle distance between two points on a sphere given their longitudes and latitudes */ declare function haversine( - start: coordinate, - end: coordinate, + start: haversine.Coordinate, + end: haversine.Coordinate, options?: haversine.Options ): number; From 5564fca4c45b0bf81bbe1c6d386b8c1f63d96e90 Mon Sep 17 00:00:00 2001 From: Daniel Hritzkiv Date: Tue, 14 Nov 2017 21:12:21 -0500 Subject: [PATCH 3/5] Update haversine-tests.ts --- types/haversine/haversine-tests.ts | 34 ++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/types/haversine/haversine-tests.ts b/types/haversine/haversine-tests.ts index 043d7d5898..b7238fbd15 100644 --- a/types/haversine/haversine-tests.ts +++ b/types/haversine/haversine-tests.ts @@ -1,11 +1,11 @@ import haversine = require('haversine'); -const start: haversine.Coordinate = { +const start: haversine.CoordinateLongNames = { longitude: 48.1548256, latitude: 11.4017529 }; -const end: haversine.Coordinate = { +const end: haversine.CoordinateLongNames = { longitude: 52.5065133, latitude: 13.1445551 }; @@ -16,3 +16,33 @@ const options: haversine.Options = { }; haversine(start, end, options); + +const startShort: haversine.CoordinateShortNames = { + lon: 48.1548256, + lat: 11.4017529 +}; + +const endShort: haversine.CoordinateShortNames = { + lon: 52.5065133, + lat: 13.1445551 +}; + +const optionsShort: haversine.Options = { + unit: 'km', + threshold: 1, + format: '{lon,lat}' +}; + +haversine(startShort, endShort, optionsShort); + +const startLatLon: haversine.LatLonTuple = [11.4017529, 48.1548256]; + +const endLatLon: haversine.LatLonTuple = [13.1445551, 52.5065133]; + +const optionsLatLon: haversine.Options = { + unit: 'km', + threshold: 1, + format: '[lat,lon]' +}; + +haversine(startLatLon, endLatLon, optionsLatLon); From fec98c71f8986469ad16fe8339b98f2f383e8196 Mon Sep 17 00:00:00 2001 From: Daniel Hritzkiv Date: Wed, 15 Nov 2017 11:26:32 -0500 Subject: [PATCH 4/5] haversine: cleanup union types, simplify LatLonTuple, add GeoJSON test --- types/haversine/haversine-tests.ts | 34 +++++++++++++++++++++++------- types/haversine/index.d.ts | 23 +++++++------------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/types/haversine/haversine-tests.ts b/types/haversine/haversine-tests.ts index b7238fbd15..2bfac64856 100644 --- a/types/haversine/haversine-tests.ts +++ b/types/haversine/haversine-tests.ts @@ -1,11 +1,11 @@ import haversine = require('haversine'); -const start: haversine.CoordinateLongNames = { +const start = { longitude: 48.1548256, latitude: 11.4017529 }; -const end: haversine.CoordinateLongNames = { +const end = { longitude: 52.5065133, latitude: 13.1445551 }; @@ -17,19 +17,17 @@ const options: haversine.Options = { haversine(start, end, options); -const startShort: haversine.CoordinateShortNames = { +const startShort = { lon: 48.1548256, lat: 11.4017529 }; -const endShort: haversine.CoordinateShortNames = { +const endShort = { lon: 52.5065133, lat: 13.1445551 }; const optionsShort: haversine.Options = { - unit: 'km', - threshold: 1, format: '{lon,lat}' }; @@ -40,9 +38,29 @@ const startLatLon: haversine.LatLonTuple = [11.4017529, 48.1548256]; const endLatLon: haversine.LatLonTuple = [13.1445551, 52.5065133]; const optionsLatLon: haversine.Options = { - unit: 'km', - threshold: 1, format: '[lat,lon]' }; haversine(startLatLon, endLatLon, optionsLatLon); + +const startGeoJSON = { + type: "Feature", + geometry: { + type: "LineString", + coordinates: startLatLon + } +}; + +const endGeoJSON = { + type: "Feature", + geometry: { + type: "LineString", + coordinates: endLatLon + } +}; + +const optionsGeoJSON: haversine.Options = { + format: 'geojson' +}; + +haversine(startGeoJSON, endGeoJSON, optionsGeoJSON); diff --git a/types/haversine/index.d.ts b/types/haversine/index.d.ts index e5ddb1273c..d828b8eb5a 100644 --- a/types/haversine/index.d.ts +++ b/types/haversine/index.d.ts @@ -4,20 +4,7 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare namespace haversine { - interface CoordinateLongNames { - longitude: number; - latitude: number; - } - - interface CoordinateShortNames { - lat: number; - lon: number; - } - - interface LatLonTuple extends Array { - 0: number; - 1: number; - } + type LatLonTuple = [number, number]; interface GeoJSON { geometry: { @@ -25,7 +12,13 @@ declare namespace haversine { }; } - type Coordinate = (CoordinateLongNames | CoordinateShortNames | LatLonTuple | GeoJSON); + type Coordinate = ({ + longitude: number; + latitude: number; + } | { + lat: number; + lon: number; + } | LatLonTuple | GeoJSON); interface Options { /** From a5417f724c7a0a9bf7fcd2f4e5418caf5909526e Mon Sep 17 00:00:00 2001 From: Daniel Hritzkiv Date: Thu, 16 Nov 2017 11:05:39 -0500 Subject: [PATCH 5/5] haversine: explictly declare LongtitudeLatitude and LonLat interfaces --- types/haversine/haversine-tests.ts | 8 ++++---- types/haversine/index.d.ts | 18 +++++++++++------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/types/haversine/haversine-tests.ts b/types/haversine/haversine-tests.ts index 2bfac64856..5c068ea4b3 100644 --- a/types/haversine/haversine-tests.ts +++ b/types/haversine/haversine-tests.ts @@ -1,11 +1,11 @@ import haversine = require('haversine'); -const start = { +const start: haversine.CoordinateLongitudeLatitude = { longitude: 48.1548256, latitude: 11.4017529 }; -const end = { +const end: haversine.CoordinateLongitudeLatitude = { longitude: 52.5065133, latitude: 13.1445551 }; @@ -17,12 +17,12 @@ const options: haversine.Options = { haversine(start, end, options); -const startShort = { +const startShort: haversine.CoordinateLonLat = { lon: 48.1548256, lat: 11.4017529 }; -const endShort = { +const endShort: haversine.CoordinateLonLat = { lon: 52.5065133, lat: 13.1445551 }; diff --git a/types/haversine/index.d.ts b/types/haversine/index.d.ts index d828b8eb5a..5f6d1e9173 100644 --- a/types/haversine/index.d.ts +++ b/types/haversine/index.d.ts @@ -4,6 +4,16 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare namespace haversine { + interface CoordinateLongitudeLatitude { + longitude: number; + latitude: number; + } + + interface CoordinateLonLat { + lon: number; + lat: number; + } + type LatLonTuple = [number, number]; interface GeoJSON { @@ -12,13 +22,7 @@ declare namespace haversine { }; } - type Coordinate = ({ - longitude: number; - latitude: number; - } | { - lat: number; - lon: number; - } | LatLonTuple | GeoJSON); + type Coordinate = (CoordinateLongitudeLatitude | CoordinateLonLat | LatLonTuple | GeoJSON); interface Options { /**