Skip to content

Instantly share code, notes, and snippets.

@progging
Created May 15, 2019 05:58
Show Gist options
  • Save progging/94fbc74e94f759d58a560667b6222799 to your computer and use it in GitHub Desktop.
Save progging/94fbc74e94f759d58a560667b6222799 to your computer and use it in GitHub Desktop.
Simple fuzzy string matcher
/*
https://stackoverflow.com/a/15252131/3619158
The algorithm is very simple: loop through needle letters and check if they occur in the same order in the haystack:
*/
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