diff options
| author | Fuwn <[email protected]> | 2023-09-27 23:33:01 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-09-27 23:33:01 -0700 |
| commit | 8562ba4280c575b3f04df598b7954a2d28b19e50 (patch) | |
| tree | eaa43530441522b5104a1fd1e404ce6b663fc9b0 /src/lib/AniList/activity.ts | |
| parent | fix(anime): template increment render (diff) | |
| download | due.moe-8562ba4280c575b3f04df598b7954a2d28b19e50.tar.xz due.moe-8562ba4280c575b3f04df598b7954a2d28b19e50.zip | |
feat(wrapped): initial wrapped prototype
Diffstat (limited to 'src/lib/AniList/activity.ts')
| -rw-r--r-- | src/lib/AniList/activity.ts | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/lib/AniList/activity.ts b/src/lib/AniList/activity.ts index bd4bafc0..df150c96 100644 --- a/src/lib/AniList/activity.ts +++ b/src/lib/AniList/activity.ts @@ -5,6 +5,45 @@ export interface ActivityHistoryEntry { amount: number; } +export const fillMissingDays = ( + inputActivities: ActivityHistoryEntry[], + startOfYear = false +): ActivityHistoryEntry[] => { + const timezoneOffset = new Date().getTimezoneOffset() * 60 * 1000; + const activities = inputActivities; + const firstDate = startOfYear + ? new Date(new Date().getFullYear(), 0, 1, 0, 0, 0, 0) + : new Date(activities[0].date * 1000 + timezoneOffset); + const lastDate = new Date(activities[activities.length - 1].date * 1000 + timezoneOffset); + const currentDate = firstDate; + + while (currentDate <= lastDate) { + const current_unix_timestamp = currentDate.getTime(); + let found = false; + + for (let i = 0; i < activities.length; i++) { + if (activities[i].date * 1000 + timezoneOffset === current_unix_timestamp) { + found = true; + + break; + } + } + + if (!found) { + activities.push({ + date: current_unix_timestamp / 1000, + amount: 0 + }); + } + + currentDate.setDate(currentDate.getDate() + 1); + } + + // activities.sort((a: { date: number }, b: { date: number }) => a.date - b.date); + + return activities; +}; + export const activityHistory = async ( userIdentity: UserIdentity ): Promise<ActivityHistoryEntry[]> => { |