aboutsummaryrefslogtreecommitdiff
path: root/src/background/completion/impl/HistoryRepositoryImpl.ts
diff options
context:
space:
mode:
authorShin'ya Ueoka <ueokande@i-beam.org>2020-03-27 07:23:17 +0900
committerShin'ya Ueoka <ueokande@i-beam.org>2020-03-27 20:47:35 +0900
commita8d78f1286fb3fe456a786b2c0e534d212835560 (patch)
treed42a27548a7f3ad8b71336109dc5b48f95ec554f /src/background/completion/impl/HistoryRepositoryImpl.ts
parentb2a37b8fc3e273dd71e1e3558c58be8002aa3789 (diff)
Separate repository's interface and its implementation
Diffstat (limited to 'src/background/completion/impl/HistoryRepositoryImpl.ts')
-rw-r--r--src/background/completion/impl/HistoryRepositoryImpl.ts28
1 files changed, 28 insertions, 0 deletions
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!!,
+ }))
+ }
+}