Skip to content

Instantly share code, notes, and snippets.

@nzakas
Created July 23, 2011 19:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nzakas/1101787 to your computer and use it in GitHub Desktop.
Save nzakas/1101787 to your computer and use it in GitHub Desktop.
"Any word" filter for YUI Autocomplete
/*
* 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);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment