Show missing chapter info in chapter list

Closes #836
This commit is contained in:
schroda
2025-05-24 20:32:03 +02:00
parent 86578593ca
commit 61471ae214
7 changed files with 164 additions and 16 deletions

View File

@@ -407,4 +407,42 @@ export class Chapters {
T[]
>;
}
static getMissingCount<Chapter extends ChapterNumberInfo>(chapters: Chapter[]): number {
const normalizedChapterNumbers = chapters
.map((chapter) => chapter.chapterNumber)
.filter((chapterNumber) => chapterNumber !== -1)
.map(Math.floor)
.toSorted((a, b) => a - b);
const uniqueChapterNumbers = [...new Set(normalizedChapterNumbers)];
return uniqueChapterNumbers.reduce((missingChapterCount, chapterNumber, index, chapterNumbers) => {
const previousChapterNumber = chapterNumbers[index - 1] ?? chapterNumber;
if (chapterNumber > previousChapterNumber + 1) {
return missingChapterCount + (chapterNumber - previousChapterNumber - 1);
}
return missingChapterCount;
}, 0);
}
static getGap<Chapter extends ChapterNumberInfo>(
chapterA: Chapter | undefined,
chapterB: Chapter | undefined,
): number {
if (!chapterA || !chapterB) {
return 0;
}
if (chapterA.chapterNumber === -1 || chapterB.chapterNumber === -1) {
return 0;
}
const higherChapterNumber = Math.max(chapterA.chapterNumber, chapterB.chapterNumber);
const lowerChapterNumber = Math.min(chapterA.chapterNumber, chapterB.chapterNumber);
return higherChapterNumber - lowerChapterNumber - 1;
}
}