Skip to content

Commit

Permalink
Merge pull request #10 from polarsquad/HoursFilter
Browse files Browse the repository at this point in the history
Added TimeEntries.Filter function
  • Loading branch information
mikat-polarsquad authored Oct 2, 2019
2 parents fdeb00b + 1c222cb commit a8a838c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
44 changes: 44 additions & 0 deletions hours.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package harvest

import (
"github.com/polarsquad/harvest/structs"
_ "log"
"time"
)
Expand All @@ -21,6 +22,16 @@ func (h *Harvest) TotalHours() float64 {
return hours
}

// Total counts total logged hours from the TimeEntries struct
func (e TimeEntries) Total() float64 {
var hours float64
for _, v := range e.Entries {
hours = hours + v.Hours
}
// fmt.Printf("Total hours: %v\n", hours)
return hours
}

// DailyHours counts total logged in hours for selected date.
// Needs daySelector(time.Time) as parameter for selected date.
func (e *TimeEntries) DailyHours(daySelector time.Time) float64 {
Expand Down Expand Up @@ -56,6 +67,39 @@ func isWorkday(date time.Time) bool { // Should this be placed in helpers.go?
return false
}

type entries structs.Entries

// Filter is generic function to filter Entries
func (e *TimeEntries) Filter(f func(structs.Entries) bool) (ret []structs.Entries) {
// var r []structs.Entries
for _, v := range e.Entries {
if f(v) == true {
ret = append(ret, v)
}
}
return
}

// func (e *TimeEntries) Filter(f func(structs.Entries) bool) []structs.Entries {
// var r Entries
// for _, v := range e.Entries {
// if f(v) == true {
// r = append(r, v)
// }
// }
// return r
// }

// func filter(s []student, f func(student) bool) []student {
// var r []student
// for _, v := range s {
// if f(v) == true {
// r = append(r, v)
// }
// }
// return r
// }

// func (t *TimeEntries) Choose(selector string, test func(string) bool) (ret []structs.Entries) {
// for _, v := range t.Entries {
// if test(s) {
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ type Harvest struct {
TimeEntries *TimeEntries
}

type api structs.API
// type api structs.API

// TimeEntries ...
type TimeEntries structs.TimeEntries

// Entries ...
type Entries []structs.Entries

// type entries structs.Entries

// User ...
type User structs.User

Expand Down

0 comments on commit a8a838c

Please sign in to comment.