An open source design system that's fully customizable and agent ready
TypeScript
fix(utils): clamp plainDateAddMonths to the target month's last day (#3893)
Shahrier Islam Arham5b928d1f864d
diff --git a/.changeset/plaindate-addmonths-clamp.md b/.changeset/plaindate-addmonths-clamp.mdnew file mode 100644index 0000000000..2b8adb6d37--- /dev/null+++ b/.changeset/plaindate-addmonths-clamp.md@@ -0,0 +1,8 @@+---+'@astryxdesign/core': patch+---++[fix] utils: clamp plainDateAddMonths to the target month's last day++Adding a month to Jan 31 landed on Mar 3 (Date#setMonth overflow) instead of Feb 28, so month arithmetic from end-of-month dates skipped February entirely. The helper now uses pure month arithmetic and clamps the day, matching Temporal.PlainDate.add and date-fns.+@arham766diff --git a/packages/core/src/utils/plainDate.test.ts b/packages/core/src/utils/plainDate.test.tsindex c0ad3d8aa4..4a3cc298c9 100644--- a/packages/core/src/utils/plainDate.test.ts+++ b/packages/core/src/utils/plainDate.test.ts@@ -235,19 +235,37 @@ describe('plainDateAddMonths', () => {});});- it('overflows day into next month when target month is shorter', () => {+ it('clamps to the last day when the target month is shorter', () => {+ // Jan 31 + 1 month lands in February (clamped), not overflowed into+ // March — matching Temporal.PlainDate.add and date-fns addMonths.expect(plainDateAddMonths({year: 2026, month: 1, day: 31}, 1)).toEqual({year: 2026,- month: 3,- day: 3,+ month: 2,+ day: 28,});});- it('overflows Jan 31 + 1 month in leap year', () => {+ it('clamps to Feb 29 in a leap year', () => {expect(plainDateAddMonths({year: 2024, month: 1, day: 31}, 1)).toEqual({year: 2024,- month: 3,- day: 2,+ month: 2,+ day: 29,+ });+ });++ it('clamps when subtracting into a shorter month', () => {+ expect(plainDateAddMonths({year: 2026, month: 3, day: 31}, -1)).toEqual({+ year: 2026,+ month: 2,+ day: 28,+ });+ });++ it('clamps a 31st onto a 30-day month', () => {+ expect(plainDateAddMonths({year: 2026, month: 5, day: 31}, 1)).toEqual({+ year: 2026,+ month: 6,+ day: 30,});});});diff --git a/packages/core/src/utils/plainDate.ts b/packages/core/src/utils/plainDate.tsindex e1389133ad..ab369c0ca7 100644--- a/packages/core/src/utils/plainDate.ts+++ b/packages/core/src/utils/plainDate.ts@@ -72,9 +72,14 @@ export function plainDateDayOfWeek(pd: PlainDate): number {}export function plainDateAddMonths(pd: PlainDate, n: number): PlainDate {- const d = plainDateToDate(pd);- d.setMonth(d.getMonth() + n);- return plainDateFromDate(d);+ // Pure month arithmetic with day clamping (Temporal.PlainDate.add+ // semantics): Jan 31 + 1 month is Feb 28/29, not Mar 2/3. Date#setMonth+ // would overflow the excess days into the following month instead.+ const totalMonths = pd.year * 12 + (pd.month - 1) + n;+ const year = Math.floor(totalMonths / 12);+ const month = (((totalMonths % 12) + 12) % 12) + 1;+ const day = Math.min(pd.day, getDaysInMonth(year, month));+ return {year, month, day};}export function plainDateAddDays(pd: PlainDate, n: number): PlainDate {