Created
December 11, 2015 22:49
-
-
Save hergaiety/ebbb92bcb6b4123e1e8a to your computer and use it in GitHub Desktop.
Test if given string is "fuzzy-found" within a longer string, the "pattern". Thanks Jan Dvorak!
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
/** | |
* Test if given string is "fuzzy-found" within a longer string, the "pattern" | |
* @return {[boolean]} [returns true if src can be "fuzzy-found" within the pattern] | |
*/ | |
export default function(str, pattern) { | |
str = str.toLowerCase(); | |
pattern = pattern.toLowerCase(); | |
// Code shamelessly borrowed from Jan Dvorak, you're awesome! http://codereview.stackexchange.com/questions/23899/faster-javascript-fuzzy-string-matching-function | |
pattern = pattern.split("").reduce(function(a,b){ return a+".*"+b; }); | |
return (new RegExp(pattern)).test(str); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment