Skip to content
Open
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
20 changes: 20 additions & 0 deletions generic.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package helpers

import "reflect"

func Ref[T any](in T) *T {
return &in
}
Expand Down Expand Up @@ -30,3 +32,21 @@ func If[T any](cond bool, vtrue, vfalse T) T {
}
return vfalse
}

func Coalesce[T any](input ...T) T {
var check T
for _, check = range input {
if !IsZero(check) {
return check
}
}
return check
}

func IsZero(input any) bool {
if input == nil {
return true
}
r := reflect.ValueOf(input)
return !r.IsValid() || (r.Kind() == reflect.Pointer && r.IsNil()) || r.IsZero()
}
25 changes: 25 additions & 0 deletions generic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,28 @@ func TestIf(t *testing.T) {
assert.Equal(t, test.output, result)
}
}

func TestCoalesce(t *testing.T) {
tests := []struct {
input []any
output any
}{
{[]any{"", "", "hello"}, "hello"},
{[]any{"", "world", ""}, "world"},
{[]any{"", "", ""}, ""},
{[]any{0, 0, 1}, 1},
{[]any{0, 2, 1}, 2},
{[]any{0, 0, 0}, 0},
{[]any{nil, nil, "hello"}, "hello"},
{[]any{nil, "world", nil}, "world"},
{[]any{nil, "", nil}, nil},
{[]any{nil, nil, nil}, nil},
{[]any{nil, 2, nil}, 2},
{[]any{nil, 0, nil}, nil},
}

for _, test := range tests {
result := Coalesce(test.input...)
assert.Equal(t, test.output, result)
}
}
7 changes: 1 addition & 6 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ func BufferToString(buf io.Reader) string {
}

func FirstStr(input ...string) string {
for _, s := range input {
if s != "" {
return s
}
}
return ""
return Coalesce(input...)
}

func TruncateString(input string, length int) string {
Expand Down