
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
get-closest
Advanced tools
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.
npm install get-closest
Require get-closest:
var getCloset = 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:
getCloset.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.
getCloset.number(17.5, items); // 3, as items[3] === 20
getCloset.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.
getCloset.greaterNumber(0, items); // 0, as items[0] === 0 is an exact match.
Finally, you can get the closest item that is lower, using lowerNumber
:
getCloset.lowerNumber(9, items); // 0, as items[0] === 0;
getCloset.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"];
getCloset.custom("mercure", days, compareLevenshteinDistance); // "mercredi"
MIT
FAQs
Compare your item to items in an array and get the closest one.
The npm package get-closest receives a total of 423,069 weekly downloads. As such, get-closest popularity was classified as popular.
We found that get-closest demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.