From a8d78f1286fb3fe456a786b2c0e534d212835560 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Fri, 27 Mar 2020 07:23:17 +0900 Subject: Separate repository's interface and its implementation --- src/background/completion/BookmarkRepository.ts | 28 +------- src/background/completion/CompletionUseCase.ts | 15 ++--- src/background/completion/HistoryRepository.ts | 28 +------- src/background/completion/filters.ts | 76 ---------------------- .../completion/impl/BookmarkRepositoryImpl.ts | 28 ++++++++ .../completion/impl/HistoryRepositoryImpl.ts | 28 ++++++++ src/background/completion/impl/filters.ts | 76 ++++++++++++++++++++++ src/background/di.ts | 4 ++ 8 files changed, 147 insertions(+), 136 deletions(-) delete mode 100644 src/background/completion/filters.ts create mode 100644 src/background/completion/impl/BookmarkRepositoryImpl.ts create mode 100644 src/background/completion/impl/HistoryRepositoryImpl.ts create mode 100644 src/background/completion/impl/filters.ts (limited to 'src/background') diff --git a/src/background/completion/BookmarkRepository.ts b/src/background/completion/BookmarkRepository.ts index 12f5455..14105c8 100644 --- a/src/background/completion/BookmarkRepository.ts +++ b/src/background/completion/BookmarkRepository.ts @@ -1,32 +1,8 @@ -import { injectable } from "tsyringe"; - export type BookmarkItem = { title: string url: string } -const COMPLETION_ITEM_LIMIT = 10; - -@injectable() -export default class BookmarkRepository { - async queryBookmarks(query: string): Promise { - const items = await browser.bookmarks.search({ query }); - return items - .filter(item => item.title && item.title.length > 0) - .filter(item => item.type === 'bookmark' && item.url) - .filter((item) => { - let url = undefined; - try { - url = new URL(item.url!!); - } catch (e) { - return false; - } - return url.protocol !== 'place:'; - }) - .slice(0, COMPLETION_ITEM_LIMIT) - .map(item => ({ - title: item.title!!, - url: item.url!!, - })); - } +export default interface BookmarkRepository { + queryBookmarks(query: string): Promise; } diff --git a/src/background/completion/CompletionUseCase.ts b/src/background/completion/CompletionUseCase.ts index fd3c279..f7531e7 100644 --- a/src/background/completion/CompletionUseCase.ts +++ b/src/background/completion/CompletionUseCase.ts @@ -1,8 +1,8 @@ import { inject, injectable } from "tsyringe"; -import HistoryRepository from "./HistoryRepository"; -import BookmarkRepository from "./BookmarkRepository"; import CachedSettingRepository from "../repositories/CachedSettingRepository"; import CompletionType from "../../shared/CompletionType"; +import BookmarkRepository from "./BookmarkRepository"; +import HistoryRepository from "./HistoryRepository"; export type BookmarkItem = { title: string @@ -17,10 +17,9 @@ export type HistoryItem = { @injectable() export default class CompletionUseCase { constructor( - private bookmarkRepository: BookmarkRepository, - private historyRepository: HistoryRepository, - @inject("CachedSettingRepository") - private cachedSettingRepository: CachedSettingRepository, + @inject('BookmarkRepository') private bookmarkRepository: BookmarkRepository, + @inject('HistoryRepository') private historyRepository: HistoryRepository, + @inject("CachedSettingRepository") private cachedSettingRepository: CachedSettingRepository, ) { } @@ -50,11 +49,11 @@ export default class CompletionUseCase { .filter(key => key.startsWith(query)) } - requestBookmarks(query: any): Promise { + requestBookmarks(query: string): Promise { return this.bookmarkRepository.queryBookmarks(query); } - async requestHistory(query: string): Promise { + requestHistory(query: string): Promise { return this.historyRepository.queryHistories(query); } } \ No newline at end of file diff --git a/src/background/completion/HistoryRepository.ts b/src/background/completion/HistoryRepository.ts index 1cfaf1b..5eb3a2b 100644 --- a/src/background/completion/HistoryRepository.ts +++ b/src/background/completion/HistoryRepository.ts @@ -1,32 +1,8 @@ -import * as filters from "./filters"; -import { injectable } from "tsyringe"; - export type HistoryItem = { title: string url: string } -const COMPLETION_ITEM_LIMIT = 10; - -@injectable() -export default class HistoryRepository { - async queryHistories(keywords: string): Promise { - const items = await browser.history.search({ - text: keywords, - startTime: 0, - }); - - return [items] - .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) => Number(y.visitCount) - Number(x.visitCount)) - .slice(0, COMPLETION_ITEM_LIMIT) - .map(item => ({ - title: item.title!!, - url: item.url!!, - })) - } +export default interface HistoryRepository { + queryHistories(keywords: string): Promise; } diff --git a/src/background/completion/filters.ts b/src/background/completion/filters.ts deleted file mode 100644 index 98957a7..0000000 --- a/src/background/completion/filters.ts +++ /dev/null @@ -1,76 +0,0 @@ -type Item = browser.history.HistoryItem; - -const filterHttp = (items: Item[]): Item[] => { - const httpsHosts = items.map(x => new URL(x.url as string)) - .filter(x => x.protocol === 'https:') - .map(x => x.host); - const hostsSet = new Set(httpsHosts); - - return items.filter((item: Item) => { - const url = new URL(item.url as string); - return url.protocol === 'https:' || !hostsSet.has(url.host); - }); -}; - -const filterBlankTitle = (items: Item[]): Item[] => { - return items.filter(item => item.title && item.title !== ''); -}; - -const filterByTailingSlash = (items: Item[]): Item[] => { - const urls = items.map(item => new URL(item.url as string)); - const simplePaths = urls - .filter(url => url.hash === '' && url.search === '') - .map(url => url.origin + url.pathname); - const pathsSet = new Set(simplePaths); - - return items.filter((item) => { - const url = new URL(item.url as string); - if (url.hash !== '' || url.search !== '' || - url.pathname.slice(-1) !== '/') { - return true; - } - return !pathsSet.has(url.origin + url.pathname.slice(0, -1)); - }); -}; - -const filterByPathname = (items: Item[], min: number): Item[] => { - const hash: {[key: string]: Item} = {}; - for (const item of items) { - const url = new URL(item.url as string); - const pathname = url.origin + url.pathname; - if (!hash[pathname]) { - hash[pathname] = item; - } else if ((hash[pathname].url as string).length > - (item.url as string).length) { - hash[pathname] = item; - } - } - const filtered = Object.values(hash); - if (filtered.length < min) { - return items; - } - return filtered; -}; - -const filterByOrigin = (items: Item[], min: number): Item[] => { - const hash: {[key: string]: Item} = {}; - for (const item of items) { - const origin = new URL(item.url as string).origin; - if (!hash[origin]) { - hash[origin] = item; - } else if ((hash[origin].url as string).length > - (item.url as string).length) { - hash[origin] = item; - } - } - const filtered = Object.values(hash); - if (filtered.length < min) { - return items; - } - return filtered; -}; - -export { - filterHttp, filterBlankTitle, filterByTailingSlash, - filterByPathname, filterByOrigin -}; diff --git a/src/background/completion/impl/BookmarkRepositoryImpl.ts b/src/background/completion/impl/BookmarkRepositoryImpl.ts new file mode 100644 index 0000000..58df129 --- /dev/null +++ b/src/background/completion/impl/BookmarkRepositoryImpl.ts @@ -0,0 +1,28 @@ +import { injectable } from "tsyringe"; +import BookmarkRepository, {BookmarkItem} from "../BookmarkRepository"; + +const COMPLETION_ITEM_LIMIT = 10; + +@injectable() +export default class BookmarkRepositoryImpl implements BookmarkRepository { + async queryBookmarks(query: string): Promise { + const items = await browser.bookmarks.search({query}); + return items + .filter(item => item.title && item.title.length > 0) + .filter(item => item.type === 'bookmark' && item.url) + .filter((item) => { + let url = undefined; + try { + url = new URL(item.url!!); + } catch (e) { + return false; + } + return url.protocol !== 'place:'; + }) + .slice(0, COMPLETION_ITEM_LIMIT) + .map(item => ({ + title: item.title!!, + url: item.url!!, + })); + } +} diff --git a/src/background/completion/impl/HistoryRepositoryImpl.ts b/src/background/completion/impl/HistoryRepositoryImpl.ts new file mode 100644 index 0000000..42691aa --- /dev/null +++ b/src/background/completion/impl/HistoryRepositoryImpl.ts @@ -0,0 +1,28 @@ +import { injectable } from "tsyringe"; +import * as filters from "./filters"; +import HistoryRepository, {HistoryItem} from "../HistoryRepository"; + +const COMPLETION_ITEM_LIMIT = 10; + +@injectable() +export default class HistoryRepositoryImpl implements HistoryRepository { + async queryHistories(keywords: string): Promise { + const items = await browser.history.search({ + text: keywords, + startTime: 0, + }); + + return [items] + .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) => Number(y.visitCount) - Number(x.visitCount)) + .slice(0, COMPLETION_ITEM_LIMIT) + .map(item => ({ + title: item.title!!, + url: item.url!!, + })) + } +} diff --git a/src/background/completion/impl/filters.ts b/src/background/completion/impl/filters.ts new file mode 100644 index 0000000..98957a7 --- /dev/null +++ b/src/background/completion/impl/filters.ts @@ -0,0 +1,76 @@ +type Item = browser.history.HistoryItem; + +const filterHttp = (items: Item[]): Item[] => { + const httpsHosts = items.map(x => new URL(x.url as string)) + .filter(x => x.protocol === 'https:') + .map(x => x.host); + const hostsSet = new Set(httpsHosts); + + return items.filter((item: Item) => { + const url = new URL(item.url as string); + return url.protocol === 'https:' || !hostsSet.has(url.host); + }); +}; + +const filterBlankTitle = (items: Item[]): Item[] => { + return items.filter(item => item.title && item.title !== ''); +}; + +const filterByTailingSlash = (items: Item[]): Item[] => { + const urls = items.map(item => new URL(item.url as string)); + const simplePaths = urls + .filter(url => url.hash === '' && url.search === '') + .map(url => url.origin + url.pathname); + const pathsSet = new Set(simplePaths); + + return items.filter((item) => { + const url = new URL(item.url as string); + if (url.hash !== '' || url.search !== '' || + url.pathname.slice(-1) !== '/') { + return true; + } + return !pathsSet.has(url.origin + url.pathname.slice(0, -1)); + }); +}; + +const filterByPathname = (items: Item[], min: number): Item[] => { + const hash: {[key: string]: Item} = {}; + for (const item of items) { + const url = new URL(item.url as string); + const pathname = url.origin + url.pathname; + if (!hash[pathname]) { + hash[pathname] = item; + } else if ((hash[pathname].url as string).length > + (item.url as string).length) { + hash[pathname] = item; + } + } + const filtered = Object.values(hash); + if (filtered.length < min) { + return items; + } + return filtered; +}; + +const filterByOrigin = (items: Item[], min: number): Item[] => { + const hash: {[key: string]: Item} = {}; + for (const item of items) { + const origin = new URL(item.url as string).origin; + if (!hash[origin]) { + hash[origin] = item; + } else if ((hash[origin].url as string).length > + (item.url as string).length) { + hash[origin] = item; + } + } + const filtered = Object.values(hash); + if (filtered.length < min) { + return items; + } + return filtered; +}; + +export { + filterHttp, filterBlankTitle, filterByTailingSlash, + filterByPathname, filterByOrigin +}; diff --git a/src/background/di.ts b/src/background/di.ts index 9fc230c..0b52e0b 100644 --- a/src/background/di.ts +++ b/src/background/di.ts @@ -4,8 +4,12 @@ import { LocalSettingRepository, SyncSettingRepository } from "./repositories/Se import { NotifierImpl } from "./presenters/Notifier"; import { CachedSettingRepositoryImpl } from "./repositories/CachedSettingRepository"; import { container } from 'tsyringe'; +import HistoryRepositoryImpl from "./completion/impl/HistoryRepositoryImpl"; +import BookmarkRepositoryImpl from "./completion/impl/BookmarkRepositoryImpl"; container.register('LocalSettingRepository', { useValue: LocalSettingRepository }); container.register('SyncSettingRepository', { useClass: SyncSettingRepository }); container.register('CachedSettingRepository', { useClass: CachedSettingRepositoryImpl }); container.register('Notifier', { useClass: NotifierImpl }); +container.register('HistoryRepository', { useClass: HistoryRepositoryImpl }); +container.register('BookmarkRepository', { useClass: BookmarkRepositoryImpl }); -- cgit v1.2.3