mirror of
https://github.com/devbridge/jQuery-Autocomplete.git
synced 2024-11-22 04:45:12 +00:00
hoist exact matches to top of autocomplete suggestions
This commit is contained in:
parent
615fd80030
commit
fd37a268aa
@ -530,9 +530,32 @@
|
||||
return filter(suggestion, query, queryLowerCase);
|
||||
})
|
||||
};
|
||||
|
||||
function wholeWordMatch(suggestion) {
|
||||
return (suggestion.split(/\b/).indexOf(queryLowerCase) >= 0);
|
||||
}
|
||||
|
||||
function preferExactMatches(a, b) {
|
||||
var sa = a.value.toLowerCase();
|
||||
var aWhole = wholeWordMatch(sa);
|
||||
var sb = b.value.toLowerCase();
|
||||
var bWhole = wholeWordMatch(sb);
|
||||
if (aWhole && bWhole) {
|
||||
if (sa === queryLowerCase && sb === queryLowerCase) {
|
||||
return 0;
|
||||
}
|
||||
return (sa === queryLowerCase) ? -1 : 1;
|
||||
}
|
||||
if (aWhole) {
|
||||
return -1;
|
||||
}
|
||||
if (bWhole) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (limit && data.suggestions.length > limit) {
|
||||
data.suggestions = data.suggestions.slice(0, limit);
|
||||
data.suggestions = data.suggestions.sort(preferExactMatches).slice(0, limit);
|
||||
}
|
||||
|
||||
return data;
|
||||
|
Loading…
Reference in New Issue
Block a user