Skip to content

Commit

Permalink
feat(utils.go): parametrize CurrentWeekEntries
Browse files Browse the repository at this point in the history
The function will now accept a time.Time value as param.
This should improve testability, and make allow us to extend functionality

refactor(utils.go): rename `CurrentWeekEntries` to make the intention more clear

as the function has been changed to be parametrized it would now be possible to use it fetch entries
from any week given a date, so this renaming will try to make that more clear

BREAKING CHANGE: as both the signature and name of the function
changed, any code calling it should be updated
  • Loading branch information
deadpyxel committed Nov 10, 2023
1 parent 1f0a06f commit de2e55e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
4 changes: 3 additions & 1 deletion cmd/report_week.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"time"

journal "github.com/deadpyxel/workday/internal"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -29,7 +30,8 @@ func reportWeek(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
currentWeek, err := journal.CurrentWeekEntries(journalEntries)
now := time.Now()
currentWeek, err := journal.FetchEntriesByWeekDate(journalEntries, now)
if err != nil {
return err
}
Expand Down
17 changes: 8 additions & 9 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func FetchEntryByID(id string, entries []JournalEntry) (*JournalEntry, int) {
return nil, -1
}

// CurrentWeekEntries filters a slice of JournalEntry objects and returns a new slice
// FetchEntriesByWeekDate filters a slice of JournalEntry objects and returns a new slice
// containing only the entries from the current week. The function uses the ISO week date
// system, where weeks start on a Monday and the first week of the year is the one that
// includes at least four days of the new year.
Expand All @@ -32,20 +32,19 @@ func FetchEntryByID(id string, entries []JournalEntry) (*JournalEntry, int) {
// Example:
//
// entries := []JournalEntry{...}
// currentWeekEntries, err := CurrentWeekEntries(entries)
// currentWeekEntries, err := FetchEntriesByWeekDate(entries)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Println(currentWeekEntries) // Prints the entries for the current week
func CurrentWeekEntries(journalEntries []JournalEntry) ([]JournalEntry, error) {
if journalEntries == nil {
return []JournalEntry{}, fmt.Errorf("No entries were passed")
func FetchEntriesByWeekDate(journalEntries []JournalEntry, currentDate time.Time) ([]JournalEntry, error) {
if len(journalEntries) == 0 {
return nil, fmt.Errorf("No entries were passed")
}
var currentWeekEntries []JournalEntry

// Get the current year and ISO week number.
now := time.Now()
currentYear, currentWeek := now.ISOWeek()
currentYear, currentWeek := currentDate.ISOWeek()

for _, entry := range journalEntries {
// get the year and ISO week number of the entry.
Expand All @@ -56,8 +55,8 @@ func CurrentWeekEntries(journalEntries []JournalEntry) ([]JournalEntry, error) {
currentWeekEntries = append(currentWeekEntries, entry)
}
}
if currentWeekEntries == nil {
return currentWeekEntries, fmt.Errorf("No entries found for the current week")
if len(currentWeekEntries) == 0 {
return nil, fmt.Errorf("No entries found for the current week")
}

return currentWeekEntries, nil
Expand Down

0 comments on commit de2e55e

Please sign in to comment.