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 🙏

© 2025 – Pkg Stats / Ryan Hefner

simplesearch

v0.1.0

Published

Powerful list searching in simple packaging

Downloads

5

Readme

SimpleSearch

SimpleSearch is becoming a powerful JavaScript tool to filter an array for matched (and approximately matched) strings.

Installation and Setup

We use UMD to maintain compatibility with several popular module systems.

CommonJS Setup (for environments like Node.js and Browserify)

Install SimpleSearch...

npm install --save simplesearch

...and require it in your project

var SimpleSearch = require('simplesearch');
SimpleSearch.filter(...);

AMD Setup (for Require.JS)

Disclaimer: it's been awhile since I've used Require.JS, and I'm no expert. This method is currently untested, but it should work.

Install SimpleSearch by downloading a tagged release. Or you could install with npm and dig it out of the node_modules folder... I don't really know the preferred way to do package management in AMD environments.

Then, start using it like this:

require(['path/to/simplesearch.js'], function(SimpleSearch) {
  SimpleSearch.filter(...);
});

No module loader? No problem.

If you're just looking to dump a script into your page, we can work with that. Start by downloading the latest release.

<script src="/path/to/simplesearch.js"></script>
<script>
  window.SimpleSearch.filter(...);
</script>

Usage

SimpleSearch comes with two public methods. The full documentation also covers private methods, but you shouldn't use those. The following should be enough to get you started.

Filter(data, search)

Given an array as the first paramater and a string of search text as the second, filter will return a new array that is a subset of the original. The results are filtered according to the match criteria listed in the next section.

var result = SimpleSearch.filter(["foo", "bar", "baz"], "ba");
// result: ["bar", "baz"]

Matches(item, search)

Internally, the filter function passes each item through SimpleSearch.matches(), which is publicly available.

var matches = SimpleSearch.matches('foobar', 'fbr');
// matches: true

This function returns true or false for matches according to the following rules:

  • Matches can occur anywhere in the string, so SimpleSearch.matches('bar', 'a'); // true
  • The search string can leave out characters, so SimpleSearch.matchs('bar', 'br'); // true
  • Search string cannot switch character order, so SimpleSearch.matchs('bar', 'rb'); // false
  • Non-alphanumeric characters in the search string (including spaces) are ignored, so SimpleSearch.matches('bar', '(b a.r)'); //true
  • Word order of the target doesn't matter, so SimpleSearch.matches('bar foo', 'foobar'); // true

Roadmap

The matches function has some hard-coded rules about how spaces, special characters, text case, and word order work. All of that will soon be configurable.

Filter currently only handles arrays of strings. Filtering an array of objects is coming next.

// data as an array of objects instead of strings
var data = [{
  name: "berry",
  alias: ["strawberry", "acai", "blueberry"]
}, {
  name: "chickpea",
  alias: "garbanzo bean"
}, {
  name: "apple, raw"
}];

Instead of passing an array and a search string to filter, you will be able to pass a big configuration object.

// filter(obj)
SimpleSearch.filter({
  data: data,
  fields: ["name", "alias"], // fields to be searched, ordered by preference.
                             // we only search "alias" if "name" doesn't match
  exactMatch: false, // if set to true, "ba" would not match "bean"
  splitWords: true, // if set to false, "bean garb" wouldn't match "garbanzo bean"
  punctuation: false, // if set to true, "(raw)" wouldn't match "raw"
  success: function (match) {
    // a callback function to be run for each matching item
    // receives a `match` object with the following properties
    // - `field` -- the field in which the match was found
    // - `original` -- original object (from data array) in which match was found
    // - `charPos` -- array of indexes of matched characters
    //                e.g. [0, 1, 3] for "bean" given search string "ben"
  },
  fail: function (original) {
    // callback to be run if match fails
    // receives the original object that was tested for a match
  }
});

Farther future, we're looking into the Levenshtein algorithm to do fuzzy matching. This way, we'll be able to:

  • Give each match a score, based on similarity to the search term
  • Sort by score before returning the filtered array (and before calling success/fail callback functions)