autocomplete-filters.js revision ce76332f7c5a79a0abc3c9e457804818ff5affea
624675c154cbb5186e8c760b853ca79835987c9eEric FerraiuoloYUI.add('autocomplete-filters', function(Y) {
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo/**
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo * Provides pre-built result matching filters for AutoComplete.
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo *
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo * @module autocomplete
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo * @submodule autocomplete-filters
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo * @class AutoCompleteFilters
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo * @static
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo */
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolovar YArray = Y.Array,
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo YObject = Y.Object,
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo WordBreak = Y.Unicode.WordBreak,
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo
624675c154cbb5186e8c760b853ca79835987c9eEric FerraiuoloFilters = Y.mix(Y.namespace('AutoCompleteFilters'), {
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo // -- Public Methods -------------------------------------------------------
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo /**
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 *
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 * @static
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo */
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.
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo var queryChars = YArray.unique((caseSensitive ? query :
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo query.toLowerCase()).split(''));
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo return YArray.filter(results, function (result) {
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo if (!caseSensitive) {
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo result = result.toLowerCase();
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo }
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo return YArray.every(queryChars, function (chr) {
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo return result.indexOf(chr) !== -1;
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo });
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo });
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo },
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo /**
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo * Case-sensitive version of <code>charMatch()</code>.
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo *
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 * @static
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo */
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo charMatchCase: function (query, results) {
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo return Filters.charMatch(query, results, true);
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo },
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo /**
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo * Returns an array of results that contain the complete query as a phrase.
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo * Case-insensitive.
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo *
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 * @static
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo */
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
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo if (!caseSensitive) {
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo query = query.toLowerCase();
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo }
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo return YArray.filter(results, function (result) {
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo return (caseSensitive ? result : result.toLowerCase()).indexOf(query) !== -1;
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo });
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo },
039c1e71e8a73e299b06634cb4a9897393179cf2Eric Ferraiuolo
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo /**
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo * Case-sensitive version of <code>phraseMatch()</code>.
f386d69afd040f61944cff1eaad6cf615c147a6dEric Ferraiuolo *
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
624675c154cbb5186e8c760b853ca79835987c9eEric Ferraiuolo * @static
*/
phraseMatchCase: function (query, results) {
return Filters.phraseMatch(query, results, true);
},
/**
* Returns an array of results that start with the complete query as a
* phrase. Case-insensitive.
*
* @method startsWith
* @param {String} query Query to match
* @param {Array} results Results to filter
* @return {Array} Filtered results
* @static
*/
startsWith: function (query, results, caseSensitive) {
// The caseSensitive parameter is only intended for use by
// startsWithCase(). It's intentionally undocumented.
if (!caseSensitive) {
query = query.toLowerCase();
}
return YArray.filter(results, function (result) {
return (caseSensitive ? result : result.toLowerCase()).indexOf(query) === 0;
});
},
/**
* Case-sensitive version of <code>startsWith()</code>.
*
* @method startsWithCase
* @param {String} query Query to match
* @param {Array} results Results to filter
* @return {Array} Filtered results
* @static
*/
startsWithCase: function (query, results) {
return Filters.startsWith(query, results, true);
},
/**
* Returns an array of results that contain all of the words in the query,
* in any order. Non-word characters like whitespace and certain punctuation
* are ignored. Case-insensitive.
*
* @method wordMatch
* @param {String} query Query to match
* @param {Array} results Results to filter
* @return {Array} Filtered results
* @static
*/
wordMatch: function (query, results, caseSensitive) {
// The caseSensitive parameter is only intended for use by
// wordMatchCase(). It's intentionally undocumented.
var options = {ignoreCase: !caseSensitive},
queryWords = WordBreak.getUniqueWords(query, options);
return YArray.filter(results, function (result) {
// Convert resultWords array to a hash for fast lookup.
var resultWords = YArray.hash(WordBreak.getUniqueWords(result, options));
return YArray.every(queryWords, function (word) {
return YObject.owns(resultWords, word);
});
});
},
/**
* Case-sensitive version of <code>wordMatch()</code>.
*
* @method wordMatchCase
* @param {String} query Query to match
* @param {Array} results Results to filter
* @return {Array} Filtered results
* @static
*/
wordMatchCase: function (query, results) {
return Filters.wordMatch(query, results, true);
}
});
}, '@VERSION@' ,{requires:['array-extras', 'unicode-wordbreak']});