Coder Social home page Coder Social logo

get-closest's Introduction

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

get-closest's People

Contributors

podefr avatar arilotter avatar

Stargazers

AndrewYG avatar Diwakar Kumar avatar Istvan Vasil avatar Mateusz Śpiewak avatar Edir Dumaszak avatar Denis avatar Rakan Nimer avatar Eric Casthart avatar

Watchers

 avatar James Cloos avatar Rakan Nimer avatar Olivier Wietrich avatar

Forkers

arilotter

get-closest's Issues

.custom returns a number

The docs say this: getCloset.custom("mercure", days, compareLevenshteinDistance); // "mercredi", but in the test, you're doing days[getCloset.custom("mercure", days, compareLevenshteinDistance)]; and in my quick usage, I've observed that .custom does indeed return a number and not a string. I haven't looked at the code, but you have the original list, so it seems like it would be easy to return the item at the index instead of the index, and, I would argue, that's likely to be 1) what people would be expecting and 2) what people would want to happen.

requiring "assert" in index.js but it's not listed as a dependency

React-Native 0.42
@turf/turf": ^3.11.2

I am using these libraries and your package gets installed. I see in the package.json of "get_closest", that it was required by "/@turf/dissolve".

I am getting an error that I do not have the module "assert" and I was able to track it down as being required in your index.js ( https://github.com/cosmosio/get-closest/blob/master/index.js#L10 )

I believe this indicates that your project is not indicating this dependency. For now I have to manually install the package.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.