aboutsummaryrefslogtreecommitdiff
path: root/src/background/usecases/filters.ts
blob: 98957a7b244f45b4fb47bffcb16215100a588919 (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
};