Created
July 23, 2011 19:33
-
-
Save nzakas/1101787 to your computer and use it in GitHub Desktop.
"Any word" filter for YUI Autocomplete
This file contains hidden or 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
/* | |
* 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