diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2018-07-21 15:59:30 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2018-07-21 16:26:54 +0900 |
commit | 0652131de885df3ebb7c9c0e762fd1604486a892 (patch) | |
tree | 72a3776915d8d112ddfa25d2ad96a0d5c96c6e5f /src/background/repositories/completions.js | |
parent | a05df9d7b643b472208a2137a3329ba5a120f954 (diff) |
Clean and tests for filters on open command
Diffstat (limited to 'src/background/repositories/completions.js')
-rw-r--r-- | src/background/repositories/completions.js | 81 |
1 files changed, 2 insertions, 79 deletions
diff --git a/src/background/repositories/completions.js b/src/background/repositories/completions.js index c4e956e..1318d36 100644 --- a/src/background/repositories/completions.js +++ b/src/background/repositories/completions.js @@ -12,88 +12,11 @@ export default class CompletionsRepository { }); } - // eslint-disable-next-line complexity, max-lines-per-function - async queryHistories(keywords) { - const filterHttp = (items) => { - const httpsHosts = items - .filter(item => item[1].protocol === 'https:') - .map(item => item[1].host); - const httpsHostSet = new Set(httpsHosts); - return items.filter( - item => !(item[1].protocol === 'http:' && - httpsHostSet.has(item[1].host)) - ); - }; - - const filterEmptyTitle = (items) => { - return items.filter(item => item[0].title && item[0].title !== ''); - }; - - const filterClosedPath = (items) => { - const allSimplePaths = items - .filter(item => item[1].hash === '' && item[1].search === '') - .map(item => item[1].origin + item[1].pathname); - const allSimplePathSet = new Set(allSimplePaths); - return items.filter( - item => !(item[1].hash === '' && item[1].search === '' && - (/\/$/).test(item[1].pathname) && - allSimplePathSet.has( - (item[1].origin + item[1].pathname).replace(/\/$/, '') - ) - ) - ); - }; - - const reduceByPathname = (items, min) => { - let hash = {}; - for (let item of items) { - let pathname = item[1].origin + item[1].pathname; - if (!hash[pathname]) { - hash[pathname] = item; - } else if (hash[pathname][1].href.length > item[1].href.length) { - hash[pathname] = item; - } - } - let filtered = Object.values(hash); - if (filtered.length < min) { - return items; - } - return filtered; - }; - - const reduceByOrigin = (items, min) => { - let hash = {}; - for (let item of items) { - let origin = item[1].origin; - if (!hash[origin]) { - hash[origin] = item; - } else if (hash[origin][1].href.length > item[1].href.length) { - hash[origin] = item; - } - } - let filtered = Object.values(hash); - if (filtered.length < min) { - return items; - } - return filtered; - }; - - - let historyItems = await browser.history.search({ + queryHistories(keywords) { + return browser.history.search({ text: keywords, startTime: 0, }); - return [historyItems.map(item => [item, new URL(item.url)])] - .map(filterEmptyTitle) - .map(filterHttp) - .map(filterClosedPath) - .map(items => reduceByPathname(items, 10)) - .map(items => reduceByOrigin(items, 10)) - .map(items => items - .sort((x, y) => x[0].visitCount < y[0].visitCount) - .slice(0, 10) - .map(item => item[0]) - )[0]; } async queryTabs(keywords, excludePinned) { |