npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sort-by-distance

v2.0.0

Published

Sort array of points based on how close they are to a givin point

Downloads

6,107

Readme

sort-by-distance

package version package downloads standard-readme compliant package license make a pull request

Sort array of points based on how close they are to a givin point

Table of Contents

Install

Install the package locally within you project folder with your package manager:

With npm:

npm install sort-by-distance

With yarn:

yarn add sort-by-distance

With pnpm:

pnpm add sort-by-distance

Usage

Call the function providing the origin point and an array of other points as arguments:

import { sortByDistance } from 'sort-by-distance'

const points = [
	{ x: 3, y: 5},
	{ x: 80, y: 34},
	{ x: 3, y: 7},
	{ x: 22, y: 88},
	{ x: 100, y: 60},
]

const origin = { x: 50, y: 50}

console.log(sortByDistance(origin, points))

//[ { x: 80, y: 34, distance: 34 },
//  { x: 22, y: 88, distance: 47.20169488482379 },
//  { x: 100, y: 60, distance: 50.99019513592785 },
//  { x: 3, y: 7, distance: 63.702433234531945 },
//  { x: 3, y: 5, distance: 65.06919393998976 } ]

You can also change the name of the x and y:

import { sortByDistance } from 'sort-by-distance'

const points = [
	{ longitude: 3, latitude: 5},
	{ longitude: 80, latitude: 34},
	{ longitude: 3, latitude: 7},
	{ longitude: 22, latitude: 88},
	{ longitude: 100, latitude: 60},
]

const opts = {
	yName: 'latitude',
	xName: 'longitude'
}

const origin = { longitude: 4, latitude: 22}

console.log(sortByDistance(origin, points, opts))

//[ { longitude: 3, latitude: 7, distance: 15.033296378372908 },
//  { longitude: 3, latitude: 5, distance: 17.029386365926403 },
//  { longitude: 22, latitude: 88, distance: 68.41052550594829 },
//  { longitude: 80, latitude: 34, distance: 76.94153624668537 },
//  { longitude: 100, latitude: 60, distance: 103.24727599312246 } ]

The object is cloned and the distance from the origin point is added as an property of the new object.

API

The module exports a single function with the signature:

sortByDistance(originPoint, arrayOfPoints, options)

originPoint

Is an object containing x and y properties.

arrayOfPoints

Is an array of objects containing x and y properties.

options

The options available and their defaults:


{
	yName: 'y', // Name of the y property to look for on the object
	xName: 'x' // Name of the x property to look for on the object
	type: 'linear' // "linear" or "haversine";
}

For all configuration options, please see the API docs.

Contributing

Got an idea for a new feature? Found a bug? Contributions are welcome! Please open up an issue or make a pull request.

License

MIT © Tiaan du Plessis