Skip to content

Instantly share code, notes, and snippets.

@slugbyte
Created October 10, 2017 22:28
Show Gist options
  • Save slugbyte/972e8d62907d6573368c83a619ef04a8 to your computer and use it in GitHub Desktop.
Save slugbyte/972e8d62907d6573368c83a619ef04a8 to your computer and use it in GitHub Desktop.
'use strict'
// fuzzy search
//
// list filter by a regex that matches a order of characters with anything inbeween them
// the filter azb whould match string that has a before z before b
// a fuzzy azb would be the regex /.*a.*z.*b.*/
fuzzy = (filterTerm) => new RegExp('.*' + filterTerm.toLowerCase().split('').join('.*') + '.*')
fuzzyFilter = (term, collection) => {
let regex = fuzzy(term)
console.log(regex)
return collection.filter(text => {
return regex.test(text.toLowerCase())
})
}
content = ['hello world', 'cool school', 'snake clap clap', 'shark in the dark']
fuzzyFilter('SNK', content)
query = {
username: 'slugbyte',
location: 'seattle',
bio: 'lulwat'
}
let $options = 'i'
if(query.username) query.username = {$regex: fuzzy(query.username), $options}
if(query.location) query.location = {$regex: fuzzy(query.location), $options}
query
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment