Skip to content

Instantly share code, notes, and snippets.

@jxxe
Created May 31, 2023 21:26
Show Gist options
  • Save jxxe/90e02eabf77012a2f65c68c2e5c9cedf to your computer and use it in GitHub Desktop.
Save jxxe/90e02eabf77012a2f65c68c2e5c9cedf to your computer and use it in GitHub Desktop.
/**
* 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