Skip to content
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ package mypkg
- `file` - file operations
- `set` - set data structure
- `unit` - unit formatting and conversion package
- `version` - version functions
- `xjson` - JSON functions
- `xslices` - slice data type functions
- `xstrings` - string data type functions
Expand Down
12 changes: 0 additions & 12 deletions version/compare.go

This file was deleted.

49 changes: 0 additions & 49 deletions version/compare_test.go

This file was deleted.

2 changes: 0 additions & 2 deletions version/doc.go

This file was deleted.

14 changes: 14 additions & 0 deletions xstrings/funcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package xstrings

import "github.com/neticdk/go-stdlib/xslices"

// Coalesce returns the first non-empty string from the given slice.
func Coalesce(strs ...string) string {
s, found := xslices.FindFunc(strs, func(s string) bool {
return s != ""
})
if found {
return s
}
return ""
}
68 changes: 68 additions & 0 deletions xstrings/funcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package xstrings_test

import (
"testing"

"github.com/neticdk/go-stdlib/assert"
"github.com/neticdk/go-stdlib/xstrings"
)

func TestCoalesce(t *testing.T) {
tests := []struct {
name string
args []string
want string
}{
{
name: "Empty slice",
args: []string{},
want: "",
},
{
name: "All empty strings",
args: []string{"", "", ""},
want: "",
},
{
name: "First string non-empty",
args: []string{"first", "second", "third"},
want: "first",
},
{
name: "Second string non-empty",
args: []string{"", "second", "third"},
want: "second",
},
{
name: "Last string non-empty",
args: []string{"", "", "third"},
want: "third",
},
{
name: "Mixed empty and non-empty",
args: []string{"", "second", "", "fourth"},
want: "second",
},
{
name: "Single non-empty string",
args: []string{"single"},
want: "single",
},
{
name: "Single empty string",
args: []string{""},
want: "",
},
{
name: "Nil slice (variadic converts nil to empty)",
args: nil,
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := xstrings.Coalesce(tt.args...)
assert.Equal(t, got, tt.want, "Coalesce()/%s", tt.name)
})
}
}