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
import { weekRange as weekRange2 } from "https://esm.town/v/andreterron/weekRange";
import { WorkoutIcon } from "https://esm.town/v/andreterron/workoutIcons";
import { addDays, format } from "npm:date-fns@2.30.0";
import { formatInTimeZone, utcToZonedTime } from "npm:date-fns-tz";
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);
return weekRange.map((day): 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;
if (workedOut) return "done";
// Never skip twice
if (past && !workedOut && !byDay[previousKey]) return "failed";
if (past) return "skipped";
if (key === todayKey) return "today";
return "future";
});
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v4
December 19, 2023