MDL-32843 import YUI 3.5.1
[moodle.git] / lib / yui / 3.5.1 / build / autocomplete-filters / autocomplete-filters.js
blob90380a6e05cea86d063f0de1d2c40f6cebd2768b
1 /*
2 YUI 3.5.1 (build 22)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('autocomplete-filters', function(Y) {
9 /**
10 Provides pre-built result matching filters for AutoComplete.
12 @module autocomplete
13 @submodule autocomplete-filters
14 @class AutoCompleteFilters
15 @static
16 **/
18 var YArray     = Y.Array,
19     YObject    = Y.Object,
20     WordBreak  = Y.Text.WordBreak,
22 Filters = Y.mix(Y.namespace('AutoCompleteFilters'), {
23     // -- Public Methods -------------------------------------------------------
25     /**
26     Returns an array of results that contain all of the characters in the query,
27     in any order (not necessarily consecutive). Case-insensitive.
29     @method charMatch
30     @param {String} query Query to match
31     @param {Array} results Results to filter
32     @return {Array} Filtered results
33     @static
34     **/
35     charMatch: function (query, results, caseSensitive) {
36         // The caseSensitive parameter is only intended for use by
37         // charMatchCase(). It's intentionally undocumented.
39         if (!query) { return results; }
41         var queryChars = YArray.unique((caseSensitive ? query :
42                 query.toLowerCase()).split(''));
44         return YArray.filter(results, function (result) {
45             result = result.text;
47             if (!caseSensitive) {
48                 result = result.toLowerCase();
49             }
51             return YArray.every(queryChars, function (chr) {
52                 return result.indexOf(chr) !== -1;
53             });
54         });
55     },
57     /**
58     Case-sensitive version of `charMatch()`.
60     @method charMatchCase
61     @param {String} query Query to match
62     @param {Array} results Results to filter
63     @return {Array} Filtered results
64     @static
65     **/
66     charMatchCase: function (query, results) {
67         return Filters.charMatch(query, results, true);
68     },
70     /**
71     Returns an array of results that contain the complete query as a phrase.
72     Case-insensitive.
74     @method phraseMatch
75     @param {String} query Query to match
76     @param {Array} results Results to filter
77     @return {Array} Filtered results
78     @static
79     **/
80     phraseMatch: function (query, results, caseSensitive) {
81         // The caseSensitive parameter is only intended for use by
82         // phraseMatchCase(). It's intentionally undocumented.
84         if (!query) { return results; }
86         if (!caseSensitive) {
87             query = query.toLowerCase();
88         }
90         return YArray.filter(results, function (result) {
91             return (caseSensitive ? result.text : result.text.toLowerCase()).indexOf(query) !== -1;
92         });
93     },
95     /**
96     Case-sensitive version of `phraseMatch()`.
98     @method phraseMatchCase
99     @param {String} query Query to match
100     @param {Array} results Results to filter
101     @return {Array} Filtered results
102     @static
103     **/
104     phraseMatchCase: function (query, results) {
105         return Filters.phraseMatch(query, results, true);
106     },
108     /**
109     Returns an array of results that start with the complete query as a phrase.
110     Case-insensitive.
112     @method startsWith
113     @param {String} query Query to match
114     @param {Array} results Results to filter
115     @return {Array} Filtered results
116     @static
117     **/
118     startsWith: function (query, results, caseSensitive) {
119         // The caseSensitive parameter is only intended for use by
120         // startsWithCase(). It's intentionally undocumented.
122         if (!query) { return results; }
124         if (!caseSensitive) {
125             query = query.toLowerCase();
126         }
128         return YArray.filter(results, function (result) {
129             return (caseSensitive ? result.text : result.text.toLowerCase()).indexOf(query) === 0;
130         });
131     },
133     /**
134     Case-sensitive version of `startsWith()`.
136     @method startsWithCase
137     @param {String} query Query to match
138     @param {Array} results Results to filter
139     @return {Array} Filtered results
140     @static
141     **/
142     startsWithCase: function (query, results) {
143         return Filters.startsWith(query, results, true);
144     },
146     /**
147     Returns an array of results in which all the words of the query match either
148     whole words or parts of words in the result. Non-word characters like
149     whitespace and certain punctuation are ignored. Case-insensitive.
151     This is basically a combination of `wordMatch()` (by ignoring whitespace and
152     word order) and `phraseMatch()` (by allowing partial matching instead of
153     requiring the entire word to match).
155     Example use case: Trying to find personal names independently of name order
156     (Western or Eastern order) and supporting immediate feedback by allowing
157     partial occurences. So queries like "J. Doe", "Doe, John", and "J. D." would
158     all match "John Doe".
160     @method subWordMatch
161     @param {String} query Query to match
162     @param {Array} results Results to filter
163     @return {Array} Filtered results
164     @static
165     **/
166     subWordMatch: function (query, results, caseSensitive) {
167         // The caseSensitive parameter is only intended for use by
168         // subWordMatchCase(). It's intentionally undocumented.
170         if (!query) { return results; }
172         var queryWords = WordBreak.getUniqueWords(query, {
173             ignoreCase: !caseSensitive
174         });
176         return YArray.filter(results, function (result) {
177             var resultText = caseSensitive ? result.text :
178                     result.text.toLowerCase();
180             return YArray.every(queryWords, function (queryWord) {
181                 return resultText.indexOf(queryWord) !== -1;
182             });
183         });
184     },
186     /**
187     Case-sensitive version of `subWordMatch()`.
189     @method subWordMatchCase
190     @param {String} query Query to match
191     @param {Array} results Results to filter
192     @return {Array} Filtered results
193     @static
194     **/
195     subWordMatchCase: function (query, results) {
196         return Filters.subWordMatch(query, results, true);
197     },
199     /**
200     Returns an array of results that contain all of the words in the query, in
201     any order. Non-word characters like whitespace and certain punctuation are
202     ignored. Case-insensitive.
204     @method wordMatch
205     @param {String} query Query to match
206     @param {Array} results Results to filter
207     @return {Array} Filtered results
208     @static
209     **/
210     wordMatch: function (query, results, caseSensitive) {
211         // The caseSensitive parameter is only intended for use by
212         // wordMatchCase(). It's intentionally undocumented.
214         if (!query) { return results; }
216         var options    = {ignoreCase: !caseSensitive},
217             queryWords = WordBreak.getUniqueWords(query, options);
219         return YArray.filter(results, function (result) {
220             // Convert resultWords array to a hash for fast lookup.
221             var resultWords = YArray.hash(WordBreak.getUniqueWords(result.text,
222                                 options));
224             return YArray.every(queryWords, function (word) {
225                 return YObject.owns(resultWords, word);
226             });
227         });
228     },
230     /**
231     Case-sensitive version of `wordMatch()`.
233     @method wordMatchCase
234     @param {String} query Query to match
235     @param {Array} results Results to filter
236     @return {Array} Filtered results
237     @static
238     **/
239     wordMatchCase: function (query, results) {
240         return Filters.wordMatch(query, results, true);
241     }
245 }, '3.5.1' ,{requires:['array-extras', 'text-wordbreak']});