From 645eaeb3158f8d2e588c100d8d3f715547e8495b Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Mon, 9 Jun 2025 15:30:39 +0200 Subject: [PATCH] Simplify "Chapters#getMissingCount" --- src/modules/chapter/services/Chapters.ts | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/modules/chapter/services/Chapters.ts b/src/modules/chapter/services/Chapters.ts index 4c53163e..735231b3 100644 --- a/src/modules/chapter/services/Chapters.ts +++ b/src/modules/chapter/services/Chapters.ts @@ -413,23 +413,13 @@ export class Chapters { } static getMissingCount(chapters: Chapter[]): number { - const normalizedChapterNumbers = chapters - .map((chapter) => chapter.chapterNumber) - .filter((chapterNumber) => chapterNumber !== -1) - .map(Math.floor) - .toSorted((a, b) => a - b); + const sortedChapters = chapters.toSorted((a, b) => a.chapterNumber - b.chapterNumber); - 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); + return sortedChapters.reduce( + (missingChapterCount, chapter, index) => + missingChapterCount + Chapters.getGap(chapter, sortedChapters[index - 1]), + 0, + ); } static getGap(