Skip to content

Instantly share code, notes, and snippets.

@ajitid
Last active August 3, 2021 21:28
Show Gist options
  • Save ajitid/96bed873d9d40b70d53ee4360194f565 to your computer and use it in GitHub Desktop.
Save ajitid/96bed873d9d40b70d53ee4360194f565 to your computer and use it in GitHub Desktop.
πŸ”« pewpew β€” a quick and cheap fuzzy matcher

Usage

pewpew('cabbage', 'bge') // true
pewpew('garbage', 'bge') // true
pewpew('raccoon', 'bge') // false
function pewpew(text, query) {
const textLen = text.length
const queryLen = query.length
if (queryLen > textLen) return false;
if(queryLen === textLen) return query === text
let textIdx = 0;
let queryIdx = 0;
while (textIdx < textLen) {
if (query[queryIdx] === text[textIdx]) {
++queryIdx;
if (queryIdx === queryLen) return true;
}
++textIdx;
}
return false;
}
@ajitid
Copy link
Author

ajitid commented Aug 3, 2021

For a faster implementation on V8, see https://github.com/bevacqua/fuzzysearch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment