Created
May 31, 2023 21:26
-
-
Save jxxe/90e02eabf77012a2f65c68c2e5c9cedf to your computer and use it in GitHub Desktop.
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
/** | |
* Checks if all characters of the needle exist in order in the haystack | |
* @param needle The search term | |
* @param haystack The string to search against | |
*/ | |
export default function fuzzySearch(needle: string, haystack: string): boolean { | |
if(needle === '') return true; | |
needle = needle.toLowerCase().replaceAll(' ', ''); | |
haystack = haystack.toLowerCase(); | |
for(let i = 0; i < haystack.length; i++) { | |
if(haystack.charAt(i) === needle.charAt(0)) { | |
needle = needle.slice(1); | |
} | |
if(needle.length === 0) { | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment