pewpew('cabbage', 'bge') // true
pewpew('garbage', 'bge') // true
pewpew('raccoon', 'bge') // false
Last active
August 3, 2021 21:28
-
-
Save ajitid/96bed873d9d40b70d53ee4360194f565 to your computer and use it in GitHub Desktop.
π« pewpew β a quick and cheap fuzzy matcher
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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For a faster implementation on V8, see https://github.com/bevacqua/fuzzysearch.