Clean and tests for filters on open command

This commit is contained in:
Shin'ya Ueoka 2018-07-21 15:59:30 +09:00
parent a05df9d7b6
commit 0652131de8
4 changed files with 199 additions and 79 deletions

View file

@ -3,6 +3,9 @@ import CompletionGroup from '../domains/completion-group';
import Completions from '../domains/completions';
import CompletionRepository from '../repositories/completions';
import CommandDocs from 'background/shared/commands/docs';
import * as filters from './filters';
const COMPLETION_ITEM_LIMIT = 10;
export default class CompletionsInteractor {
constructor() {
@ -28,9 +31,18 @@ export default class CompletionsInteractor {
}
async queryOpen(name, keywords) {
// TODO get search engines from settings
let groups = [];
let histories = await this.completionRepository.queryHistories(keywords);
if (histories.length > 0) {
histories = [histories]
.map(filters.filterBlankTitle)
.map(filters.filterHttp)
.map(filters.filterByTailingSlash)
.map(pages => filters.filterByPathname(pages, COMPLETION_ITEM_LIMIT))
.map(pages => filters.filterByOrigin(pages, COMPLETION_ITEM_LIMIT))[0]
.sort((x, y) => x.visitCount < y.visitCount)
.slice(0, COMPLETION_ITEM_LIMIT);
let items = histories.map(page => new CompletionItem({
caption: page.title,
content: name + ' ' + page.url,

View file

@ -0,0 +1,72 @@
const filterHttp = (items) => {
let httpsHosts = items.map(x => new URL(x.url))
.filter(x => x.protocol === 'https:')
.map(x => x.host);
httpsHosts = new Set(httpsHosts);
return items.filter((item) => {
let url = new URL(item.url);
return url.protocol === 'https:' || !httpsHosts.has(url.host);
});
};
const filterBlankTitle = (items) => {
return items.filter(item => item.title && item.title !== '');
};
const filterByTailingSlash = (items) => {
let urls = items.map(item => new URL(item.url));
let simplePaths = urls
.filter(url => url.hash === '' && url.search === '')
.map(url => url.origin + url.pathname);
simplePaths = new Set(simplePaths);
return items.filter((item) => {
let url = new URL(item.url);
if (url.hash !== '' || url.search !== '' ||
url.pathname.slice(-1) !== '/') {
return true;
}
return !simplePaths.has(url.origin + url.pathname.slice(0, -1));
});
};
const filterByPathname = (items, min) => {
let hash = {};
for (let item of items) {
let url = new URL(item.url);
let pathname = url.origin + url.pathname;
if (!hash[pathname]) {
hash[pathname] = item;
} else if (hash[pathname].url.length > item.url.length) {
hash[pathname] = item;
}
}
let filtered = Object.values(hash);
if (filtered.length < min) {
return items;
}
return filtered;
};
const filterByOrigin = (items, min) => {
let hash = {};
for (let item of items) {
let origin = new URL(item.url).origin;
if (!hash[origin]) {
hash[origin] = item;
} else if (hash[origin].url.length > item.url.length) {
hash[origin] = item;
}
}
let filtered = Object.values(hash);
if (filtered.length < min) {
return items;
}
return filtered;
};
export {
filterHttp, filterBlankTitle, filterByTailingSlash,
filterByPathname, filterByOrigin
};