johnpolacek / date-range-picker-for-shadcn

DateRangePicker is a reusable component for shadcn using Radix UI and Tailwind CSS. Includes preset date ranges, text entry, calendar selection and date comparison
https://date-range-picker-for-shadcn-demo.vercel.app/
MIT License
546 stars 20 forks source link

Fix Date Mutation in checkPreset Function #6

Closed wbsdickson closed 3 months ago

wbsdickson commented 7 months ago

This pull request addresses a critical issue in the checkPreset function where the original values of range.from and range.to were being unintentionally mutated. Previously, the function directly modified these Date objects by calling setHours(0, 0, 0, 0) on them. This behavior was leading to side effects, as the range object's from and to values were altered globally, affecting other parts of the application relying on these values.

Changes Made:

Modified the checkPreset function to prevent mutation of the original range.from and range.to values. Implemented creation of new Date objects (normalizedRangeFrom and normalizedRangeTo) based on the existing range.from and range.to before setting their hours to midnight. This ensures that the original range object remains unchanged after the function's execution. Impact:

The updates ensure that the checkPreset function now performs its intended check without causing unintended side effects in other parts of the application. Improves the reliability of date comparisons in the function, ensuring that the preset selection logic is more robust and error-free.

johnpolacek commented 3 months ago

Thanks!