autocomplete-filters.js revision ce76332f7c5a79a0abc3c9e457804818ff5affea
624675c154cbb5186e8c760b853ca79835987c9eEric FerraiuoloYUI.add('autocomplete-filters', function(Y) {
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo * Provides pre-built result matching filters for AutoComplete.
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo * @module autocomplete
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo * @submodule autocomplete-filters
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo * @class AutoCompleteFilters
624675c154cbb5186e8c760b853ca79835987c9eEric FerraiuoloFilters = Y.mix(Y.namespace('AutoCompleteFilters'), {
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo // -- Public Methods -------------------------------------------------------
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo * Returns an array of results that contain all of the characters in the
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo * query, in any order (not necessarily consecutive). Case-insensitive.
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo * @method charMatch
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo * @param {String} query Query to match
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo * @param {Array} results Results to filter
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo * @return {Array} Filtered results
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo charMatch: function (query, results, caseSensitive) {
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo // The caseSensitive parameter is only intended for use by
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo // charMatchCase(). It's intentionally undocumented.
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo var queryChars = YArray.unique((caseSensitive ? query :
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo return YArray.filter(results, function (result) {
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo return YArray.every(queryChars, function (chr) {
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo * Case-sensitive version of <code>charMatch()</code>.
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo * @method charMatchCase
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo * @param {String} query Query to match
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo * @param {Array} results Results to filter
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo * @return {Array} Filtered results
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo return Filters.charMatch(query, results, true);
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo * Returns an array of results that contain the complete query as a phrase.
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo * Case-insensitive.
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo * @method phraseMatch
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo * @param {String} query Query to match
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo * @param {Array} results Results to filter
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo * @return {Array} Filtered results
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo phraseMatch: function (query, results, caseSensitive) {
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo // The caseSensitive parameter is only intended for use by
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo // phraseMatchCase(). It's intentionally undocumented.
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo return YArray.filter(results, function (result) {
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo return (caseSensitive ? result : result.toLowerCase()).indexOf(query) !== -1;
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo * Case-sensitive version of <code>phraseMatch()</code>.
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo * @method phraseMatchCase
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo * @param {String} query Query to match
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo * @param {Array} results Results to filter
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo * @return {Array} Filtered results
if (!caseSensitive) {