Skip to content

Instantly share code, notes, and snippets.

@mitsuhiko
Created December 6, 2013 16:12
Show Gist options
  • Save mitsuhiko/7827368 to your computer and use it in GitHub Desktop.
Save mitsuhiko/7827368 to your computer and use it in GitHub Desktop.
_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