Simplify Calendar.day (now read-only)#182
Conversation
| new Day(new Date(2025, 0, 1), 10), | ||
| ]); | ||
| calendar.day(new Date(2024, 11, 30)).contributionCount = 3; | ||
| calendar.day(new Date(2024, 11, 30))!.contributionCount = 3; |
There was a problem hiding this comment.
The ! here would surface as a TypeError: Cannot set properties of undefined rather than an AssertionError if day() ever returned undefined. Consider using assertExists to get a clearer failure message:
| calendar.day(new Date(2024, 11, 30))!.contributionCount = 3; | |
| const dec30 = calendar.day(new Date(2024, 11, 30)); | |
| assertExists(dec30); | |
| dec30.contributionCount = 3; |
There was a problem hiding this comment.
Addressed — the update uses assertExists + a named variable, matching the suggestion exactly.
| new Day(new Date(2025, 0, 1), 10), | ||
| ]); | ||
| calendar.day(new Date(2025, 0, 3)).contributionCount = 3; | ||
| calendar.day(new Date(2025, 0, 3))!.contributionCount = 3; |
There was a problem hiding this comment.
Same ! concern as the previous test — a regression would throw TypeError instead of AssertionError.
| calendar.day(new Date(2025, 0, 3))!.contributionCount = 3; | |
| const jan3 = calendar.day(new Date(2025, 0, 3)); | |
| assertExists(jan3); | |
| jan3.contributionCount = 3; |
There was a problem hiding this comment.
Addressed — same assertExists pattern applied here too.
| const dayAgain = calendar.day(wednesday); | ||
| assertStrictEquals(day, dayAgain); | ||
| assertEquals(dayAgain.contributionCount, 9); | ||
| assertEquals(calendar.day(new Date(2025, 0, 1)), undefined); |
There was a problem hiding this comment.
This only covers the empty-calendar case. There's no test for day() returning undefined when the calendar is non-empty but the requested date is out of range. Something like:
Deno.test("Calendar.day() returns undefined when date is out of range", () => {
const calendar = new Calendar("testuser", [new Day(new Date(2025, 0, 1), 5)]);
assertEquals(calendar.day(new Date(2025, 1, 1)), undefined);
});There was a problem hiding this comment.
Addressed — "Calendar.day() should return undefined when out of range" covers exactly this case.
|
Clean simplification overall — removing the day-creation side effect from A few minor notes (details inline):
|
`Calendar.day` is only really used by tests. This radically simplifies it by removing its ability to create `Day`s, so it is now only used to access a `Day` by date. This also removes the unused `Calendar.existingRepoDay` and makes `Calendar.normalizeDays` and `Calendar.repoDayForDay` private. This removes a bunch of tests related to calendar modification and `normalizeDays`, since we now expect calendar ranges to be pre-declared, and `normalizeDays` will be simplified in a future change (it’s only used for testing).
|
All three issues from my prior review are addressed: the two non-null assertion uses replaced with assertExists, and the out-of-range test added. LGTM. |
Calendar.dayis only really used by tests. This radically simplifiesit by removing its ability to create
Days, so it is now only used toaccess a
Dayby date.This also removes the unused
Calendar.existingRepoDayand makesCalendar.normalizeDaysandCalendar.repoDayForDayprivate.This removes a bunch of tests related to calendar modification and
normalizeDays, since we now expect calendar ranges to be pre-declared,and
normalizeDayswill be simplified in a future change (it’s onlyused for testing).