adobe / react-spectrum

A collection of libraries and tools that help you build adaptive, accessible, and robust user experiences.
https://react-spectrum.adobe.com
Apache License 2.0
13.1k stars 1.14k forks source link

Add date normalization in constructors of CalendarDate classes #7436

Open 1amageek opened 3 days ago

1amageek commented 3 days ago

Closes

✅ Pull Request Checklist:

📝 Test Instructions:

Verify constructor normalizes dates correctly:

// Date overflow should normalize to next month
new CalendarDate(2024, 1, 32)           // -> 2024-02-01

// Hours overflow should normalize to next day
new CalendarDateTime(2024, 1, 1, 26)    // -> 2024-01-02T02:00:00 

// Complex overflow should normalize all fields
new CalendarDateTime(2024, 1, 1, 26, 70) // -> 2024-01-02T03:10:00

// Month overflow should normalize to next year
new ZonedDateTime(2024, 13, 1, 'UTC', 0) // -> 2025-01-01T00:00:00Z
1amageek commented 3 days ago

Major Changes: Refactor Calendar Date Interfaces

Problem

  1. Including the copy() method in AnyCalendarDate interface caused data handling issues:

    • Mixing data structure with behavior
    • Implicit object construction during date normalization
    • Recursive constructor calls during normalization process
  2. When normalizing dates, CalendarDate constructor was being called recursively due to the object spread operator, making the process unnecessarily complex and prone to side effects.

Solution

  1. Separated concerns by:

    • Moving copy() method to a new Copyable interface
    • Keeping AnyCalendarDate as a pure data structure interface
    • Making data manipulation explicit through pure functions
  2. New interface structure:

    
    export interface AnyCalendarDate {
    readonly calendar: Calendar,
    readonly era: string,
    readonly year: number,
    readonly month: number,
    readonly day: number
    }

export interface AnyTime { readonly hour: number, readonly minute: number, readonly second: number, readonly millisecond: number }

export interface Copyable { copy(): this }