Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add date normalization in constructors of CalendarDate classes #7436

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

1amageek
Copy link

@1amageek 1amageek commented Nov 26, 2024

Closes

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 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
Copy link
Author

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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant