An open source design system that's fully customizable and agent ready
TypeScript
fix(core): a month past the end of its year constrains to the last month (#10461)
Aashish Mahato24d845b42040
diff --git a/.changeset/a-month-position-past-the-end-of-a-gregorian-ali-efi9.md b/.changeset/a-month-position-past-the-end-of-a-gregorian-ali-efi9.mdnew file mode 100644index 0000000000..730289d95c--- /dev/null+++ b/.changeset/a-month-position-past-the-end-of-a-gregorian-ali-efi9.md@@ -0,0 +1,6 @@+---+'@astryxdesign/core': patch+---++[fix] a month position past the end of a Gregorian-aligned calendar's year constrains to that year's last month in calendarMonthFromYearMonth and useCalendarDays, instead of returning a NaN CalendarMonth and laying out the next year's January (#10461)+@aashish254diff --git a/packages/core/src/Calendar/hooks/calendarHooks.calendarSystems.test.tsx b/packages/core/src/Calendar/hooks/calendarHooks.calendarSystems.test.tsxindex aa8fa42381..80ed74d0cd 100644--- a/packages/core/src/Calendar/hooks/calendarHooks.calendarSystems.test.tsx+++ b/packages/core/src/Calendar/hooks/calendarHooks.calendarSystems.test.tsx@@ -414,4 +414,23 @@ describe('the calendar hooks without Temporal', () => {});expect(warn).not.toHaveBeenCalled();});++ it('constrains a month past the end of the fallback year to its last month', () => {+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});+ withoutTemporal(() => {+ const past = renderHook(() =>+ useCalendarDays({year: 5784, month: 13, calendar: 'hebrew'}),+ );+ expect(past.result.current.days).toEqual(+ releasedDays(5784, 12, 0, false),+ );+ const gregorian = renderHook(() =>+ useCalendarDays({year: 2026, month: 13}),+ );+ expect(gregorian.result.current.days).toEqual(+ releasedDays(2026, 12, 0, false),+ );+ });+ expect(warn).toHaveBeenCalledTimes(1);+ });});diff --git a/packages/core/src/utils/calendarSystem.test.ts b/packages/core/src/utils/calendarSystem.test.tsindex e7ec5a3ab2..3131ea7cc3 100644--- a/packages/core/src/utils/calendarSystem.test.ts+++ b/packages/core/src/utils/calendarSystem.test.ts@@ -9,6 +9,7 @@ import {calendarAddMonths,calendarDateFromFields,calendarDayOfMonth,+ calendarMonthFromYearMonth,calendarMonthOf,calendarMonthsInYear,calendarShiftMonths,@@ -26,6 +27,7 @@ import {import {DATE_FORMAT_WITH_WEEKDAY,formatSharedDate,+ getDaysInMonth,type PlainDate,plainDateAddDays,plainDateFormat,@@ -238,6 +240,62 @@ describe('calendar month and year steps', () => {});});+describe('calendarMonthFromYearMonth past the end of its year', () => {+ const alignedYears: [CalendarSystem, number][] = [+ ['gregory', 2026],+ ['iso8601', 2026],+ ['buddhist', 2569],+ ['roc', 115],+ ['japanese', 2026],+ ];++ it.each(alignedYears)(+ '%s constrains a month past December to December',+ (calendar, year) => {+ const december = calendarMonthFromYearMonth(calendar, {year, month: 12});+ for (const month of [13, 14, 99]) {+ expect(calendarMonthFromYearMonth(calendar, {year, month})).toEqual(+ december,+ );+ }+ expect(december).toEqual({+ start: {year: 2026, month: 12, day: 1},+ end: {year: 2026, month: 12, day: 31},+ length: 31,+ });+ },+ );++ it.each(alignedYears)(+ '%s keeps every in-range month and its year offset',+ (calendar, year) => {+ for (const month of [1, 2, 6, 12]) {+ const built = calendarMonthFromYearMonth(calendar, {year, month});+ expect(built.start).toEqual({year: 2026, month, day: 1});+ expect(built.length).toBe(getDaysInMonth(2026, month));+ expect(built.end).toEqual({+ year: 2026,+ month,+ day: getDaysInMonth(2026, month),+ });+ }+ },+ );++ it('constrains a Hebrew month past the end of its year, leap or not', () => {+ const elul = calendarMonthFromYearMonth('hebrew', {year: 5784, month: 13});+ expect(+ calendarMonthFromYearMonth('hebrew', {year: 5784, month: 14}),+ ).toEqual(elul);+ expect(+ calendarMonthFromYearMonth('hebrew', {year: 5784, month: 99}),+ ).toEqual(elul);+ expect(+ calendarMonthFromYearMonth('hebrew', {year: 5783, month: 13}),+ ).toEqual(calendarMonthFromYearMonth('hebrew', {year: 5783, month: 12}));+ });+});+describe('calendar availability', () => {it('every calendar is available with the full polyfill', () => {for (const calendar of CALENDAR_SYSTEMS) {diff --git a/packages/core/src/utils/calendarSystem.ts b/packages/core/src/utils/calendarSystem.tsindex 5c2a2775de..3c76fe5150 100644--- a/packages/core/src/utils/calendarSystem.ts+++ b/packages/core/src/utils/calendarSystem.ts@@ -271,9 +271,15 @@ export function calendarMonthFromYearMonth({year, month}: CalendarYearMonth,): CalendarMonth {if (isGregorianAligned(calendar)) {+ // A Gregorian year has 12 months. Past it, `getDaysInMonth` answers+ // undefined, so constrain here as the Temporal branch below already does.+ const monthOfYear = Math.min(+ month,+ calendarMonthsInCalendarYear(calendar, year),+ );return gregorianMonth(year - (GREGORIAN_YEAR_OFFSETS[calendar] ?? 0),- month,+ monthOfYear,);}// `with` replaces the year and month of any date in the calendar.