Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { weekRange as weekRange2 } from "https://esm.town/v/andreterron/weekRange";
import { WorkoutIcon } from "https://esm.town/v/andreterron/workoutIcons";
import { formatInTimeZone, utcToZonedTime } from "npm:date-fns-tz";
import { addDays, format } from "npm:date-fns@2.30.0";
export function weekWorkoutIcons(
byDay: Record<string, any>,
timezone: string = "America/Los_Angeles",
): WorkoutIcon[] {
const todayKey = formatInTimeZone(
new Date(),
timezone,
"yyyy-MM-dd",
);
const weekRange = weekRange2(timezone);
let workoutsThisWeek = 0;
return weekRange.map((day, i): WorkoutIcon => {
const key = format(day, "yyyy-MM-dd");
const previousKey = format(addDays(day, -1), "yyyy-MM-dd");
const workedOut = !!byDay[key];
const past = key < todayKey;
// Returns a "star" (medal) if this is this week has 5 or more workouts
if (workedOut) {
workoutsThisWeek += 1;
return workoutsThisWeek >= 5 ? "star" : "done";
}
// Never skip twice
if (past && !workedOut && !byDay[previousKey]) return "failed";
if (past) return "skipped";
if (key === todayKey) return "today";
return "future";
});
}
September 12, 2024