NOBUG: Fixed file access permissions
[moodle.git] / lib / yuilib / 3.13.0 / autocomplete-filters / autocomplete-filters.js
bloba768080acd439577f999fbd3b2c49797f3f144a6
1 /*
2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
8 YUI.add('autocomplete-filters', function (Y, NAME) {
10 /**
11 Provides pre-built result matching filters for AutoComplete.
13 @module autocomplete
14 @submodule autocomplete-filters
15 @class AutoCompleteFilters
16 @static
17 **/
19 var YArray     = Y.Array,
20     YObject    = Y.Object,
21     WordBreak  = Y.Text.WordBreak,
23 Filters = Y.mix(Y.namespace('AutoCompleteFilters'), {
24     // -- Public Methods -------------------------------------------------------
26     /**
27     Returns an array of results that contain all of the characters in the query,
28     in any order (not necessarily consecutive). Case-insensitive.
30     @method charMatch
31     @param {String} query Query to match
32     @param {Array} results Results to filter
33     @return {Array} Filtered results
34     @static
35     **/
36     charMatch: function (query, results, caseSensitive) {
37         // The caseSensitive parameter is only intended for use by
38         // charMatchCase(). It's intentionally undocumented.
40         if (!query) { return results; }
42         var queryChars = YArray.unique((caseSensitive ? query :
43                 query.toLowerCase()).split(''));
45         return YArray.filter(results, function (result) {
46             result = result.text;
48             if (!caseSensitive) {
49                 result = result.toLowerCase();
50             }
52             return YArray.every(queryChars, function (chr) {
53                 return result.indexOf(chr) !== -1;
54             });
55         });
56     },
58     /**
59     Case-sensitive version of `charMatch()`.
61     @method charMatchCase
62     @param {String} query Query to match
63     @param {Array} results Results to filter
64     @return {Array} Filtered results
65     @static
66     **/
67     charMatchCase: function (query, results) {
68         return Filters.charMatch(query, results, true);
69     },
71     /**
72     Returns an array of results that contain the complete query as a phrase.
73     Case-insensitive.
75     @method phraseMatch
76     @param {String} query Query to match
77     @param {Array} results Results to filter
78     @return {Array} Filtered results
79     @static
80     **/
81     phraseMatch: function (query, results, caseSensitive) {
82         // The caseSensitive parameter is only intended for use by
83         // phraseMatchCase(). It's intentionally undocumented.
85         if (!query) { return results; }
87         if (!caseSensitive) {
88             query = query.toLowerCase();
89         }
91         return YArray.filter(results, function (result) {
92             return (caseSensitive ? result.text : result.text.toLowerCase()).indexOf(query) !== -1;
93         });
94     },
96     /**
97     Case-sensitive version of `phraseMatch()`.
99     @method phraseMatchCase
100     @param {String} query Query to match
101     @param {Array} results Results to filter
102     @return {Array} Filtered results
103     @static
104     **/
105     phraseMatchCase: function (query, results) {
106         return Filters.phraseMatch(query, results, true);
107     },
109     /**
110     Returns an array of results that start with the complete query as a phrase.
111     Case-insensitive.
113     @method startsWith
114     @param {String} query Query to match
115     @param {Array} results Results to filter
116     @return {Array} Filtered results
117     @static
118     **/
119     startsWith: function (query, results, caseSensitive) {
120         // The caseSensitive parameter is only intended for use by
121         // startsWithCase(). It's intentionally undocumented.
123         if (!query) { return results; }
125         if (!caseSensitive) {
126             query = query.toLowerCase();
127         }
129         return YArray.filter(results, function (result) {
130             return (caseSensitive ? result.text : result.text.toLowerCase()).indexOf(query) === 0;
131         });
132     },
134     /**
135     Case-sensitive version of `startsWith()`.
137     @method startsWithCase
138     @param {String} query Query to match
139     @param {Array} results Results to filter
140     @return {Array} Filtered results
141     @static
142     **/
143     startsWithCase: function (query, results) {
144         return Filters.startsWith(query, results, true);
145     },
147     /**
148     Returns an array of results in which all the words of the query match either
149     whole words or parts of words in the result. Non-word characters like
150     whitespace and certain punctuation are ignored. Case-insensitive.
152     This is basically a combination of `wordMatch()` (by ignoring whitespace and
153     word order) and `phraseMatch()` (by allowing partial matching instead of
154     requiring the entire word to match).
156     Example use case: Trying to find personal names independently of name order
157     (Western or Eastern order) and supporting immediate feedback by allowing
158     partial occurences. So queries like "J. Doe", "Doe, John", and "J. D." would
159     all match "John Doe".
161     @method subWordMatch
162     @param {String} query Query to match
163     @param {Array} results Results to filter
164     @return {Array} Filtered results
165     @static
166     **/
167     subWordMatch: function (query, results, caseSensitive) {
168         // The caseSensitive parameter is only intended for use by
169         // subWordMatchCase(). It's intentionally undocumented.
171         if (!query) { return results; }
173         var queryWords = WordBreak.getUniqueWords(query, {
174             ignoreCase: !caseSensitive
175         });
177         return YArray.filter(results, function (result) {
178             var resultText = caseSensitive ? result.text :
179                     result.text.toLowerCase();
181             return YArray.every(queryWords, function (queryWord) {
182                 return resultText.indexOf(queryWord) !== -1;
183             });
184         });
185     },
187     /**
188     Case-sensitive version of `subWordMatch()`.
190     @method subWordMatchCase
191     @param {String} query Query to match
192     @param {Array} results Results to filter
193     @return {Array} Filtered results
194     @static
195     **/
196     subWordMatchCase: function (query, results) {
197         return Filters.subWordMatch(query, results, true);
198     },
200     /**
201     Returns an array of results that contain all of the words in the query, in
202     any order. Non-word characters like whitespace and certain punctuation are
203     ignored. Case-insensitive.
205     @method wordMatch
206     @param {String} query Query to match
207     @param {Array} results Results to filter
208     @return {Array} Filtered results
209     @static
210     **/
211     wordMatch: function (query, results, caseSensitive) {
212         // The caseSensitive parameter is only intended for use by
213         // wordMatchCase(). It's intentionally undocumented.
215         if (!query) { return results; }
217         var options    = {ignoreCase: !caseSensitive},
218             queryWords = WordBreak.getUniqueWords(query, options);
220         return YArray.filter(results, function (result) {
221             // Convert resultWords array to a hash for fast lookup.
222             var resultWords = YArray.hash(WordBreak.getUniqueWords(result.text,
223                                 options));
225             return YArray.every(queryWords, function (word) {
226                 return YObject.owns(resultWords, word);
227             });
228         });
229     },
231     /**
232     Case-sensitive version of `wordMatch()`.
234     @method wordMatchCase
235     @param {String} query Query to match
236     @param {Array} results Results to filter
237     @return {Array} Filtered results
238     @static
239     **/
240     wordMatchCase: function (query, results) {
241         return Filters.wordMatch(query, results, true);
242     }
246 }, '3.13.0', {"requires": ["array-extras", "text-wordbreak"]});