Simplify "Chapters#getMissingCount"
This commit is contained in:
@@ -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>(
|
||||||
|
|||||||
Reference in New Issue
Block a user