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

get-closest

v0.0.4

Published

Compare your item to items in an array and get the closest one.

Downloads

1,392,467

Readme

Get closest

Compare your item to items in an array and get the closest one. It comes by default with number comparison tools but it can also be used with strings, or anything else that you want to compare.

Installation

npm install get-closest

How to use

Require get-closest:

var getClosest = require("get-closest");

Let's say that you have an array of items such as:

var items = [0, 10, 15, 20, 50];

And that you want to get the item that's the closest to the number 18, which would be 20 in our case:

getClosest.number(18, items); // 3, as items[3] === 20;

If you want to find the closest to 17.5, as it's exactly between 15 and 20, number will return the last number in the array that matches, which is 20 in our case.

getClosest.number(17.5, items); // 3, as items[3] === 20
getClosest.number(35, items); // 4, as items[4] === 50

If you're interested in getting the closest item that is greater, you can use greaterNumber:

tools.greaterNumber(1, items); // 1, as items[1] === 10

If there's an exact match, it's returned. The last exact match will be returned too, to be consistent .number.

getClosest.greaterNumber(0, items); // 0, as items[0] === 0 is an exact match.

Finally, you can get the closest item that is lower, using lowerNumber:

getClosest.lowerNumber(9, items); // 0, as items[0] === 0;

getClosest.lowerNumber(10, items); // 1, as items[1] === 10;

But you can also compare custom types by giving a custom comparison function, like string comparison with the levensthein distance:

/**
 * Returns the distance between the two strings using the Levenshtein method
 * @param {String} compareTo the string to test, it comes from the array
 * @param {String} baseItem the item that you want to test against the array
 * @returns {Number} it needs to return a distance as a number, whatever the type
 * of the current items is.
 */  
function compareLevenshteinDistance(compareTo, baseItem) {
  return new Levenshtein(compareTo, baseItem).distance;
}

var days = ["lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"];

getClosest.custom("mercure", days, compareLevenshteinDistance); // 2 (mercredi)

CHANGELOG

0.0.4 - MARCH 12 2017

  • Remove dependency on assert

LICENSE

MIT