~rharding/loggerhead/yui3.7.1

« back to all changes in this revision

Viewing changes to loggerhead/static/javascript/yui/build/autocomplete-filters-accentfold/autocomplete-filters-accentfold.js

  • Committer: Rick Harding
  • Date: 2012-11-07 15:51:42 UTC
  • Revision ID: rick.harding@canonical.com-20121107155142-56lxp77hopixc9xf
Fix the collapsable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
YUI 3.7.1 (build 5627)
 
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-accentfold', function (Y, NAME) {
 
8
 
 
9
/**
 
10
Provides pre-built accent-folding result matching filters for AutoComplete.
 
11
 
 
12
These filters are similar to the ones provided by the `autocomplete-filters`
 
13
module, but use accent-aware comparisons. For example, "resume" and "résumé"
 
14
will be considered equal when using the accent-folding filters.
 
15
 
 
16
@module autocomplete
 
17
@submodule autocomplete-filters-accentfold
 
18
**/
 
19
 
 
20
/**
 
21
@class AutoCompleteFilters
 
22
@static
 
23
**/
 
24
 
 
25
var AccentFold = Y.Text.AccentFold,
 
26
    WordBreak  = Y.Text.WordBreak,
 
27
    YArray     = Y.Array,
 
28
    YObject    = Y.Object;
 
29
 
 
30
Y.mix(Y.namespace('AutoCompleteFilters'), {
 
31
    /**
 
32
    Accent folding version of `charMatch()`.
 
33
 
 
34
    @method charMatchFold
 
35
    @param {String} query Query to match
 
36
    @param {Array} results Results to filter
 
37
    @return {Array} Filtered results
 
38
    @static
 
39
    **/
 
40
    charMatchFold: function (query, results) {
 
41
        if (!query) { return results; }
 
42
 
 
43
        var queryChars = YArray.unique(AccentFold.fold(query).split(''));
 
44
 
 
45
        return YArray.filter(results, function (result) {
 
46
            var text = AccentFold.fold(result.text);
 
47
 
 
48
            return YArray.every(queryChars, function (chr) {
 
49
                return text.indexOf(chr) !== -1;
 
50
            });
 
51
        });
 
52
    },
 
53
 
 
54
    /**
 
55
    Accent folding version of `phraseMatch()`.
 
56
 
 
57
    @method phraseMatchFold
 
58
    @param {String} query Query to match
 
59
    @param {Array} results Results to filter
 
60
    @return {Array} Filtered results
 
61
    @static
 
62
    **/
 
63
    phraseMatchFold: function (query, results) {
 
64
        if (!query) { return results; }
 
65
 
 
66
        query = AccentFold.fold(query);
 
67
 
 
68
        return YArray.filter(results, function (result) {
 
69
            return AccentFold.fold(result.text).indexOf(query) !== -1;
 
70
        });
 
71
    },
 
72
 
 
73
    /**
 
74
    Accent folding version of `startsWith()`.
 
75
 
 
76
    @method startsWithFold
 
77
    @param {String} query Query to match
 
78
    @param {Array} results Results to filter
 
79
    @return {Array} Filtered results
 
80
    @static
 
81
    **/
 
82
    startsWithFold: function (query, results) {
 
83
        if (!query) { return results; }
 
84
 
 
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 `subWordMatch()`.
 
94
 
 
95
    @method subWordMatchFold
 
96
    @param {String} query Query to match
 
97
    @param {Array} results Results to filter
 
98
    @return {Array} Filtered results
 
99
    @static
 
100
    **/
 
101
    subWordMatchFold: function (query, results) {
 
102
        if (!query) { return results; }
 
103
 
 
104
        var queryWords = WordBreak.getUniqueWords(AccentFold.fold(query));
 
105
 
 
106
        return YArray.filter(results, function (result) {
 
107
            var resultText = AccentFold.fold(result.text);
 
108
 
 
109
            return YArray.every(queryWords, function (queryWord) {
 
110
                return resultText.indexOf(queryWord) !== -1;
 
111
            });
 
112
        });
 
113
    },
 
114
 
 
115
    /**
 
116
    Accent folding version of `wordMatch()`.
 
117
 
 
118
    @method wordMatchFold
 
119
    @param {String} query Query to match
 
120
    @param {Array} results Results to filter
 
121
    @return {Array} Filtered results
 
122
    @static
 
123
    **/
 
124
    wordMatchFold: function (query, results) {
 
125
        if (!query) { return results; }
 
126
 
 
127
        var queryWords = WordBreak.getUniqueWords(AccentFold.fold(query));
 
128
 
 
129
        return YArray.filter(results, function (result) {
 
130
            // Convert resultWords array to a hash for fast lookup.
 
131
            var resultWords = YArray.hash(WordBreak.getUniqueWords(
 
132
                    AccentFold.fold(result.text)));
 
133
 
 
134
            return YArray.every(queryWords, function (word) {
 
135
                return YObject.owns(resultWords, word);
 
136
            });
 
137
        });
 
138
    }
 
139
});
 
140
 
 
141
 
 
142
}, '3.7.1', {"requires": ["array-extras", "text-accentfold", "text-wordbreak"]});