Skip to content

Commit 8402475

Browse files
committed
feat(schedule): prepare schedule generator
1 parent 193de35 commit 8402475

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/lib/schedule.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import dayjs from "dayjs";
2+
3+
// Schedules must be started on Monday, Wednesday or Friday
4+
function calcFirstDate(start: dayjs.Dayjs): dayjs.Dayjs {
5+
switch (start.day()) {
6+
case 0:
7+
case 2:
8+
case 4:
9+
return start.add(1, "d");
10+
case 1:
11+
case 3:
12+
case 5:
13+
return start;
14+
case 6:
15+
return start.add(2, "d");
16+
}
17+
}
18+
19+
/**
20+
Generates scheduled dates only on Monday, Wednesday and Friday
21+
*/
22+
export function* generate({
23+
start,
24+
}: {
25+
start?: dayjs.Dayjs;
26+
}): Generator<dayjs.Dayjs, void, unknown> {
27+
let initialDay = calcFirstDate(start ?? dayjs());
28+
29+
let cur = initialDay;
30+
while (true) {
31+
yield cur;
32+
cur = cur.add(cur.day() >= 4 ? 3 : 2, "d");
33+
}
34+
}

0 commit comments

Comments
 (0)