diff options
Diffstat (limited to 'src/background/completion')
-rw-r--r-- | src/background/completion/BookmarkRepository.ts | 28 | ||||
-rw-r--r-- | src/background/completion/CompletionUseCase.ts | 15 | ||||
-rw-r--r-- | src/background/completion/HistoryRepository.ts | 28 | ||||
-rw-r--r-- | src/background/completion/impl/BookmarkRepositoryImpl.ts | 28 | ||||
-rw-r--r-- | src/background/completion/impl/HistoryRepositoryImpl.ts | 28 | ||||
-rw-r--r-- | src/background/completion/impl/filters.ts (renamed from src/background/completion/filters.ts) | 0 |
6 files changed, 67 insertions, 60 deletions
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<BookmarkItem[]> { - 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<BookmarkItem[]>; } 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<BookmarkItem[]> { + requestBookmarks(query: string): Promise<BookmarkItem[]> { return this.bookmarkRepository.queryBookmarks(query); } - async requestHistory(query: string): Promise<HistoryItem[]> { + requestHistory(query: string): Promise<HistoryItem[]> { 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<HistoryItem[]> { - 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<HistoryItem[]>; } 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<BookmarkItem[]> { + 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<HistoryItem[]> { + 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/filters.ts b/src/background/completion/impl/filters.ts index 98957a7..98957a7 100644 --- a/src/background/completion/filters.ts +++ b/src/background/completion/impl/filters.ts |