Keep order of chapters when removing duplicates

This commit is contained in:
schroda
2024-04-29 12:37:11 +02:00
parent 045c60c828
commit 3f507e0a30

View File

@@ -320,15 +320,18 @@ export class Chapters {
static removeDuplicates<T extends ChapterScanlatorInfo & ChapterNumberInfo>(currentChapter: T, chapters: T[]): T[] { static removeDuplicates<T extends ChapterScanlatorInfo & ChapterNumberInfo>(currentChapter: T, chapters: T[]): T[] {
const chapterNumberToChapters = Object.groupBy(chapters, ({ chapterNumber }) => chapterNumber); const chapterNumberToChapters = Object.groupBy(chapters, ({ chapterNumber }) => chapterNumber);
return Object.values(chapterNumberToChapters) const uniqueChapters = Object.values(chapterNumberToChapters).map(
.map(
(groupedChapters) => (groupedChapters) =>
// the result of groupBy can't result in undefined values // the result of groupBy can't result in undefined values
groupedChapters!.find((chapter) => chapter.id === currentChapter.id) ?? groupedChapters!.find((chapter) => chapter.id === currentChapter.id) ??
groupedChapters!.findLast((chapter) => chapter.scanlator === currentChapter.scanlator) ?? groupedChapters!.findLast((chapter) => chapter.scanlator === currentChapter.scanlator) ??
groupedChapters!.slice(-1)[0], groupedChapters!.slice(-1)[0],
) );
.sort((a, b) => b.chapterNumber - a.chapterNumber);
// keep the chapters in the same order as they were passed
return chapters
.map(({ id }) => uniqueChapters.find((chapter) => chapter.id === id))
.filter((chapter): chapter is T => !!chapter);
} }
static getNextChapter<Chapter extends ChapterScanlatorInfo & ChapterNumberInfo & ChapterReadInfo>( static getNextChapter<Chapter extends ChapterScanlatorInfo & ChapterNumberInfo & ChapterReadInfo>(