Last active
February 23, 2018 19:56
-
-
Save scwood/720a7084f5364caddea1f604ede8d676 to your computer and use it in GitHub Desktop.
Fuzzy Search in JS
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
| const search = 'fiouedblds'; | |
| const strings = ['something random', '_filterOutNonEditableFields', 'abc', 'fiouedblds']; | |
| function findFuzzyMatches(strings, search) { | |
| return strings.filter((string) => isFuzzyMatch(string, search)); | |
| } | |
| function isFuzzyMatch(string, search) { | |
| string = string.toLowerCase(); | |
| search = search.toLowerCase(); | |
| let i = 0; | |
| for (let j = 0; j < string.length; j++) { | |
| if (string.charAt(j) === search.charAt(i) ) { | |
| i++; | |
| } | |
| if (i === search.length) { | |
| return true; | |
| } | |
| } | |
| return false; | |
| } | |
| console.log(findFuzzyMatches(strings, search)); | |
| // [ '_filterOutNonEditableFields', 'fiouedblds' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment