Skip to content

Commit

Permalink
Account for decimals in unread chapter count (#335)
Browse files Browse the repository at this point in the history
* Account for decimals in unread chapter count

This removes reliance on Chapter.chapterNumber (which is nullable)

* Account for almost all considerations for unread chapter count

* Lint

* Account for gaps in chapter numbers

* Use sourceId instead of title
  • Loading branch information
crxssed7 authored Feb 22, 2024
1 parent 07a62d5 commit 509f509
Showing 1 changed file with 49 additions and 5 deletions.
54 changes: 49 additions & 5 deletions src/util/comparison.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.sourceId : 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
Expand All @@ -56,15 +85,30 @@ export function selectMostSimilarChapter(original: Chapter, options: Chapter[]):
export function getNumberUnreadChapters(chapterList: Chapter[]): number {
let highestRead = 0;
let highestReleased = 0;
let previousChapNumber = 0;
let cumulativeGaps = 1;

chapterList.forEach((chapter: Chapter) => {
const chapters = consolidateAndSortChapters(chapterList);

chapters.forEach((chapter: Chapter, index: number) => {
let absoluteNumber = cumulativeGaps + index;
const chapterNumber = parseFloat(chapter.chapterNumber);
if (chapter.read && chapterNumber > highestRead) {
highestRead = 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 (chapterNumber > highestReleased) {
highestReleased = chapterNumber;
if (absoluteNumber > highestReleased) {
highestReleased = absoluteNumber;
}

previousChapNumber = chapterNumber;
});

return Math.ceil(highestReleased - highestRead);
Expand Down

0 comments on commit 509f509

Please sign in to comment.