aboutsummaryrefslogtreecommitdiff
path: root/source/library/state/history-logic.ts
blob: 5fce766c8f7b8cfc0d666aa1adb71b6738b929c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*** EXPORT ------------------------------------------- ***/

export type HistoryEvictable = {
  favorite: boolean;
  timestamp: number;
};

export function evict<T extends HistoryEvictable>(entries: T[], max: number): T[] {
  if (entries.length <= max)
    return entries;

  const favorites = entries.filter((e) => e.favorite);
  const regular = entries.filter((e) => !e.favorite);
  const keepRegular = regular.slice(0, Math.max(0, max - favorites.length));

  return [...favorites, ...keepRegular].sort((a, b) => b.timestamp - a.timestamp);
}