-
Notifications
You must be signed in to change notification settings - Fork 515
/
search-index.ts
39 lines (34 loc) · 1.02 KB
/
search-index.ts
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
import { getPopularities } from "../content/index.js";
// getPopularities() is memoized so it's fast to call repeatedly
const getPopularity = (item) => getPopularities().get(item.url) || 0;
type Locale = string;
interface SearchItem {
title: string;
url: string;
}
export default class SearchIndex {
private _itemsByLocale: Record<Locale, SearchItem[]> = {};
add({ metadata: { locale, title }, url }) {
const localeLC = locale.toLowerCase();
if (!this._itemsByLocale[localeLC]) {
this._itemsByLocale[localeLC] = [];
}
this._itemsByLocale[localeLC].push({ title, url });
}
sort() {
for (const items of Object.values(this._itemsByLocale)) {
items.sort((a, b) => {
const popularityA = getPopularity(a);
const popularityB = getPopularity(b);
const diff = popularityB - popularityA;
if (diff === 0) {
return a.url < b.url ? -1 : a.url > b.url ? 1 : 0;
}
return diff;
});
}
}
getItems() {
return this._itemsByLocale;
}
}