From 0c2029a60135db0549429a90d3c436609a83ceaa Mon Sep 17 00:00:00 2001 From: Tanveer Najib Date: Sat, 3 Feb 2024 21:16:15 +0000 Subject: [PATCH 1/5] Account for decimals in unread chapter count This removes reliance on Chapter.chapterNumber (which is nullable) --- src/util/comparison.ts | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/util/comparison.ts b/src/util/comparison.ts index 279dbdea6..01fbb8bbe 100644 --- a/src/util/comparison.ts +++ b/src/util/comparison.ts @@ -49,23 +49,10 @@ export function selectMostSimilarChapter(original: Chapter, options: Chapter[]): /** * Get the number of unread chapters from a list. * This function calculates a value using the Chapter.chapterNumber field and read status of each - * chapter. It is not necessarily correlated with the number of chapter objects in the list. + * chapter. * @param chapterList the list of chapters to calculate from (usually all of a series' chapters) * @returns the number of unread chapters (by chapter number) */ export function getNumberUnreadChapters(chapterList: Chapter[]): number { - let highestRead = 0; - let highestReleased = 0; - - chapterList.forEach((chapter: Chapter) => { - const chapterNumber = parseFloat(chapter.chapterNumber); - if (chapter.read && chapterNumber > highestRead) { - highestRead = chapterNumber; - } - if (chapterNumber > highestReleased) { - highestReleased = chapterNumber; - } - }); - - return Math.ceil(highestReleased - highestRead); + return chapterList.filter((chapter: Chapter) => !chapter.read).length } From 1fa674cfc0620989d9890fae18285f3ae6b6e242 Mon Sep 17 00:00:00 2001 From: Tanveer Najib Date: Fri, 16 Feb 2024 23:59:07 +0000 Subject: [PATCH 2/5] Account for almost all considerations for unread chapter count --- src/util/comparison.ts | 46 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/src/util/comparison.ts b/src/util/comparison.ts index 01fbb8bbe..e4000b9f8 100644 --- a/src/util/comparison.ts +++ b/src/util/comparison.ts @@ -49,10 +49,52 @@ export function selectMostSimilarChapter(original: Chapter, options: Chapter[]): /** * Get the number of unread chapters from a list. * This function calculates a value using the Chapter.chapterNumber field and read status of each - * chapter. + * chapter. It is not necessarily correlated with the number of chapter objects in the list. * @param chapterList the list of chapters to calculate from (usually all of a series' chapters) * @returns the number of unread chapters (by chapter number) */ export function getNumberUnreadChapters(chapterList: Chapter[]): number { - return chapterList.filter((chapter: Chapter) => !chapter.read).length + let highestRead = 0; + let highestReleased = 0; + + const chapters = consolidateAndSortChapters(chapterList); + + chapters.forEach((chapter: Chapter, index: number) => { + const absoluteNumber = index + 1; + if (chapter.read && absoluteNumber > highestRead) { + highestRead = absoluteNumber; + } + if (absoluteNumber > highestReleased) { + highestReleased = absoluteNumber; + } + }); + + return Math.ceil(highestReleased - highestRead); +} + +function consolidateAndSortChapters(chapterList: Chapter[]): Chapter[] { + var grouped: {[index: string]:Chapter[]} = {}; + chapterList.forEach((chapter: Chapter) => { + const key = chapter.chapterNumber === '' ? chapter.title : chapter.chapterNumber; + + if (grouped[key] === undefined) { + grouped[key] = []; + } + + grouped[key].push(chapter); + }) + + var chapters: Chapter[] = []; + Object.keys(grouped).forEach((key) => { + const groupedChapters = grouped[key]; + + let chapter = groupedChapters.find((chap) => chap.read); + if (chapter === undefined) { + chapter = groupedChapters[0]; + } + + chapters.push(chapter); + }) + + return chapters.sort((a: Chapter, b: Chapter) => parseFloat(a.chapterNumber) - parseFloat(b.chapterNumber)); } From cbe2ed3784ab07c11cd08629339b9cafd2f2893e Mon Sep 17 00:00:00 2001 From: Tanveer Najib Date: Sat, 17 Feb 2024 00:06:07 +0000 Subject: [PATCH 3/5] Lint --- src/util/comparison.ts | 56 ++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/src/util/comparison.ts b/src/util/comparison.ts index e4000b9f8..f040fee3f 100644 --- a/src/util/comparison.ts +++ b/src/util/comparison.ts @@ -46,6 +46,35 @@ export function selectMostSimilarChapter(original: Chapter, options: Chapter[]): return null; } +function consolidateAndSortChapters(chapterList: Chapter[]): Chapter[] { + const grouped: { [index: string]: Chapter[] } = {}; + chapterList.forEach((chapter: Chapter) => { + const key = chapter.chapterNumber === '' ? chapter.title : chapter.chapterNumber; + + if (grouped[key] === undefined) { + grouped[key] = []; + } + + grouped[key].push(chapter); + }); + + const chapters: Chapter[] = []; + Object.keys(grouped).forEach((key) => { + const groupedChapters = grouped[key]; + + let chapter = groupedChapters.find((chap) => chap.read); + if (chapter === undefined) { + [chapter] = groupedChapters; + } + + chapters.push(chapter); + }); + + return chapters.sort( + (a: Chapter, b: Chapter) => parseFloat(a.chapterNumber) - parseFloat(b.chapterNumber) + ); +} + /** * Get the number of unread chapters from a list. * This function calculates a value using the Chapter.chapterNumber field and read status of each @@ -71,30 +100,3 @@ export function getNumberUnreadChapters(chapterList: Chapter[]): number { return Math.ceil(highestReleased - highestRead); } - -function consolidateAndSortChapters(chapterList: Chapter[]): Chapter[] { - var grouped: {[index: string]:Chapter[]} = {}; - chapterList.forEach((chapter: Chapter) => { - const key = chapter.chapterNumber === '' ? chapter.title : chapter.chapterNumber; - - if (grouped[key] === undefined) { - grouped[key] = []; - } - - grouped[key].push(chapter); - }) - - var chapters: Chapter[] = []; - Object.keys(grouped).forEach((key) => { - const groupedChapters = grouped[key]; - - let chapter = groupedChapters.find((chap) => chap.read); - if (chapter === undefined) { - chapter = groupedChapters[0]; - } - - chapters.push(chapter); - }) - - return chapters.sort((a: Chapter, b: Chapter) => parseFloat(a.chapterNumber) - parseFloat(b.chapterNumber)); -} From 71a6cf03048c59ba08a1dbeb0ae80afa5e71445b Mon Sep 17 00:00:00 2001 From: Tanveer Najib Date: Sat, 17 Feb 2024 16:36:01 +0000 Subject: [PATCH 4/5] Account for gaps in chapter numbers --- src/util/comparison.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/util/comparison.ts b/src/util/comparison.ts index f040fee3f..b1e860290 100644 --- a/src/util/comparison.ts +++ b/src/util/comparison.ts @@ -85,17 +85,30 @@ function consolidateAndSortChapters(chapterList: Chapter[]): Chapter[] { export function getNumberUnreadChapters(chapterList: Chapter[]): number { let highestRead = 0; let highestReleased = 0; + let previousChapNumber = 0; + let cumulativeGaps = 1 const chapters = consolidateAndSortChapters(chapterList); chapters.forEach((chapter: Chapter, index: number) => { - const absoluteNumber = index + 1; + let absoluteNumber = cumulativeGaps + index; + const chapterNumber = parseFloat(chapter.chapterNumber) + + const gap = Math.ceil(chapterNumber - previousChapNumber) - 1 + if (gap > 1) { + // A gap between chapters was found. Account for this in the absolute numbers + absoluteNumber += gap; + cumulativeGaps += gap; + } + if (chapter.read && absoluteNumber > highestRead) { highestRead = absoluteNumber; } if (absoluteNumber > highestReleased) { highestReleased = absoluteNumber; } + + previousChapNumber = chapterNumber; }); return Math.ceil(highestReleased - highestRead); From e762e5d45e9fa871866f42363da600a2b491698a Mon Sep 17 00:00:00 2001 From: Tanveer Najib Date: Sat, 17 Feb 2024 16:40:03 +0000 Subject: [PATCH 5/5] Use sourceId instead of title --- src/util/comparison.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/util/comparison.ts b/src/util/comparison.ts index b1e860290..982c83df5 100644 --- a/src/util/comparison.ts +++ b/src/util/comparison.ts @@ -49,7 +49,7 @@ export function selectMostSimilarChapter(original: Chapter, options: Chapter[]): function consolidateAndSortChapters(chapterList: Chapter[]): Chapter[] { const grouped: { [index: string]: Chapter[] } = {}; chapterList.forEach((chapter: Chapter) => { - const key = chapter.chapterNumber === '' ? chapter.title : chapter.chapterNumber; + const key = chapter.chapterNumber === '' ? chapter.sourceId : chapter.chapterNumber; if (grouped[key] === undefined) { grouped[key] = []; @@ -86,15 +86,15 @@ export function getNumberUnreadChapters(chapterList: Chapter[]): number { let highestRead = 0; let highestReleased = 0; let previousChapNumber = 0; - let cumulativeGaps = 1 + let cumulativeGaps = 1; const chapters = consolidateAndSortChapters(chapterList); chapters.forEach((chapter: Chapter, index: number) => { let absoluteNumber = cumulativeGaps + index; - const chapterNumber = parseFloat(chapter.chapterNumber) + const chapterNumber = parseFloat(chapter.chapterNumber); - const gap = Math.ceil(chapterNumber - previousChapNumber) - 1 + const gap = Math.ceil(chapterNumber - previousChapNumber) - 1; if (gap > 1) { // A gap between chapters was found. Account for this in the absolute numbers absoluteNumber += gap;