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 🙏

© 2026 – Pkg Stats / Ryan Hefner

closest-str

v2.2.1

Published

closest-str is a library for finding closest key in the dictionary and getting it's value.

Readme

closest-str

closest-str is a library for finding closest key in the dictionary and getting it's value.

Installation

Install a lib using npm

npm i closest-str

Usage

Basic usage:

var closest = require("closest-str");
closest.setdict({
    "hello": function(query) {
        return "Hello! You typed: " + query
    },
    "goodbye": function() {
        return "Goodbye!";
    }
});

// or

closest.setdict([
	{
		query: "hello",
		answer: function(query) {
			return "Hello! You typed: " + query
		}
	}
	// ...
]);

function makeRequest(query) {
    return closest.request(query).answer(query);
} 

makeRequest("hello, random guy!"); // "Hello! You typed: hello, random guy!"
makeRequest("goodbye then"); // "Goodbye!"

Load dict from file

closest.setdict("./dict.json"); // file should be json

You can detect, if all matches are too low:

closest.setlow(0.4);
closest.setlow("No matching keys");
closest.request("This is a random string, no close key in dictionary"); // { query: null, answer: "No matching keys" }

We can optimize our code... We don't need to check other keys if there was exact match.

closest.setmax(1); // Do not check other keys if there was 100% similarity
// closest.setmax(0.95); // If 95%

Also we can provide custom params for separate queries.

closest.request("hello", {
	low: 0.5,
	lowresponse: "I don't understand you",
	max: 0.9,
	skip: 5 // Skip first 5 keys
});

What can it be used for?

Simple bots:

closest.setdict({
	"weather": function(query, callback) {
		/* Something to get weather */
		callback(/*result*/);
	}
	/* More commands here */
});

closest.setlow(0.3);
closest.setlow(function(query, callback) {
	callback("I don't understand you");
});

function makeRequest(query, callback) {
	closest.request(query).answer(callback);
}

makeRequest("what's the weather", console.log);

Word correction:

closest.setdict({
	"pen": "pen",
	"apple": "apple",
	"pineapple": "pineapple"
});

closest.setlow(0.4);
closest.setlow("No idea");

closest.request("aple").answer; // "apple"
closest.request("pnapple").answer; // "pineapple"

Chat bot... why not?

var fs = require("fs");
var closest = require("closest-str");
var answers = fs.readFileSync("./answers.txt").split("\n");
var dict = {};

answers.forEach(function(ans) {
	var s = ans.split("\\");
	if (dict[s[0]]) dict[s[0]].push(s[1]);
	else dict[s[0]] = [dict[s[1]]];
});

closest.setdict(dict);

function randPick(arr) {
	return arr[Math.floor(Math.random() * arr.length)];
}

function makeRequest(query) {
	return randPick(closest.request(query).answer);
}

Other

For comparing two strings

closest._similarity("test", "tost"); // 0.75 (75%)
closest._similarity("test", "TEST"); // 1 (100%, case-insensetive)
closest.__dict; // current dict
closest.__low; // lowest similarity
closest.__lowresponse; // lowest response
closest.__max; // maximum similarity

Changelog

  • 2.2.0
  • Load dict from file
  • 2.1.0
  • Added custom params for separate queries
  • Added max similarity
  • Dict can be array
  • 2.0.0
  • Initial release

TODO

  • Async requests (for big dictionaries)