Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for decimals in unread chapter count #335

Merged
merged 5 commits into from
Feb 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Comment on lines +65 to +68
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The [chapter] assignment here looks pretty weird, it seems to work but I don't really understand why. I think this section is equivalent to just:

let chapter = groupedChapters.find(chap => chap.read) || groupedChapters[0];

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I did this was because the linter kept complaining at me, so I assumed it was because of the project level linter config 😅

In hindsight it was probably just my VSCode extension 🤷🏾


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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you could avoid needing to increment both here and only set absoluteNumber after this block (since it's not used yet)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh yes, didn't catch that

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