From 4549f937f2e1f3202d8d5604df69c9a61cf4bbca Mon Sep 17 00:00:00 2001 From: Naputt1 <94742489+naputt1@users.noreply.github.com> Date: Fri, 16 Aug 2024 09:21:50 +0700 Subject: [PATCH] fix(chapter): Fixed chapter number parsing when number is after unwanted tag --- .../chapter/services/ChapterRecognition.kt | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/domain/src/commonMain/kotlin/yokai/domain/chapter/services/ChapterRecognition.kt b/domain/src/commonMain/kotlin/yokai/domain/chapter/services/ChapterRecognition.kt index fd14c06252..bc257db608 100644 --- a/domain/src/commonMain/kotlin/yokai/domain/chapter/services/ChapterRecognition.kt +++ b/domain/src/commonMain/kotlin/yokai/domain/chapter/services/ChapterRecognition.kt @@ -37,25 +37,35 @@ object ChapterRecognition { } // Get chapter title with lower case - var name = chapterName.lowercase() + val cleanChapterName = chapterName.lowercase() + // Remove manga title from chapter title. + .replace(mangaTitle.lowercase(), "").trim() + // Remove comma's or hyphens. + .replace(',', '.') + .replace('-', '.') + // Remove unwanted white spaces. + .replace(unwantedWhiteSpace, "") - // Remove manga title from chapter title. - name = name.replace(mangaTitle.lowercase(), "").trim() + val numberMatch = number.findAll(cleanChapterName) - // Remove comma's or hyphens. - name = name.replace(',', '.').replace('-', '.') + when { + numberMatch.none() -> { + return chapterNumber ?: -1f + } + numberMatch.count() > 1 -> { + // Remove unwanted tags. + unwanted.replace(cleanChapterName, "").let { name -> + // Check base case ch.xx + basic.find(name)?.let { return getChapterNumberFromMatch(it) } - // Remove unwanted white spaces. - name = unwantedWhiteSpace.replace(name, "") + // need to find again first number might already removed + number.find(name)?.let { return getChapterNumberFromMatch(it) } + } + } + } - // Remove unwanted tags. - name = unwanted.replace(name, "") - - basic.find(name)?.let { return getChapterNumberFromMatch(it) } - - number.find(name)?.let { return getChapterNumberFromMatch(it) } - - return chapterNumber ?: -1f + // return the first number encountered + return getChapterNumberFromMatch(numberMatch.first()) } /**