@@ -658,37 +658,43 @@ extension Calendar {
658
658
/// Normalized months so that all months are positive
659
659
func _normalizedMonths( _ months: [ Calendar . RecurrenceRule . Month ] , for anchor: Date ) -> [ Calendar . RecurrenceRule . Month ] {
660
660
lazy var monthRange = self . range ( of: . month, in: . year, for: anchor)
661
- return months. map { month in
661
+ return months. compactMap { month in
662
662
if month. index > 0 {
663
663
return month
664
- } else {
664
+ } else if month . index > - monthRange! . upperBound {
665
665
let newIndex = monthRange!. upperBound + month. index
666
666
// The upper bound is the last month plus one. Subtracting 1 we get the last month
667
667
return Calendar . RecurrenceRule. Month ( newIndex, isLeap: month. isLeap)
668
+ } else {
669
+ return nil
668
670
}
669
671
}
670
672
}
671
673
672
674
/// Normalized days in a month so that all days are positive
673
675
internal func _normalizedDaysOfMonth( _ days: [ Int ] , for anchor: Date ) -> [ Int ] {
674
676
lazy var dayRange = self . range ( of: . day, in: . month, for: anchor)
675
- return days. map { day in
677
+ return days. compactMap { day in
676
678
if day > 0 {
677
679
day
678
- } else {
680
+ } else if day > - dayRange! . upperBound {
679
681
dayRange!. upperBound + day
682
+ } else {
683
+ nil
680
684
}
681
685
}
682
686
}
683
687
684
688
/// Normalized days in a year so that all days are positive
685
689
internal func _normalizedDaysOfYear( _ days: [ Int ] , for anchor: Date ) -> [ Int ] {
686
690
lazy var dayRange = self . range ( of: . day, in: . year, for: anchor)
687
- return days. map { day in
691
+ return days. compactMap { day in
688
692
if day > 0 {
689
693
day
690
- } else {
694
+ } else if day > - dayRange! . upperBound {
691
695
dayRange!. upperBound + day
696
+ } else {
697
+ nil
692
698
}
693
699
}
694
700
}
@@ -709,11 +715,13 @@ extension Calendar {
709
715
weekRange. upperBound
710
716
}
711
717
712
- return weeksOfYear. map { weekIdx in
718
+ return weeksOfYear. compactMap { weekIdx in
713
719
if weekIdx > 0 {
714
720
weekIdx
715
- } else {
721
+ } else if weekIdx > - lastWeekIdx {
716
722
lastWeekIdx + weekIdx
723
+ } else {
724
+ nil
717
725
}
718
726
}
719
727
}
0 commit comments