|
1 | 1 | //------------------------------------- Initialisation --------------------------------------//
|
2 | 2 | "use strict";
|
3 | 3 |
|
| 4 | +const FuzzySort = require("fuzzysort"); |
4 | 5 | const TldJS = require("tldjs");
|
| 6 | +const sha1 = require("sha1"); |
5 | 7 |
|
6 | 8 | module.exports = {
|
7 |
| - pathToDomain |
| 9 | + pathToDomain, |
| 10 | + prepareLogins, |
| 11 | + filterSortLogins |
8 | 12 | };
|
9 | 13 |
|
10 | 14 | //----------------------------------- Function definitions ----------------------------------//
|
@@ -37,3 +41,237 @@ function pathToDomain(path, currentHost) {
|
37 | 41 |
|
38 | 42 | return null;
|
39 | 43 | }
|
| 44 | + |
| 45 | +/** |
| 46 | + * Prepare list of logins based on provided files |
| 47 | + * |
| 48 | + * @since 3.1.0 |
| 49 | + * |
| 50 | + * @param string array List of password files |
| 51 | + * @param string object Settings object |
| 52 | + * @return array List of logins |
| 53 | + */ |
| 54 | +function prepareLogins(files, settings) { |
| 55 | + const logins = []; |
| 56 | + let index = 0; |
| 57 | + |
| 58 | + for (let storeId in files) { |
| 59 | + for (let key in files[storeId]) { |
| 60 | + // set login fields |
| 61 | + const login = { |
| 62 | + index: index++, |
| 63 | + store: settings.stores[storeId], |
| 64 | + login: files[storeId][key].replace(/\.gpg$/i, ""), |
| 65 | + allowFill: true |
| 66 | + }; |
| 67 | + login.domain = pathToDomain(storeId + "/" + login.login, settings.host); |
| 68 | + login.inCurrentDomain = |
| 69 | + settings.host == login.domain || settings.host.endsWith("." + login.domain); |
| 70 | + login.recent = |
| 71 | + settings.recent[sha1(settings.host + sha1(login.store.id + sha1(login.login)))]; |
| 72 | + if (!login.recent) { |
| 73 | + login.recent = { |
| 74 | + when: 0, |
| 75 | + count: 0 |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + logins.push(login); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return logins; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Filter and sort logins |
| 88 | + * |
| 89 | + * @since 3.1.0 |
| 90 | + * |
| 91 | + * @param string array List of logins |
| 92 | + * @param string object Settings object |
| 93 | + * @return array Filtered and sorted list of logins |
| 94 | + */ |
| 95 | +function filterSortLogins(logins, searchQuery, currentDomainOnly) { |
| 96 | + var fuzzyFirstWord = searchQuery.substr(0, 1) !== " "; |
| 97 | + searchQuery = searchQuery.trim(); |
| 98 | + |
| 99 | + // get candidate list |
| 100 | + var candidates = logins.map(candidate => { |
| 101 | + let lastSlashIndex = candidate.login.lastIndexOf("/") + 1; |
| 102 | + return Object.assign(candidate, { |
| 103 | + path: candidate.login.substr(0, lastSlashIndex), |
| 104 | + display: candidate.login.substr(lastSlashIndex) |
| 105 | + }); |
| 106 | + }); |
| 107 | + |
| 108 | + var mostRecent = null; |
| 109 | + if (currentDomainOnly) { |
| 110 | + var recent = candidates.filter(function(login) { |
| 111 | + if (login.recent.count > 0) { |
| 112 | + // find most recently used login |
| 113 | + if (!mostRecent || login.recent.when > mostRecent.recent.when) { |
| 114 | + mostRecent = login; |
| 115 | + } |
| 116 | + return true; |
| 117 | + } |
| 118 | + return false; |
| 119 | + }); |
| 120 | + var remainingInCurrentDomain = candidates.filter( |
| 121 | + login => login.inCurrentDomain && !login.recent.count |
| 122 | + ); |
| 123 | + candidates = recent.concat(remainingInCurrentDomain); |
| 124 | + } |
| 125 | + |
| 126 | + candidates.sort((a, b) => { |
| 127 | + // show most recent first |
| 128 | + if (a === mostRecent) { |
| 129 | + return -1; |
| 130 | + } |
| 131 | + if (b === mostRecent) { |
| 132 | + return 1; |
| 133 | + } |
| 134 | + |
| 135 | + // sort by frequency |
| 136 | + var countDiff = b.recent.count - a.recent.count; |
| 137 | + if (countDiff) { |
| 138 | + return countDiff; |
| 139 | + } |
| 140 | + |
| 141 | + // sort by specificity, only if filtering for one domain |
| 142 | + if (currentDomainOnly) { |
| 143 | + var domainLevelsDiff = |
| 144 | + (b.login.match(/\./g) || []).length - (a.login.match(/\./g) || []).length; |
| 145 | + if (domainLevelsDiff) { |
| 146 | + return domainLevelsDiff; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + // sort alphabetically |
| 151 | + return a.login.localeCompare(b.login); |
| 152 | + }); |
| 153 | + |
| 154 | + if (searchQuery.length) { |
| 155 | + let filter = searchQuery.split(/\s+/); |
| 156 | + let fuzzyFilter = fuzzyFirstWord ? filter[0] : ""; |
| 157 | + let substringFilters = filter.slice(fuzzyFirstWord ? 1 : 0).map(w => w.toLowerCase()); |
| 158 | + |
| 159 | + // First reduce the list by running the substring search |
| 160 | + substringFilters.forEach(function(word) { |
| 161 | + candidates = candidates.filter(c => c.login.toLowerCase().indexOf(word) >= 0); |
| 162 | + }); |
| 163 | + |
| 164 | + // Then run the fuzzy filter |
| 165 | + let fuzzyResults = {}; |
| 166 | + if (fuzzyFilter) { |
| 167 | + candidates = FuzzySort.go(fuzzyFilter, candidates, { |
| 168 | + keys: ["login", "store.name"], |
| 169 | + allowTypo: false |
| 170 | + }).map(result => { |
| 171 | + fuzzyResults[result.obj.login] = result; |
| 172 | + return result.obj; |
| 173 | + }); |
| 174 | + } |
| 175 | + |
| 176 | + // Finally highlight all matches |
| 177 | + candidates = candidates.map(c => highlightMatches(c, fuzzyResults, substringFilters)); |
| 178 | + } |
| 179 | + |
| 180 | + // Prefix root entries with slash to let them have some visible path |
| 181 | + candidates.forEach(c => { |
| 182 | + c.path = c.path || "/"; |
| 183 | + }); |
| 184 | + |
| 185 | + return candidates; |
| 186 | +} |
| 187 | + |
| 188 | +//----------------------------------- Private functions ----------------------------------// |
| 189 | + |
| 190 | +/** |
| 191 | + * Highlight filter matches |
| 192 | + * |
| 193 | + * @since 3.0.0 |
| 194 | + * |
| 195 | + * @param object entry password entry |
| 196 | + * @param object fuzzyResults positions of fuzzy filter matches |
| 197 | + * @param array substringFilters list of substring filters applied |
| 198 | + * @return object entry with highlighted matches |
| 199 | + */ |
| 200 | +function highlightMatches(entry, fuzzyResults, substringFilters) { |
| 201 | + // Add all positions of the fuzzy search to the array |
| 202 | + let matches = (fuzzyResults[entry.login] && fuzzyResults[entry.login][0] |
| 203 | + ? fuzzyResults[entry.login][0].indexes |
| 204 | + : [] |
| 205 | + ).slice(); |
| 206 | + |
| 207 | + // Add all positions of substring searches to the array |
| 208 | + let login = entry.login.toLowerCase(); |
| 209 | + for (let word of substringFilters) { |
| 210 | + let startIndex = login.indexOf(word); |
| 211 | + for (let i = 0; i < word.length; i++) { |
| 212 | + matches.push(startIndex + i); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + // Prepare the final array of matches before |
| 217 | + matches = sortUnique(matches, (a, b) => a - b); |
| 218 | + |
| 219 | + const OPEN = "<em>"; |
| 220 | + const CLOSE = "</em>"; |
| 221 | + let highlighted = ""; |
| 222 | + var matchesIndex = 0; |
| 223 | + var opened = false; |
| 224 | + for (var i = 0; i < entry.login.length; ++i) { |
| 225 | + var char = entry.login[i]; |
| 226 | + |
| 227 | + if (i == entry.path.length) { |
| 228 | + if (opened) { |
| 229 | + highlighted += CLOSE; |
| 230 | + } |
| 231 | + var path = highlighted; |
| 232 | + highlighted = ""; |
| 233 | + if (opened) { |
| 234 | + highlighted += OPEN; |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + if (matches[matchesIndex] === i) { |
| 239 | + matchesIndex++; |
| 240 | + if (!opened) { |
| 241 | + opened = true; |
| 242 | + highlighted += OPEN; |
| 243 | + } |
| 244 | + } else { |
| 245 | + if (opened) { |
| 246 | + opened = false; |
| 247 | + highlighted += CLOSE; |
| 248 | + } |
| 249 | + } |
| 250 | + highlighted += char; |
| 251 | + } |
| 252 | + if (opened) { |
| 253 | + opened = false; |
| 254 | + highlighted += CLOSE; |
| 255 | + } |
| 256 | + let display = highlighted; |
| 257 | + |
| 258 | + return Object.assign(entry, { |
| 259 | + path: path, |
| 260 | + display: display |
| 261 | + }); |
| 262 | +} |
| 263 | + |
| 264 | +/** |
| 265 | + * Sort and remove duplicates |
| 266 | + * |
| 267 | + * @since 3.0.0 |
| 268 | + * |
| 269 | + * @param array array items to sort |
| 270 | + * @param function comparator sort comparator |
| 271 | + * @return array sorted items without duplicates |
| 272 | + */ |
| 273 | +function sortUnique(array, comparator) { |
| 274 | + return array |
| 275 | + .sort(comparator) |
| 276 | + .filter((elem, index, arr) => index == !arr.length || arr[index - 1] != elem); |
| 277 | +} |
0 commit comments