Keep order of chapters when removing duplicates
This commit is contained in:
@@ -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>(
|
||||||
|
|||||||
Reference in New Issue
Block a user