aboutsummaryrefslogtreecommitdiff
path: root/src/background/completion/impl/HistoryRepositoryImpl.ts
blob: 789b3930e0bbdc0b3fac024263de24994e16932b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import * as filters from "./filters";
import HistoryRepository, { HistoryItem } from "../HistoryRepository";

const COMPLETION_ITEM_LIMIT = 10;

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!,
      }));
  }
}