Skip to content

Instantly share code, notes, and snippets.

@scwood
Last active February 23, 2018 19:56
Show Gist options
  • Select an option

  • Save scwood/720a7084f5364caddea1f604ede8d676 to your computer and use it in GitHub Desktop.

Select an option

Save scwood/720a7084f5364caddea1f604ede8d676 to your computer and use it in GitHub Desktop.
Fuzzy Search in JS
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