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/
8 YUI.add('text-accentfold', function (Y, NAME) {
18 * Provides a basic accent folding implementation that converts common accented
19 * letters (like "á") to their non-accented forms (like "a").
22 * @submodule text-accentfold
27 * Provides a basic accent folding implementation that converts common accented
28 * letters (like "á") to their non-accented forms (like "a").
32 * This implementation is not comprehensive, and should only be used as a last
33 * resort when accent folding can't be done on the server. A comprehensive
34 * accent folding implementation would require much more character data to be
35 * sent to the browser, resulting in a significant performance penalty. This
36 * implementation strives for a compromise between usefulness and performance.
40 * Accent folding is a destructive operation that can't be reversed, and may
41 * change or destroy the actual meaning of the text depending on the language.
42 * It should not be used on strings that will later be displayed to a user,
43 * unless this is done with the understanding that linguistic meaning may be
44 * lost and that you may in fact confuse or insult the user by doing so.
48 * When used for matching, accent folding is likely to produce erroneous matches
49 * for languages in which characters with diacritics are considered different
50 * from their base characters, or where correct folding would map to other
51 * character sequences than just stripped characters. For example, in German
52 * "ü" is a character that's clearly different from "u" and should match "ue"
53 * instead. The word "betrügen" means "to defraud", while "betrugen" is the past
54 * tense of "to behave". The name "Müller" is expected to match "Mueller", but
55 * not "Muller". On the other hand, accent folding falls short for languages
56 * where different base characters are expected to match. In Japanese, for
57 * example, hiragana and katakana characters with the same pronunciation ("あ"
58 * and "ア") are commonly treated as equivalent for lookups, but accent folding
59 * treats them as different.
62 * @class Text.AccentFold
68 FoldData = Text.Data.AccentFold,
71 // -- Public Static Methods ------------------------------------------------
74 * Returns <code>true</code> if the specified string contains one or more
75 * characters that can be folded, <code>false</code> otherwise.
78 * @param {String} string String to test.
82 canFold: function (string) {
85 for (letter in FoldData) {
86 if (FoldData.hasOwnProperty(letter) &&
87 string.search(FoldData[letter]) !== -1) {
96 * Compares the accent-folded versions of two strings and returns
97 * <code>true</code> if they're the same, <code>false</code> otherwise. If
98 * a custom comparison function is supplied, the accent-folded strings will
99 * be passed to that function for comparison.
102 * @param {String} a First string to compare.
103 * @param {String} b Second string to compare.
104 * @param {Function} func (optional) Custom comparison function. Should
105 * return a truthy or falsy value.
106 * @return {Boolean} Results of the comparison.
109 compare: function (a, b, func) {
110 var aFolded = AccentFold.fold(a),
111 bFolded = AccentFold.fold(b);
113 return func ? !!func(aFolded, bFolded) : aFolded === bFolded;
118 * Returns a copy of <em>haystack</em> containing only the strings for which
119 * the supplied function returns <code>true</code>.
123 * While comparisons will be made using accent-folded strings, the returned
124 * array of matches will contain the original strings that were passed in.
128 * @param {Array} haystack Array of strings to filter.
129 * @param {Function} func Comparison function. Will receive an accent-folded
130 * haystack string as an argument, and should return a truthy or falsy
132 * @return {Array} Filtered copy of <em>haystack</em>.
135 filter: function (haystack, func) {
136 return YArray.filter(haystack, function (item) {
137 return func(AccentFold.fold(item));
142 * Accent-folds the specified string or array of strings and returns a copy
143 * in which common accented letters have been converted to their closest
144 * non-accented, lowercase forms.
147 * @param {String|Array} input String or array of strings to be folded.
148 * @return {String|Array} Folded string or array of strings.
151 fold: function (input) {
152 if (Y.Lang.isArray(input)) {
153 return YArray.map(input, AccentFold.fold);
156 input = input.toLowerCase();
158 Y.Object.each(FoldData, function (regex, letter) {
159 input = input.replace(regex, letter);
166 Text.AccentFold = AccentFold;
169 }, '3.13.0', {"requires": ["array-extras", "text-data-accentfold"]});