Skip to content

Instantly share code, notes, and snippets.

@tborychowski
Created August 22, 2014 12:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tborychowski/b3a23cd924f215724854 to your computer and use it in GitHub Desktop.
Save tborychowski/b3a23cd924f215724854 to your computer and use it in GitHub Desktop.
JS :: fuzzy search
String.prototype.fuzzy = function (s) {
var hay = this.toLowerCase(), i = 0, n = -1, l;
s = s.toLowerCase();
for (; l = s[i++] ;) if (!~(n = hay.indexOf(l, n + 1))) return false;
return true;
};
('a haystack with a needle').fuzzy('hay sucks'); // false
('a haystack with a needle').fuzzy('sack hand'); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment