API Docs for: 3.10.3
Show:

File: text/js/text-accentfold.js

  1. /**
  2. * Text utilities.
  3. *
  4. * @module text
  5. * @since 3.3.0
  6. */
  7.  
  8. /**
  9. * Provides a basic accent folding implementation that converts common accented
  10. * letters (like "á") to their non-accented forms (like "a").
  11. *
  12. * @module text
  13. * @submodule text-accentfold
  14. */
  15.  
  16. /**
  17. * <p>
  18. * Provides a basic accent folding implementation that converts common accented
  19. * letters (like "á") to their non-accented forms (like "a").
  20. * </p>
  21. *
  22. * <p>
  23. * This implementation is not comprehensive, and should only be used as a last
  24. * resort when accent folding can't be done on the server. A comprehensive
  25. * accent folding implementation would require much more character data to be
  26. * sent to the browser, resulting in a significant performance penalty. This
  27. * implementation strives for a compromise between usefulness and performance.
  28. * </p>
  29. *
  30. * <p>
  31. * Accent folding is a destructive operation that can't be reversed, and may
  32. * change or destroy the actual meaning of the text depending on the language.
  33. * It should not be used on strings that will later be displayed to a user,
  34. * unless this is done with the understanding that linguistic meaning may be
  35. * lost and that you may in fact confuse or insult the user by doing so.
  36. * </p>
  37. *
  38. * <p>
  39. * When used for matching, accent folding is likely to produce erroneous matches
  40. * for languages in which characters with diacritics are considered different
  41. * from their base characters, or where correct folding would map to other
  42. * character sequences than just stripped characters. For example, in German
  43. * "ü" is a character that's clearly different from "u" and should match "ue"
  44. * instead. The word "betrügen" means "to defraud", while "betrugen" is the past
  45. * tense of "to behave". The name "Müller" is expected to match "Mueller", but
  46. * not "Muller". On the other hand, accent folding falls short for languages
  47. * where different base characters are expected to match. In Japanese, for
  48. * example, hiragana and katakana characters with the same pronunciation ("あ"
  49. * and "ア") are commonly treated as equivalent for lookups, but accent folding
  50. * treats them as different.
  51. * </p>
  52. *
  53. * @class Text.AccentFold
  54. * @static
  55. */
  56.  
  57. var YArray = Y.Array,
  58. Text = Y.Text,
  59. FoldData = Text.Data.AccentFold,
  60.  
  61. AccentFold = {
  62. // -- Public Static Methods ------------------------------------------------
  63.  
  64. /**
  65. * Returns <code>true</code> if the specified string contains one or more
  66. * characters that can be folded, <code>false</code> otherwise.
  67. *
  68. * @method canFold
  69. * @param {String} string String to test.
  70. * @return {Boolean}
  71. * @static
  72. */
  73. canFold: function (string) {
  74. var letter;
  75.  
  76. for (letter in FoldData) {
  77. if (FoldData.hasOwnProperty(letter) &&
  78. string.search(FoldData[letter]) !== -1) {
  79. return true;
  80. }
  81. }
  82.  
  83. return false;
  84. },
  85.  
  86. /**
  87. * Compares the accent-folded versions of two strings and returns
  88. * <code>true</code> if they're the same, <code>false</code> otherwise. If
  89. * a custom comparison function is supplied, the accent-folded strings will
  90. * be passed to that function for comparison.
  91. *
  92. * @method compare
  93. * @param {String} a First string to compare.
  94. * @param {String} b Second string to compare.
  95. * @param {Function} func (optional) Custom comparison function. Should
  96. * return a truthy or falsy value.
  97. * @return {Boolean} Results of the comparison.
  98. * @static
  99. */
  100. compare: function (a, b, func) {
  101. var aFolded = AccentFold.fold(a),
  102. bFolded = AccentFold.fold(b);
  103.  
  104. return func ? !!func(aFolded, bFolded) : aFolded === bFolded;
  105. },
  106.  
  107. /**
  108. * <p>
  109. * Returns a copy of <em>haystack</em> containing only the strings for which
  110. * the supplied function returns <code>true</code>.
  111. * </p>
  112. *
  113. * <p>
  114. * While comparisons will be made using accent-folded strings, the returned
  115. * array of matches will contain the original strings that were passed in.
  116. * </p>
  117. *
  118. * @method filter
  119. * @param {Array} haystack Array of strings to filter.
  120. * @param {Function} func Comparison function. Will receive an accent-folded
  121. * haystack string as an argument, and should return a truthy or falsy
  122. * value.
  123. * @return {Array} Filtered copy of <em>haystack</em>.
  124. * @static
  125. */
  126. filter: function (haystack, func) {
  127. return YArray.filter(haystack, function (item) {
  128. return func(AccentFold.fold(item));
  129. });
  130. },
  131.  
  132. /**
  133. * Accent-folds the specified string or array of strings and returns a copy
  134. * in which common accented letters have been converted to their closest
  135. * non-accented, lowercase forms.
  136. *
  137. * @method fold
  138. * @param {String|Array} input String or array of strings to be folded.
  139. * @return {String|Array} Folded string or array of strings.
  140. * @static
  141. */
  142. fold: function (input) {
  143. if (Y.Lang.isArray(input)) {
  144. return YArray.map(input, AccentFold.fold);
  145. }
  146.  
  147. input = input.toLowerCase();
  148.  
  149. Y.Object.each(FoldData, function (regex, letter) {
  150. input = input.replace(regex, letter);
  151. });
  152.  
  153. return input;
  154. }
  155. };
  156.  
  157. Text.AccentFold = AccentFold;
  158.