Skip to content

Commit 9fa47d6

Browse files
committed
fix(date-picker): fix error on invalid date when calling isSameDay
1 parent b8cc29a commit 9fa47d6

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

packages/lumx-react/src/utils/date/isSameDay.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,17 @@ describe(isSameDay, () => {
2121

2222
it('should handle different years', () => {
2323
const date1 = new Date('2022-12-25T18:30:00');
24-
const date2 = new Date('2023-12-25T18:30:00');
24+
const date2 = new Date('1923-12-25T18:30:00');
2525
expect(isSameDay(date1, date2)).toBe(false);
2626
});
27+
28+
it('should handle invalid date', () => {
29+
// Invalid date input are not comparable, so we always return `false`
30+
// Undefined date
31+
expect(isSameDay(undefined as any, undefined as any)).toBe(false);
32+
// Null date
33+
expect(isSameDay(null as any, new Date())).toBe(false);
34+
// Invalid date
35+
expect(isSameDay(new Date('-'), new Date('-'))).toBe(false);
36+
});
2737
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import { isDateValid } from '@lumx/react/utils/date/isDateValid';
2+
13
/**
24
* Check `date1` is on the same day as `date2`.
35
*/
46
export const isSameDay = (date1: Date, date2: Date) =>
7+
isDateValid(date1) &&
8+
isDateValid(date2) &&
59
date1.getFullYear() === date2.getFullYear() &&
610
date1.getMonth() === date2.getMonth() &&
711
date1.getDate() === date2.getDate();

0 commit comments

Comments
 (0)