Skip to content

Commit

Permalink
improved circuit breaker and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
bevacqua committed Mar 6, 2015
1 parent ca8017e commit 1f4d8d7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v1.0.3 Short Fuse

- Improved circuit-breaker when `needle` and `haystack` length are equal

# v1.0.2 Vodka Tonic

- Slightly updated circuit-breaker that tests for equal length first
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Fuzzy searching allows for flexibly matching a string with partial input, useful

# Demo

To see `fuzzysearch` in action, head over to [bevacqua.github.io/horsey](http://bevacqua.github.io/horsey), which is a demo of an autocomplete component that uses `fuzzysearch` to filter out results based on user input.
To see `fuzzysearch` in action, head over to [bevacqua.github.io/horsey][3], which is a demo of an autocomplete component that uses `fuzzysearch` to filter out results based on user input.

# Install

Expand All @@ -16,9 +16,9 @@ From `npm`
npm install --save fuzzysearch
```

# `fuzzysearch(query, data)`
# `fuzzysearch(needle, haystack)`

Returns `true` if `query` matches `data` using a fuzzy-searching algorithm.
Returns `true` if `needle` matches `haystack` using a fuzzy-searching algorithm. Note that this program doesn't implement _[levenshtein distance][2]_, but rather a simplified version where **there's no approximation**. The method will return `true` only if each character in the `needle` can be found in the `haystack` and occurs after the preceding character.

```js
fuzzysearch('twl', 'cartwheel') // <- true
Expand All @@ -30,6 +30,8 @@ fuzzysearch('eeel', 'cartwheel') // <- false
fuzzysearch('dog', 'cartwheel') // <- false
```

An exciting application for this kind of algorithm is to filter options from an autocomplete menu, check out [horsey][3] for an example on how that might look like.

# But! _`RegExp`s...!_

![chart showing abysmal performance for regexp-based implementation][1]
Expand All @@ -39,3 +41,5 @@ fuzzysearch('dog', 'cartwheel') // <- false
MIT

[1]: https://cloud.githubusercontent.com/assets/934293/6495796/106a61a6-c2ac-11e4-945d-3d1bb066a76e.png
[2]: http://en.wikipedia.org/wiki/Levenshtein_distance
[3]: http://bevacqua.github.io/horsey
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
'use strict';

function fuzzysearch (query, text) {
var tlen = text.length;
var qlen = query.length;
function fuzzysearch (needle, haystack) {
var tlen = haystack.length;
var qlen = needle.length;
if (qlen > tlen) {
return false;
}
if (qlen === tlen) {
return query === text;
return needle === haystack;
}
outer: for (var i = 0, j = 0; i < qlen; i++) {
var qch = query.charCodeAt(i);
var nch = needle.charCodeAt(i);
while (j < tlen) {
if (text.charCodeAt(j++) === qch) {
if (haystack.charCodeAt(j++) === nch) {
continue outer;
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fuzzysearch",
"description": "Tiny and blazing-fast fuzzy search in JavaScript",
"version": "1.0.2",
"version": "1.0.3",
"homepage": "https://github.com/bevacqua/fuzzysearch",
"author": {
"name": "Nicolas Bevacqua",
Expand Down

0 comments on commit 1f4d8d7

Please sign in to comment.