]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/autocomplete/autocomplete-filters-accentfold.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / autocomplete / autocomplete-filters-accentfold.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 YUI.add('autocomplete-filters-accentfold', function(Y) {
9
10 /**
11  * <p>
12  * Provides pre-built accent-folding result matching filters for AutoComplete.
13  * </p>
14  *
15  * <p>
16  * These filters are similar to the ones provided by the
17  * <code>autocomplete-filters</code> module, but use accent-aware comparisons.
18  * For example, "resume" and "résumé" will be considered equal when using the
19  * accent-folding filters.
20  * </p>
21  *
22  * @module autocomplete
23  * @submodule autocomplete-filters-accentfold
24  */
25
26 /**
27  * @class AutoCompleteFilters
28  * @static
29  */
30
31 var AccentFold = Y.Text.AccentFold,
32     WordBreak  = Y.Text.WordBreak,
33     YArray     = Y.Array,
34     YObject    = Y.Object;
35
36 Y.mix(Y.namespace('AutoCompleteFilters'), {
37     /**
38      * Accent folding version of <code>charMatch()</code>.
39      *
40      * @method charMatchFold
41      * @param {String} query Query to match
42      * @param {Array} results Results to filter
43      * @return {Array} Filtered results
44      * @static
45      */
46     charMatchFold: function (query, results) {
47         var queryChars = YArray.unique(AccentFold.fold(query).split(''));
48
49         return YArray.filter(results, function (result) {
50             var text = AccentFold.fold(result.text);
51
52             return YArray.every(queryChars, function (chr) {
53                 return text.indexOf(chr) !== -1;
54             });
55         });
56     },
57
58     /**
59      * Accent folding version of <code>phraseMatch()</code>.
60      *
61      * @method phraseMatchFold
62      * @param {String} query Query to match
63      * @param {Array} results Results to filter
64      * @return {Array} Filtered results
65      * @static
66      */
67     phraseMatchFold: function (query, results) {
68         query = AccentFold.fold(query);
69
70         return YArray.filter(results, function (result) {
71             return AccentFold.fold(result.text).indexOf(query) !== -1;
72         });
73     },
74
75     /**
76      * Accent folding version of <code>startsWithFold()</code>.
77      *
78      * @method startsWithFold
79      * @param {String} query Query to match
80      * @param {Array} results Results to filter
81      * @return {Array} Filtered results
82      * @static
83      */
84     startsWithFold: function (query, results) {
85         query = AccentFold.fold(query);
86
87         return YArray.filter(results, function (result) {
88             return AccentFold.fold(result.text).indexOf(query) === 0;
89         });
90     },
91
92     /**
93      * Accent folding version of <code>wordMatchFold()</code>.
94      *
95      * @method wordMatchFold
96      * @param {String} query Query to match
97      * @param {Array} results Results to filter
98      * @return {Array} Filtered results
99      * @static
100      */
101     wordMatchFold: function (query, results) {
102         var queryWords = WordBreak.getUniqueWords(AccentFold.fold(query));
103
104         return YArray.filter(results, function (result) {
105             // Convert resultWords array to a hash for fast lookup.
106             var resultWords = YArray.hash(WordBreak.getUniqueWords(
107                     AccentFold.fold(result.text)));
108
109             return YArray.every(queryWords, function (word) {
110                 return YObject.owns(resultWords, word);
111             });
112         });
113     }
114 });
115
116
117 }, '3.3.0' ,{requires:['array-extras', 'text-accentfold', 'text-wordbreak']});