manuelbieh / geolib

Zero dependency library to provide some basic geo functions
MIT License
4.21k stars 341 forks source link

Improve orderByDistance with type generics #293

Open leumasme opened 1 year ago

leumasme commented 1 year ago

This change adds a generic type to the orderByDistance function. It is not a breaking change, and just allows for easier usage of the function. Specifically, the function was previously typed as returning GeolibInputCoordinates[] which is a type that includes all the different ways of representing coordinates, with no respect to which specific type the passed input coordinates actually have. This was hard to use and not always complete as extra data could be added to the elements of the input coordinate array. This extra data would still be in the result of the function, but would not be in its return type.

Here is an example of what this change makes possible:

type CoordinatesAndExtraData = {
  lat: string, lon: string, locationName: string
};
// ... any data loaded here
let points: CoordinatesAndExtraData = generateSomeData();
let targetPoint: GeolibInputCoordinates = generateTargetPoint(); 
// Generate ordered list
let ordered = orderByDistance(targetPoint, points);
// This works but was previously disallowed by the types as locationName doesn't exist on `GeolibInputCoordinates`
console.log("The first point in the ordered list is "+ordered[0].locationName)
bdow commented 1 year ago

This would be amazing to get merged! @manuelbieh - any reason to not merge this?