diff options
| author | Fuwn <[email protected]> | 2023-09-10 02:01:23 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-09-10 02:01:23 -0700 |
| commit | e5bb4dd56192cf74b018ba8259e90df4a6d9e41d (patch) | |
| tree | 2c6edbecf07c6651449a269e0f45bed44138c5b1 /src/lib | |
| parent | feat(settings): add feedback message (diff) | |
| download | due.moe-e5bb4dd56192cf74b018ba8259e90df4a6d9e41d.tar.xz due.moe-e5bb4dd56192cf74b018ba8259e90df4a6d9e41d.zip | |
refactor(anime): generalise airing time
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/List/Due/AnimeList.svelte | 32 | ||||
| -rw-r--r-- | src/lib/List/UpcomingAnimeList.svelte | 38 | ||||
| -rw-r--r-- | src/lib/anime.ts | 48 |
3 files changed, 51 insertions, 67 deletions
diff --git a/src/lib/List/Due/AnimeList.svelte b/src/lib/List/Due/AnimeList.svelte index e65ac965..74c48fef 100644 --- a/src/lib/List/Due/AnimeList.svelte +++ b/src/lib/List/Due/AnimeList.svelte @@ -7,6 +7,7 @@ import anime from '../../../stores/anime'; import settings from '../../../stores/settings'; import lastPruneTimes from '../../../stores/lastPruneTimes'; + import { airingTime } from '$lib/anime'; export let user: AniListAuthorisation; export let identity: UserIdentity; @@ -104,37 +105,6 @@ return finalMedia; }; - const airingTime = (anime: Media) => { - const untilAiring = anime.nextAiringEpisode?.timeUntilAiring; - let timeFrame; - - if (untilAiring !== undefined) { - let hours = untilAiring / 3600; - - if (hours >= 24) { - let weeks = Math.floor(Math.floor(hours / 24) / 7); - - if (weeks >= 1) { - weeks = Math.round(weeks); - - timeFrame = `${weeks} week${weeks === 1 ? '' : 's'}`; - } else { - const days = Math.round(Math.floor(hours / 24)); - - timeFrame = `${days} day${days === 1 ? '' : 's'}`; - } - } else { - hours = Math.round(hours); - - timeFrame = `${hours} hour${hours === 1 ? '' : 's'}`; - } - - return `<span style="opacity: 50%">${anime.nextAiringEpisode?.episode} in ${timeFrame}</span>`; - } - - return ''; - }; - const totalEpisodes = (anime: Media) => { return anime.episodes === null ? '' : `<span style="opacity: 50%">/${anime.episodes}</span>`; }; diff --git a/src/lib/List/UpcomingAnimeList.svelte b/src/lib/List/UpcomingAnimeList.svelte index 679a38d7..7cac01fc 100644 --- a/src/lib/List/UpcomingAnimeList.svelte +++ b/src/lib/List/UpcomingAnimeList.svelte @@ -7,6 +7,7 @@ import anime from '../../stores/anime'; import lastPruneTimes from '../../stores/lastPruneTimes'; import settings from '../../stores/settings'; + import { airingTime } from '$lib/anime'; export let user: AniListAuthorisation; export let identity: UserIdentity; @@ -72,41 +73,6 @@ return finalMedia; }; - const airingTime = (anime: Media) => { - const untilAiring = anime.nextAiringEpisode?.timeUntilAiring; - let timeFrame; - - if (untilAiring !== undefined) { - let hours = untilAiring / 3600; - - if (hours >= 24) { - let weeks = Math.floor(Math.floor(hours / 24) / 7); - - if (weeks >= 1) { - weeks = Math.round(weeks); - - timeFrame = `${weeks} week${weeks === 1 ? '' : 's'}`; - } else { - const days = Math.round(Math.floor(hours / 24)); - - timeFrame = `${days} day${days === 1 ? '' : 's'}`; - } - } else { - hours = Math.round(hours); - - timeFrame = `${hours} hour${hours === 1 ? '' : 's'}`; - } - - return `${anime.nextAiringEpisode?.episode}${totalEpisodes(anime)} in ${timeFrame}`; - } - - return ''; - }; - - const totalEpisodes = (anime: Media) => { - return anime.episodes === null ? '' : `<span style="opacity: 50%">/${anime.episodes}</span>`; - }; - const cleanCache = () => { animeLists = mediaListCollection( user, @@ -151,7 +117,7 @@ {anime.title.english || anime.title.romaji || anime.title.native} </a> <span style="opacity: 50%;">|</span> - {@html airingTime(anime)} + {@html airingTime(anime, true)} </li> {/each} </ul> diff --git a/src/lib/anime.ts b/src/lib/anime.ts new file mode 100644 index 00000000..5431a230 --- /dev/null +++ b/src/lib/anime.ts @@ -0,0 +1,48 @@ +import type { Media } from './AniList/media'; + +const totalEpisodes = (anime: Media) => { + return anime.episodes === null ? '' : `<span style="opacity: 50%">/${anime.episodes}</span>`; +}; + +export const airingTime = (anime: Media, upcoming = false) => { + const untilAiring = anime.nextAiringEpisode?.timeUntilAiring; + let timeFrame; + + if (untilAiring !== undefined) { + let minutes = untilAiring / 60; + + if (minutes >= 30) { + let hours = minutes / 60; + + if (hours >= 24) { + let weeks = Math.floor(Math.floor(hours / 24) / 7); + + if (weeks >= 1) { + weeks = Math.round(weeks); + + timeFrame = `${weeks} week${weeks === 1 ? '' : 's'}`; + } else { + const days = Math.round(Math.floor(hours / 24)); + + timeFrame = `${days} day${days === 1 ? '' : 's'}`; + } + } else { + hours = Math.round(hours); + + timeFrame = `${hours} hour${hours === 1 ? '' : 's'}`; + } + } else { + minutes = Math.round(minutes); + + timeFrame = `${minutes} minute${minutes === 1 ? '' : 's'}`; + } + + if (upcoming) { + return `${anime.nextAiringEpisode?.episode}${totalEpisodes(anime)} in ${timeFrame}`; + } else { + return `<span style="opacity: 50%">${anime.nextAiringEpisode?.episode} in ${timeFrame}</span>`; + } + } + + return ''; +}; |