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