API Docs for: 3.18.1
Show:

File: autocomplete/js/autocomplete-filters.js

  1. /**
  2. Provides pre-built result matching filters for AutoComplete.
  3.  
  4. @module autocomplete
  5. @submodule autocomplete-filters
  6. @class AutoCompleteFilters
  7. @static
  8. **/
  9.  
  10. var YArray = Y.Array,
  11. YObject = Y.Object,
  12. WordBreak = Y.Text.WordBreak,
  13.  
  14. Filters = Y.mix(Y.namespace('AutoCompleteFilters'), {
  15. // -- Public Methods -------------------------------------------------------
  16.  
  17. /**
  18. Returns an array of results that contain all of the characters in the query,
  19. in any order (not necessarily consecutive). Case-insensitive.
  20.  
  21. @method charMatch
  22. @param {String} query Query to match
  23. @param {Array} results Results to filter
  24. @return {Array} Filtered results
  25. @static
  26. **/
  27. charMatch: function (query, results, caseSensitive) {
  28. // The caseSensitive parameter is only intended for use by
  29. // charMatchCase(). It's intentionally undocumented.
  30.  
  31. if (!query) { return results; }
  32.  
  33. var queryChars = YArray.unique((caseSensitive ? query :
  34. query.toLowerCase()).split(''));
  35.  
  36. return YArray.filter(results, function (result) {
  37. result = result.text;
  38.  
  39. if (!caseSensitive) {
  40. result = result.toLowerCase();
  41. }
  42.  
  43. return YArray.every(queryChars, function (chr) {
  44. return result.indexOf(chr) !== -1;
  45. });
  46. });
  47. },
  48.  
  49. /**
  50. Case-sensitive version of `charMatch()`.
  51.  
  52. @method charMatchCase
  53. @param {String} query Query to match
  54. @param {Array} results Results to filter
  55. @return {Array} Filtered results
  56. @static
  57. **/
  58. charMatchCase: function (query, results) {
  59. return Filters.charMatch(query, results, true);
  60. },
  61.  
  62. /**
  63. Returns an array of results that contain the complete query as a phrase.
  64. Case-insensitive.
  65.  
  66. @method phraseMatch
  67. @param {String} query Query to match
  68. @param {Array} results Results to filter
  69. @return {Array} Filtered results
  70. @static
  71. **/
  72. phraseMatch: function (query, results, caseSensitive) {
  73. // The caseSensitive parameter is only intended for use by
  74. // phraseMatchCase(). It's intentionally undocumented.
  75.  
  76. if (!query) { return results; }
  77.  
  78. if (!caseSensitive) {
  79. query = query.toLowerCase();
  80. }
  81.  
  82. return YArray.filter(results, function (result) {
  83. return (caseSensitive ? result.text : result.text.toLowerCase()).indexOf(query) !== -1;
  84. });
  85. },
  86.  
  87. /**
  88. Case-sensitive version of `phraseMatch()`.
  89.  
  90. @method phraseMatchCase
  91. @param {String} query Query to match
  92. @param {Array} results Results to filter
  93. @return {Array} Filtered results
  94. @static
  95. **/
  96. phraseMatchCase: function (query, results) {
  97. return Filters.phraseMatch(query, results, true);
  98. },
  99.  
  100. /**
  101. Returns an array of results that start with the complete query as a phrase.
  102. Case-insensitive.
  103.  
  104. @method startsWith
  105. @param {String} query Query to match
  106. @param {Array} results Results to filter
  107. @return {Array} Filtered results
  108. @static
  109. **/
  110. startsWith: function (query, results, caseSensitive) {
  111. // The caseSensitive parameter is only intended for use by
  112. // startsWithCase(). It's intentionally undocumented.
  113.  
  114. if (!query) { return results; }
  115.  
  116. if (!caseSensitive) {
  117. query = query.toLowerCase();
  118. }
  119.  
  120. return YArray.filter(results, function (result) {
  121. return (caseSensitive ? result.text : result.text.toLowerCase()).indexOf(query) === 0;
  122. });
  123. },
  124.  
  125. /**
  126. Case-sensitive version of `startsWith()`.
  127.  
  128. @method startsWithCase
  129. @param {String} query Query to match
  130. @param {Array} results Results to filter
  131. @return {Array} Filtered results
  132. @static
  133. **/
  134. startsWithCase: function (query, results) {
  135. return Filters.startsWith(query, results, true);
  136. },
  137.  
  138. /**
  139. Returns an array of results in which all the words of the query match either
  140. whole words or parts of words in the result. Non-word characters like
  141. whitespace and certain punctuation are ignored. Case-insensitive.
  142.  
  143. This is basically a combination of `wordMatch()` (by ignoring whitespace and
  144. word order) and `phraseMatch()` (by allowing partial matching instead of
  145. requiring the entire word to match).
  146.  
  147. Example use case: Trying to find personal names independently of name order
  148. (Western or Eastern order) and supporting immediate feedback by allowing
  149. partial occurences. So queries like "J. Doe", "Doe, John", and "J. D." would
  150. all match "John Doe".
  151.  
  152. @method subWordMatch
  153. @param {String} query Query to match
  154. @param {Array} results Results to filter
  155. @return {Array} Filtered results
  156. @static
  157. **/
  158. subWordMatch: function (query, results, caseSensitive) {
  159. // The caseSensitive parameter is only intended for use by
  160. // subWordMatchCase(). It's intentionally undocumented.
  161.  
  162. if (!query) { return results; }
  163.  
  164. var queryWords = WordBreak.getUniqueWords(query, {
  165. ignoreCase: !caseSensitive
  166. });
  167.  
  168. return YArray.filter(results, function (result) {
  169. var resultText = caseSensitive ? result.text :
  170. result.text.toLowerCase();
  171.  
  172. return YArray.every(queryWords, function (queryWord) {
  173. return resultText.indexOf(queryWord) !== -1;
  174. });
  175. });
  176. },
  177.  
  178. /**
  179. Case-sensitive version of `subWordMatch()`.
  180.  
  181. @method subWordMatchCase
  182. @param {String} query Query to match
  183. @param {Array} results Results to filter
  184. @return {Array} Filtered results
  185. @static
  186. **/
  187. subWordMatchCase: function (query, results) {
  188. return Filters.subWordMatch(query, results, true);
  189. },
  190.  
  191. /**
  192. Returns an array of results that contain all of the words in the query, in
  193. any order. Non-word characters like whitespace and certain punctuation are
  194. ignored. Case-insensitive.
  195.  
  196. @method wordMatch
  197. @param {String} query Query to match
  198. @param {Array} results Results to filter
  199. @return {Array} Filtered results
  200. @static
  201. **/
  202. wordMatch: function (query, results, caseSensitive) {
  203. // The caseSensitive parameter is only intended for use by
  204. // wordMatchCase(). It's intentionally undocumented.
  205.  
  206. if (!query) { return results; }
  207.  
  208. var options = {ignoreCase: !caseSensitive},
  209. queryWords = WordBreak.getUniqueWords(query, options);
  210.  
  211. return YArray.filter(results, function (result) {
  212. // Convert resultWords array to a hash for fast lookup.
  213. var resultWords = YArray.hash(WordBreak.getUniqueWords(result.text,
  214. options));
  215.  
  216. return YArray.every(queryWords, function (word) {
  217. return YObject.owns(resultWords, word);
  218. });
  219. });
  220. },
  221.  
  222. /**
  223. Case-sensitive version of `wordMatch()`.
  224.  
  225. @method wordMatchCase
  226. @param {String} query Query to match
  227. @param {Array} results Results to filter
  228. @return {Array} Filtered results
  229. @static
  230. **/
  231. wordMatchCase: function (query, results) {
  232. return Filters.wordMatch(query, results, true);
  233. }
  234. });
  235.