public
Created

"Any word" filter for YUI Autocomplete

  • Download Gist
anyword.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
* Use the following in the "resultFilters" option when creating a YUI 3 Autocomplete
* widget. This will return matches when *any* of the words in the query string match.
* This is opposed to the default "wordMatch", which matches *all* words in the
* query string.
*/
 
function matchAnyWord(query, results) {
 
var WordBreak = Y.Text.WorkBreak,
options = { ignoreCase: 1 },
queryWords = WordBreak.getUniqueWords(query, options);
 
return Y.Array.filter(results, function (result) {
// Convert resultWords array to a hash for fast lookup.
var resultWords = Y.Array.hash(WordBreak.getUniqueWords(result.text,
options));
 
return Y.Array.some(queryWords, function (word) {
return Y.Object.owns(resultWords, word);
});
});
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.