Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use the new orderByDistance? #198

Open
WesSouza opened this issue May 31, 2019 · 1 comment
Open

How to use the new orderByDistance? #198

WesSouza opened this issue May 31, 2019 · 1 comment

Comments

@WesSouza
Copy link

I have a collection of objects that happens to have geographic coordinates. I want to sort them by distance and used this method before, and mapped the output to restore the properties, adding distance:

import { orderByDistance } from 'geolib';

import { IStation } from '~/state/station';

export const sortStationsByProximity = (
  stations: IStation[],
  latitude: number,
  longitude: number,
): IStation[] =>
  orderByDistance({ latitude, longitude }, stations).map(
    ({ distance, key }) => ({
      ...stations[Number(key)],
      distance,
    }),
  );

The 3.0 version changes the input for that method so that the only relevant properties are the coordinates. I couldn't find an example of what a code that uses this method achieves now.

Is the purpose of orderByDistance different? Should I sort my collection myself using getDistance as orderByDistance does internally?

@OscarBarrett
Copy link

Same problem here. Almost all of our use cases of orderByDistance use the actual distance values so this method becomes redundant.

Another problem with the new changes is that the output is typed as GeolibInputCoordinates[] even though it outputs the original objects passed in to coords. Even if you don't want to use the actual distance values, you wont be able to reference any other values in your original object without casting it back. A better type would be something like

orderByDistance<T[] extends GeolibInputCoordinates[]>(point: GeolibInputCoordinates, coords: T[], distanceFn?: DistanceFn | undefined): T[]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants