Created
May 15, 2019 05:58
-
-
Save progging/94fbc74e94f759d58a560667b6222799 to your computer and use it in GitHub Desktop.
Simple fuzzy string matcher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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