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 = ;SimpleSearch;
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:
;
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.
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;// result: ["bar", "baz"]
Matches(item, search)
Internally, the filter
function passes each item through SimpleSearch.matches()
, which is publicly available.
var matches = SimpleSearch;// 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 stringsvar 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;
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)