From 890d93fe773ea5cde41d19125d4bad626e54059e Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 21 Jul 2018 10:35:27 +0900 Subject: Buffer completion on Clean Architecture --- src/background/repositories/completions.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/background/repositories/completions.js (limited to 'src/background/repositories/completions.js') diff --git a/src/background/repositories/completions.js b/src/background/repositories/completions.js new file mode 100644 index 0000000..30ede05 --- /dev/null +++ b/src/background/repositories/completions.js @@ -0,0 +1,11 @@ +export default class CompletionsRepository { + async queryTabs(keywords, excludePinned) { + let tabs = await browser.tabs.query({ currentWindow: true }); + return tabs.filter((t) => { + return t.url.toLowerCase().includes(keywords.toLowerCase()) || + t.title && t.title.toLowerCase().includes(keywords.toLowerCase()); + }).filter((t) => { + return !(excludePinned && t.pinned); + }); + } +} -- cgit v1.2.3 From fc5e6b2b410d6850278dd982990ce42552a3b1b2 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 21 Jul 2018 11:17:51 +0900 Subject: Complete open histories and bookmarks --- src/background/controllers/completions.js | 2 +- src/background/repositories/completions.js | 97 ++++++++++++++++++++++++++++++ src/background/usecases/completions.js | 24 +++++++- 3 files changed, 120 insertions(+), 3 deletions(-) (limited to 'src/background/repositories/completions.js') diff --git a/src/background/controllers/completions.js b/src/background/controllers/completions.js index c22ba83..f8eade9 100644 --- a/src/background/controllers/completions.js +++ b/src/background/controllers/completions.js @@ -21,7 +21,7 @@ export default class ContentMessageController { case 'tabopen': case 'w': case 'winopen': - return this.completionsInteractor.queryOpen(name); + return this.completionsInteractor.queryOpen(name, keywords); case 'b': case 'buffer': return this.completionsInteractor.queryBuffer(name, keywords); diff --git a/src/background/repositories/completions.js b/src/background/repositories/completions.js index 30ede05..c4e956e 100644 --- a/src/background/repositories/completions.js +++ b/src/background/repositories/completions.js @@ -1,4 +1,101 @@ export default class CompletionsRepository { + async queryBookmarks(keywords) { + let items = await browser.bookmarks.search({ query: keywords }); + return items.filter((item) => { + let url = undefined; + try { + url = new URL(item.url); + } catch (e) { + return false; + } + return item.type === 'bookmark' && url.protocol !== 'place:'; + }); + } + + // 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({ + 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) { let tabs = await browser.tabs.query({ currentWindow: true }); return tabs.filter((t) => { diff --git a/src/background/usecases/completions.js b/src/background/usecases/completions.js index 21049d2..0bc34d1 100644 --- a/src/background/usecases/completions.js +++ b/src/background/usecases/completions.js @@ -27,8 +27,28 @@ export default class CompletionsInteractor { ); } - queryOpen() { - return Promise.resolve(Completions.empty()); + async queryOpen(name, keywords) { + let groups = []; + let histories = await this.completionRepository.queryHistories(keywords); + if (histories.length > 0) { + let items = histories.map(page => new CompletionItem({ + caption: page.title, + content: name + ' ' + page.url, + url: page.url + })); + groups.push(new CompletionGroup('History', items)); + } + + let bookmarks = await this.completionRepository.queryBookmarks(keywords); + if (bookmarks.length > 0) { + let items = bookmarks.map(page => new CompletionItem({ + caption: page.title, + content: name + ' ' + page.url, + url: page.url + })); + groups.push(new CompletionGroup('Bookmarks', items)); + } + return new Completions(groups); } queryBuffer(name, keywords) { -- cgit v1.2.3 From 0652131de885df3ebb7c9c0e762fd1604486a892 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 21 Jul 2018 15:59:30 +0900 Subject: Clean and tests for filters on open command --- src/background/repositories/completions.js | 81 +-------------------- src/background/usecases/completions.js | 12 +++ src/background/usecases/filters.js | 72 ++++++++++++++++++ test/background/usecases/filters.test.js | 113 +++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+), 79 deletions(-) create mode 100644 src/background/usecases/filters.js create mode 100644 test/background/usecases/filters.test.js (limited to 'src/background/repositories/completions.js') 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) { diff --git a/src/background/usecases/completions.js b/src/background/usecases/completions.js index 0bc34d1..1c0fe38 100644 --- a/src/background/usecases/completions.js +++ b/src/background/usecases/completions.js @@ -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, diff --git a/src/background/usecases/filters.js b/src/background/usecases/filters.js new file mode 100644 index 0000000..d057dca --- /dev/null +++ b/src/background/usecases/filters.js @@ -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 +}; diff --git a/test/background/usecases/filters.test.js b/test/background/usecases/filters.test.js new file mode 100644 index 0000000..bdfb0be --- /dev/null +++ b/test/background/usecases/filters.test.js @@ -0,0 +1,113 @@ +import * as filters from 'background/usecases/filters'; + +describe("background/usecases/filters", () => { + describe('filterHttp', () => { + it('filters http URLs duplicates to https hosts', () => { + let pages = [ + { url: 'http://i-beam.org/foo' }, + { url: 'https://i-beam.org/bar' }, + { url: 'http://i-beam.net/hoge' }, + { url: 'http://i-beam.net/fuga' }, + ]; + let filtered = filters.filterHttp(pages); + + let urls = filtered.map(x => x.url); + expect(urls).to.deep.equal([ + 'https://i-beam.org/bar', 'http://i-beam.net/hoge', 'http://i-beam.net/fuga' + ]); + }) + }); + + describe('filterBlankTitle', () => { + it('filters blank titles', () => { + let pages = [ + { title: 'hello' }, + { title: '' }, + {}, + ]; + let filtered = filters.filterBlankTitle(pages); + + expect(filtered).to.deep.equal([{ title: 'hello' }]); + }); + }) + + describe('filterByTailingSlash', () => { + it('filters duplicated pathname on tailing slash', () => { + let pages = [ + { url: 'http://i-beam.org/content' }, + { url: 'http://i-beam.org/content/' }, + { url: 'http://i-beam.org/search' }, + { url: 'http://i-beam.org/search?q=apple_banana_cherry' }, + ]; + let filtered = filters.filterByTailingSlash(pages); + + let urls = filtered.map(x => x.url); + expect(urls).to.deep.equal([ + 'http://i-beam.org/content', + 'http://i-beam.org/search', + 'http://i-beam.org/search?q=apple_banana_cherry', + ]); + }); + }) + + describe('filterByPathname', () => { + it('remains items less than minimam length', () => { + let pages = [ + { url: 'http://i-beam.org/search?q=apple' }, + { url: 'http://i-beam.org/search?q=apple_banana' }, + { url: 'http://i-beam.org/search?q=apple_banana_cherry' }, + { url: 'http://i-beam.org/request?q=apple' }, + { url: 'http://i-beam.org/request?q=apple_banana' }, + { url: 'http://i-beam.org/request?q=apple_banana_cherry' }, + ]; + let filtered = filters.filterByPathname(pages, 10); + expect(filtered).to.have.lengthOf(6); + }); + + it('filters by length of pathname', () => { + let pages = [ + { url: 'http://i-beam.org/search?q=apple' }, + { url: 'http://i-beam.org/search?q=apple_banana' }, + { url: 'http://i-beam.org/search?q=apple_banana_cherry' }, + { url: 'http://i-beam.net/search?q=apple' }, + { url: 'http://i-beam.net/search?q=apple_banana' }, + { url: 'http://i-beam.net/search?q=apple_banana_cherry' }, + ]; + let filtered = filters.filterByPathname(pages, 0); + expect(filtered).to.deep.equal([ + { url: 'http://i-beam.org/search?q=apple' }, + { url: 'http://i-beam.net/search?q=apple' }, + ]); + }); + }) + + describe('filterByOrigin', () => { + it('remains items less than minimam length', () => { + let pages = [ + { url: 'http://i-beam.org/search?q=apple' }, + { url: 'http://i-beam.org/search?q=apple_banana' }, + { url: 'http://i-beam.org/search?q=apple_banana_cherry' }, + { url: 'http://i-beam.org/request?q=apple' }, + { url: 'http://i-beam.org/request?q=apple_banana' }, + { url: 'http://i-beam.org/request?q=apple_banana_cherry' }, + ]; + let filtered = filters.filterByOrigin(pages, 10); + expect(filtered).to.have.lengthOf(6); + }); + + it('filters by length of pathname', () => { + let pages = [ + { url: 'http://i-beam.org/search?q=apple' }, + { url: 'http://i-beam.org/search?q=apple_banana' }, + { url: 'http://i-beam.org/search?q=apple_banana_cherry' }, + { url: 'http://i-beam.org/request?q=apple' }, + { url: 'http://i-beam.org/request?q=apple_banana' }, + { url: 'http://i-beam.org/request?q=apple_banana_cherry' }, + ]; + let filtered = filters.filterByOrigin(pages, 0); + expect(filtered).to.deep.equal([ + { url: 'http://i-beam.org/search?q=apple' }, + ]); + }); + }) +}); -- cgit v1.2.3