Created
December 20, 2012 03:48
-
-
Save Suor/4342814 to your computer and use it in GitHub Desktop.
fuzzy matching try
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
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