Skip to content

Instantly share code, notes, and snippets.

@Suor
Created December 20, 2012 03:48
Show Gist options
  • Save Suor/4342814 to your computer and use it in GitHub Desktop.
Save Suor/4342814 to your computer and use it in GitHub Desktop.
fuzzy matching try
var q = query.replace(/\s+/, ' '),
re = new RegExp('^(.*?)' + q.split('').join('(.*?)'));
function score(junk) {
if (!junk) return Infinity;
return junk.slice(1).reduce(function (len, item) {return len + item.length}, 0);
}
return list.reduce(function (best, item) {
var junk = item.title.match(re),
item_score = score(junk);
return (item_score < best.score) ? {match: item, score: item_score} : best;
}, {match: null, score: Infinity}).match;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment