Created
December 6, 2013 16:12
-
-
Save mitsuhiko/7827368 to your computer and use it in GitHub Desktop.
This file contains 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
_getLocalSuggestions: function(terms) { | |
var that = this, firstChars = {}, lists = [], shortestList, suggestions = []; | |
utils.each(terms, function(i, term) { | |
var firstChar = term.charAt(0); | |
!~utils.indexOf(firstChars, firstChar) && firstChars.push(firstChar); | |
}); | |
utils.each(firstChars, function(i, firstChar) { | |
var list = that.adjacencyList[firstChar]; | |
if (!list) { | |
return false; | |
} | |
lists.push(list); | |
if (!shortestList || list.length < shortestList.length) { | |
shortestList = list; | |
} | |
}); | |
if (lists.length < firstChars.length) { | |
return []; | |
} | |
utils.each(shortestList, function(i, id) { | |
var item = that.itemHash[id], isCandidate, isMatch; | |
isCandidate = utils.every(lists, function(list) { | |
return ~utils.indexOf(list, id); | |
}); | |
isMatch = isCandidate && utils.every(terms, function(term) { | |
return utils.some(item.tokens, function(token) { | |
return token.indexOf(term) === 0; | |
}); | |
}); | |
isMatch && suggestions.push(item); | |
}); | |
return suggestions; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment