Skip to content

Commit

Permalink
include sorting for days of week
Browse files Browse the repository at this point in the history
  • Loading branch information
jh-bate committed Nov 8, 2023
1 parent 589783f commit 96d7de6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
34 changes: 34 additions & 0 deletions data/types/common/day.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,37 @@ func DaysOfWeek() []string {
DaySaturday,
}
}

type DaysOfWeekByDayIndex []string

func (d DaysOfWeekByDayIndex) Len() int {
return len(d)
}
func (d DaysOfWeekByDayIndex) Swap(i int, j int) {
d[i], d[j] = d[j], d[i]
}

func (d DaysOfWeekByDayIndex) Less(i int, j int) bool {
return DayIndex(d[i]) < DayIndex(d[j])
}

func DayIndex(day string) int {
switch day {
case DaySunday:
return 1
case DayMonday:
return 2
case DayTuesday:
return 3
case DayWednesday:
return 4
case DayThursday:
return 5
case DayFriday:
return 6
case DaySaturday:
return 7
default:
return 0
}
}
24 changes: 24 additions & 0 deletions data/types/common/day_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,28 @@ var _ = Describe("Day", func() {
common.DaySaturday,
}))
})

Context("DayIndex", func() {
DescribeTable("return the expected index when the day",
func(day string, expectedIndex int) {
Expect(common.DayIndex(day)).To(Equal(expectedIndex))
},
Entry("is an empty string", "", 0),
Entry("is sunday", "sunday", 1),
Entry("is constant sunday", common.DaySunday, 1),
Entry("is monday", "monday", 2),
Entry("is constant monday", common.DayMonday, 2),
Entry("is tuesday", "tuesday", 3),
Entry("is constant tuesday", common.DayTuesday, 3),
Entry("is wednesday", "wednesday", 4),
Entry("isconstant wednesday", common.DayWednesday, 4),
Entry("is thursday", "thursday", 5),
Entry("is constant thursday", common.DayThursday, 5),
Entry("is friday", "friday", 6),
Entry("is constant friday", common.DayFriday, 6),
Entry("is saturday", "saturday", 7),
Entry("is constant saturday", common.DaySaturday, 7),
Entry("is an invalid string", "invalid", 0),
)
})
})

0 comments on commit 96d7de6

Please sign in to comment.