-
Notifications
You must be signed in to change notification settings - Fork 45
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0c2029a
Account for decimals in unread chapter count
crxssed7 1fa674c
Account for almost all considerations for unread chapter count
crxssed7 cbe2ed3
Lint
crxssed7 71a6cf0
Account for gaps in chapter numbers
crxssed7 e762e5d
Use sourceId instead of title
crxssed7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: you could avoid needing to increment both here and only set There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:There was a problem hiding this comment.
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 🤷🏾