Skip to content

Commit

Permalink
Fixes incomplete parsing of BYDAY rules when prefixed with integers. F…
Browse files Browse the repository at this point in the history
…ixes teambition#16.
  • Loading branch information
nobre84 committed Jan 31, 2020
1 parent cd9d6f3 commit 39eab1e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 13 additions & 8 deletions Sources/RRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,14 @@ public struct RRule {
if ruleName == "BYDAY" {
// These variables will define the weekdays where the recurrence will be applied.
// In the RFC documentation, it is specified as BYDAY, but was renamed to avoid the ambiguity of that argument.
let byweekday = ruleValue.components(separatedBy: ",").compactMap({ (string) -> EKWeekday? in
return EKWeekday.weekdayFromSymbol(string)
let byweekday = ruleValue.components(separatedBy: ",").compactMap({ (string) -> (Int?, EKWeekday)? in
guard let parsedSymbol = EKWeekday.weekdayFromSymbol(String(string.suffix(2))) else { return nil }
let specific = Int(string.prefix(string.count - 2))
return (specific, parsedSymbol)
})
recurrenceRule.byweekday = byweekday.sorted(by: { (a, b) -> Bool in
return a.1 < b.1
})
recurrenceRule.byweekday = byweekday.sorted(by: <)
}

if ruleName == "BYHOUR" {
Expand Down Expand Up @@ -245,11 +249,12 @@ public struct RRule {
rruleString += "BYMONTHDAY=\(bymonthdayStrings.joined(separator: ","));"
}

let byweekdaySymbols = rule.byweekday.map({ (weekday) -> String in
return weekday.toSymbol()
})
if byweekdaySymbols.count > 0 {
rruleString += "BYDAY=\(byweekdaySymbols.joined(separator: ","));"
let byweekdayElements = rule.byweekday.map { weekdayTuple -> String in
let modifier = weekdayTuple.0.flatMap { "\($0)" } ?? ""
return "\(modifier)\(weekdayTuple.1.toSymbol())"
}
if byweekdayElements.count > 0 {
rruleString += "BYDAY=\(byweekdayElements.joined(separator: ","));"
}

let byhourStrings = rule.byhour.map({ (hour) -> String in
Expand Down
2 changes: 1 addition & 1 deletion Sources/RecurrenceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public struct RecurrenceRule {
public var bymonthday = [Int]()

/// The days of the week associated with the recurrence rule, as an array of EKWeekday objects.
public var byweekday = [EKWeekday]()
public var byweekday = [(Int?, EKWeekday)]()

/// The hours of the day associated with the recurrence rule, as an array of integers.
public var byhour = [Int]()
Expand Down

0 comments on commit 39eab1e

Please sign in to comment.