Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added TimeEntries.Filter function #10

Merged
merged 1 commit into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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