Simplify "Chapters#getMissingCount"

This commit is contained in:
schroda
2025-06-09 15:30:39 +02:00
parent 20a325a6c2
commit 645eaeb315

View File

@@ -413,23 +413,13 @@ export class Chapters {
} }
static getMissingCount<Chapter extends ChapterNumberInfo>(chapters: Chapter[]): number { static getMissingCount<Chapter extends ChapterNumberInfo>(chapters: Chapter[]): number {
const normalizedChapterNumbers = chapters const sortedChapters = chapters.toSorted((a, b) => a.chapterNumber - b.chapterNumber);
.map((chapter) => chapter.chapterNumber)
.filter((chapterNumber) => chapterNumber !== -1)
.map(Math.floor)
.toSorted((a, b) => a - b);
const uniqueChapterNumbers = [...new Set(normalizedChapterNumbers)]; return sortedChapters.reduce(
(missingChapterCount, chapter, index) =>
return uniqueChapterNumbers.reduce((missingChapterCount, chapterNumber, index, chapterNumbers) => { missingChapterCount + Chapters.getGap(chapter, sortedChapters[index - 1]),
const previousChapterNumber = chapterNumbers[index - 1] ?? chapterNumber; 0,
);
if (chapterNumber > previousChapterNumber + 1) {
return missingChapterCount + (chapterNumber - previousChapterNumber - 1);
}
return missingChapterCount;
}, 0);
} }
static getGap<Chapter extends ChapterNumberInfo>( static getGap<Chapter extends ChapterNumberInfo>(