-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathcatalog.ts
More file actions
46 lines (38 loc) · 1.24 KB
/
Copy pathcatalog.ts
File metadata and controls
46 lines (38 loc) · 1.24 KB
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
import type { VolumeMetadata } from '$lib/types';
import { sortVolumes } from './sort-volumes';
export interface Series {
title: string;
series_uuid: string;
volumes: VolumeMetadata[];
}
function sortTitles(a: Series, b: Series) {
return a.title.localeCompare(b.title, undefined, { sensitivity: 'base' });
}
function normalizeSeriesTitle(title: string): string {
return title.trim().replace(/\s+/g, ' ').toLowerCase();
}
export function deriveSeriesFromVolumes(volumeEntries: Array<VolumeMetadata>) {
// Group volumes by normalized series title (user-visible identity)
const titleMap = new Map<string, Series>();
for (const entry of volumeEntries) {
const key = normalizeSeriesTitle(entry.series_title);
let volumes = titleMap.get(key);
if (volumes === undefined) {
volumes = {
title: entry.series_title,
series_uuid: entry.series_uuid,
volumes: []
};
titleMap.set(key, volumes);
}
volumes.volumes.push(entry);
}
// Convert map to array and sort everything
const titles = Array.from(titleMap.values());
// Sort series by title, and volumes within each series
titles.sort(sortTitles);
for (const series of titles) {
series.volumes.sort(sortVolumes);
}
return titles;
}