update manager search filter

fixes #296

* don't ignore 1-letter input if it's the only thing
* normal minimum word length is now 2 letters
* switch to AND combination mode so searching for dark github lists only what you expect
This commit is contained in:
tophf 2017-12-09 17:17:33 +03:00
parent 9486c7f3df
commit ae2efaa4a1

View File

@ -369,13 +369,12 @@ function showFiltersStats() {
function searchStyles({immediately, container}) { function searchStyles({immediately, container}) {
const searchElement = $('#search'); const searchElement = $('#search');
const value = searchElement.value; const value = searchElement.value.trim();
const urlMode = /^\s*url:/i.test(value); const urlMode = /^\s*url:/i.test(value);
const query = urlMode const query = urlMode
? value.replace(/^\s*url:/i, '').trim() ? value.replace(/^\s*url:/i, '')
: value.toLocaleLowerCase(); : value.toLocaleLowerCase();
const queryPrev = searchElement.lastValue || ''; if (query === searchElement.lastValue && !immediately && !container) {
if (query === queryPrev && !immediately && !container) {
return; return;
} }
if (!immediately) { if (!immediately) {
@ -384,20 +383,20 @@ function searchStyles({immediately, container}) {
} }
searchElement.lastValue = query; searchElement.lastValue = query;
const trimmed = query.trim(); const rx = query.startsWith('/') && query.indexOf('/', 1) > 0 &&
const rx = trimmed.startsWith('/') && trimmed.indexOf('/', 1) > 0 &&
tryRegExp(...(value.match(/^\s*\/(.*?)\/([gimsuy]*)\s*$/) || []).slice(1)); tryRegExp(...(value.match(/^\s*\/(.*?)\/([gimsuy]*)\s*$/) || []).slice(1));
const words = rx ? null : const words = rx ? null :
trimmed.startsWith('"') && trimmed.endsWith('"') ? [value.trim().slice(1, -1)] : query.startsWith('"') && query.endsWith('"') ? [value.trim().slice(1, -1)] :
query.split(/\s+/).filter(s => s.length > 2); query.split(/\s+/).filter(s => s.length > 1);
const searchInVisible = !urlMode && queryPrev && query.includes(queryPrev); if (!words.length) {
const entries = container && container.children || container || words.push(query);
(searchInVisible ? $$('.entry:not(.hidden)') : installed.children); }
const entries = container && container.children || container || installed.children;
const siteStyleIds = urlMode && const siteStyleIds = urlMode &&
new Set(BG.filterStyles({matchUrl: query}).map(style => style.id)); new Set(BG.filterStyles({matchUrl: query}).map(style => style.id));
let needsRefilter = false; let needsRefilter = false;
for (const entry of entries) { for (const entry of entries) {
let isMatching = !query; let isMatching = !query || !words.length;
if (!isMatching) { if (!isMatching) {
const style = urlMode ? siteStyleIds.has(entry.styleId) : const style = urlMode ? siteStyleIds.has(entry.styleId) :
BG.cachedStyles.byId.get(entry.styleId) || {}; BG.cachedStyles.byId.get(entry.styleId) || {};
@ -445,10 +444,8 @@ function searchStyles({immediately, container}) {
return rx.test(text); return rx.test(text);
} }
for (let pass = 1; pass <= 2; pass++) { for (let pass = 1; pass <= 2; pass++) {
for (const word of words) { if (words.every(word => text.includes(word))) {
if (text.includes(word)) { return true;
return true;
}
} }
text = text.toLocaleLowerCase(); text = text.toLocaleLowerCase();
} }