Created
October 10, 2017 22:28
-
-
Save slugbyte/972e8d62907d6573368c83a619ef04a8 to your computer and use it in GitHub Desktop.
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
'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