Skip to content

Commit

Permalink
some more helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
tagus committed Jun 5, 2024
1 parent 16da484 commit dde4566
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 0 deletions.
4 changes: 4 additions & 0 deletions codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ func TestUnmarshalFromString(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, decodedSlice)
require.Equal(t, []string{"foo", "bar"}, *decodedSlice)

decodedSlice, err = UnmarshalFromString[[]string]("null")
require.NoError(t, err)
require.Nil(t, decodedSlice)
}
12 changes: 12 additions & 0 deletions numbers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mango

// Min determines the minimum among the provided values
func Min(vals ...int) int {
min := vals[0]
for _, val := range vals {
if val < min {
min = val
}
}
return min
}
12 changes: 12 additions & 0 deletions numbers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mango

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestFindingMin(t *testing.T) {
min := Min(1, 2, 3, -594849)
assert.Equal(t, -594849, min)
}
10 changes: 10 additions & 0 deletions slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,13 @@ func Filter[F any](list []F, predicate func(F) bool) []F {
}
return filtered
}

func Find[F any](list []F, predicate func(F) bool) (F, bool) {
var empty F
for _, item := range list {
if predicate(item) {
return item, true
}
}
return empty, false
}
13 changes: 13 additions & 0 deletions slices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMap(t *testing.T) {
Expand All @@ -23,3 +24,15 @@ func TestFilter(t *testing.T) {

assert.Equal(t, expected, actual)
}

func TestFind(t *testing.T) {
lst := []int{1, 2, 3}

actual, ok := Find(lst, func(i int) bool { return i == 2 })
require.True(t, ok)
require.Equal(t, 2, actual)

actual, ok = Find(lst, func(i int) bool { return i == 45 })
require.False(t, ok)
require.Equal(t, 0, actual)
}
46 changes: 46 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package mango

import (
"fmt"
"time"
)

const (
DurationDay = 24 * time.Hour
DurationYear = 365 * DurationDay
)

func FormatSimpleDate(ts *time.Time) string {
if ts == nil {
return "never"
}
return ts.Format("01-02-06")
}

func FormatTimeSince(ts *time.Time) string {
if ts == nil {
return "never"
}
now := time.Now()
elapsed := now.Sub(*ts)
return fmt.Sprintf("%v ago", formatDuration(elapsed))
}

// imprecise formatting since we only care about the highest possible unit
func formatDuration(d time.Duration) string {
if d >= DurationYear {
years := d / DurationYear
return fmt.Sprintf("%dy", years)
} else if d >= DurationDay {
days := d / DurationDay
return fmt.Sprintf("%dd", days)
} else if d >= time.Hour {
hours := d / time.Hour
return fmt.Sprintf("%dh", hours)
} else if d >= time.Minute {
minutes := d / time.Minute
return fmt.Sprintf("%dm", minutes)
}
seconds := d / time.Second
return fmt.Sprintf("%ds", seconds)
}
18 changes: 18 additions & 0 deletions time_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package mango

import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func TestFormatSimpleDate(t *testing.T) {
ts := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)
assert.Equal(t, "01-01-21", FormatSimpleDate(&ts))
}

func TestFormatTimeSince(t *testing.T) {
ts := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)
timeSince := FormatTimeSince(&ts)
assert.NotEmpty(t, timeSince)
}

0 comments on commit dde4566

Please sign in to comment.