Skip to content

Commit aadb26e

Browse files
review: update isDateRangeTheWholeMonth
1 parent 1995b3d commit aadb26e

File tree

3 files changed

+75
-36
lines changed

3 files changed

+75
-36
lines changed

src/components/DatePicker/DateRangePicker.tsx

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Body, CalendarRenderer, DateRangePickerInput, DateTableCell } from "./C
1313
import { Container } from "../Container/Container";
1414
import { Panel } from "../Panel/Panel";
1515
import { Icon } from "../Icon/Icon";
16-
import { DateRange, datesAreWithinMaxRange, selectedDateFormatter } from "./utils";
16+
import { DateRange, datesAreWithinMaxRange, isDateRangeTheWholeMonth, selectedDateFormatter } from "./utils";
1717

1818
const PredefinedCalendarContainer = styled(Panel)`
1919
align-items: start;
@@ -184,29 +184,6 @@ const monthFormatter = new Intl.DateTimeFormat(locale, {
184184
year: "numeric",
185185
});
186186

187-
const isDateRangeTheWholeMonth = ({ startDate, endDate }: DateRange): boolean => {
188-
if (startDate.getMonth() !== endDate.getMonth()) {
189-
return false;
190-
}
191-
192-
const normalizedStartDate = new Date(startDate);
193-
normalizedStartDate.setHours(0, 0, 0, 0);
194-
const normalizedEndDate = new Date(endDate);
195-
normalizedEndDate.setHours(0, 0, 0, 0);
196-
197-
const startDateIsFirstDay = normalizedStartDate.getDate() === 1;
198-
199-
const endDateNextMonth = new Date(
200-
normalizedEndDate.getFullYear(),
201-
normalizedEndDate.getMonth() + 1,
202-
1
203-
).getTime();
204-
const lastDayOfMonth = new Date(endDateNextMonth - 1).getDate();
205-
const endDateIsLastDay = normalizedEndDate.getDate() === lastDayOfMonth;
206-
207-
return startDateIsFirstDay && endDateIsLastDay;
208-
};
209-
210187
interface PredefinedDatesProps {
211188
onSelectDateRange: (selectedStartDate: Date, selectedEndDate: Date) => void;
212189
predefinedDatesList: Array<DateRange>;
Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,75 @@
1-
import { datesAreWithinMaxRange } from "./utils";
1+
import { datesAreWithinMaxRange, isDateRangeTheWholeMonth } from "./utils";
22

33
describe("DatePicker utils", () => {
44
describe("checking if two dates are fall within a range", () => {
55
it("returns true if the two dates are within the range", () => {
6-
const date1 = new Date("07-01-2025");
7-
const date2 = new Date("07-08-2025");
6+
const startDate = new Date("07-01-2025");
7+
const endDate = new Date("07-08-2025");
88

9-
expect(datesAreWithinMaxRange(date1, date2, 15)).toBeTruthy();
9+
expect(datesAreWithinMaxRange(startDate, endDate, 15)).toBeTruthy();
1010
});
1111

1212
it("returns false if the two dates are not within the range", () => {
13-
const date1 = new Date("07-01-2025");
14-
const date2 = new Date("07-31-2025");
13+
const startDate = new Date("07-01-2025");
14+
const endDate = new Date("07-31-2025");
1515

16-
expect(datesAreWithinMaxRange(date1, date2, 15)).toBeFalsy();
17-
})
16+
expect(datesAreWithinMaxRange(startDate, endDate, 15)).toBeFalsy();
17+
});
1818

1919
it("is inclusive with dates", () => {
20-
const date1 = new Date("07-01-2025");
21-
const date2 = new Date("07-16-2025");
20+
const startDate = new Date("07-01-2025");
21+
const endDate = new Date("07-16-2025");
2222

23-
expect(datesAreWithinMaxRange(date1, date2, 15)).toBeTruthy();
24-
})
23+
expect(datesAreWithinMaxRange(startDate, endDate, 15)).toBeTruthy();
24+
});
25+
});
26+
27+
describe("checking if a date range occupies an entire month", () => {
28+
it("returns false is the date range don't have the same month", () => {
29+
const startDate = new Date("07-01-2025");
30+
const endDate = new Date("08-16-2025");
31+
expect(isDateRangeTheWholeMonth({ startDate, endDate })).toBeFalsy();
32+
});
33+
34+
it("returns false is the date range starts before the first day in the same month", () => {
35+
const startDate = new Date("07-02-2025");
36+
const endDate = new Date("07-31-2025");
37+
expect(isDateRangeTheWholeMonth({ startDate, endDate })).toBeFalsy();
38+
});
39+
40+
it("returns false is the date range ends before the last day in the same month", () => {
41+
const startDate = new Date("07-01-2025");
42+
const endDate = new Date("07-30-2025");
43+
expect(isDateRangeTheWholeMonth({ startDate, endDate })).toBeFalsy();
44+
});
45+
46+
it("returns true is the date range occupies the whole month", () => {
47+
let startDate = new Date("07-01-2025");
48+
let endDate = new Date("07-31-2025");
49+
expect(isDateRangeTheWholeMonth({ startDate, endDate })).toBeTruthy();
50+
51+
startDate = new Date("08-01-2025");
52+
endDate = new Date("08-31-2025");
53+
expect(isDateRangeTheWholeMonth({ startDate, endDate })).toBeTruthy();
54+
55+
startDate = new Date("09-01-2025");
56+
endDate = new Date("09-30-2025");
57+
expect(isDateRangeTheWholeMonth({ startDate, endDate })).toBeTruthy();
58+
59+
startDate = new Date("02-01-2025");
60+
endDate = new Date("02-28-2025");
61+
expect(isDateRangeTheWholeMonth({ startDate, endDate })).toBeTruthy();
62+
});
63+
64+
it("handles leap years", () => {
65+
// 2024 was a leap year
66+
let startDate = new Date("02-01-2024");
67+
let endDate = new Date("02-29-2024");
68+
expect(isDateRangeTheWholeMonth({ startDate, endDate })).toBeTruthy();
69+
70+
startDate = new Date("02-01-2024");
71+
endDate = new Date("02-28-2024");
72+
expect(isDateRangeTheWholeMonth({ startDate, endDate })).toBeFalsy();
73+
});
2574
});
2675
});

src/components/DatePicker/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,16 @@ export const datesAreWithinMaxRange = (
5959

6060
return daysDifference <= maxRangeLength;
6161
};
62+
63+
export const isDateRangeTheWholeMonth = ({ startDate, endDate }: DateRange): boolean => {
64+
if (startDate.getMonth() !== endDate.getMonth()) {
65+
return false;
66+
}
67+
68+
const start = dayjs(startDate);
69+
const end = dayjs(endDate);
70+
const startDateIsFirstDay = start.isSame(start.startOf("month"), "day");
71+
const endDateIsLastDay = end.isSame(end.endOf("month"), "day");
72+
73+
return startDateIsFirstDay && endDateIsLastDay;
74+
};

0 commit comments

Comments
 (0)