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

feat: adding CoalesceOrEmpty #469

Merged
merged 1 commit into from
Jun 27, 2024
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ Type manipulation helpers:
- [IsEmpty](#isempty)
- [IsNotEmpty](#isnotempty)
- [Coalesce](#coalesce)
- [CoalesceOrEmpty](#coalesceorempty)

Function helpers:

Expand Down Expand Up @@ -2479,6 +2480,23 @@ result, ok := lo.Coalesce(nil, nilStr, &str)
// &"foobar" true
```

### CoalesceOrEmpty

Returns the first non-empty arguments. Arguments must be comparable.

```go
result := lo.CoalesceOrEmpty(0, 1, 2, 3)
// 1

result := lo.CoalesceOrEmpty("")
// ""

var nilStr *string
str := "foobar"
result := lo.CoalesceOrEmpty(nil, nilStr, &str)
// &"foobar"
```

### Partial

Returns new function that, when called, has its first argument set to the provided value.
Expand Down
6 changes: 6 additions & 0 deletions type_manipulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,9 @@ func Coalesce[T comparable](v ...T) (result T, ok bool) {

return
}

// CoalesceOrEmpty returns the first non-empty arguments. Arguments must be comparable.
func CoalesceOrEmpty[T comparable](v ...T) T {
result, _ := Coalesce(v...)
return result
}
40 changes: 40 additions & 0 deletions type_manipulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,43 @@ func TestCoalesce(t *testing.T) {
is.Equal(result10, struct1)
is.True(ok10)
}

func TestCoalesceOrEmpty(t *testing.T) {
t.Parallel()
is := assert.New(t)

newStr := func(v string) *string { return &v }
var nilStr *string
str1 := newStr("str1")
str2 := newStr("str2")

type structType struct {
field1 int
field2 float64
}
var zeroStruct structType
struct1 := structType{1, 1.0}
struct2 := structType{2, 2.0}

result1 := CoalesceOrEmpty[int]()
result2 := CoalesceOrEmpty(3)
result3 := CoalesceOrEmpty(nil, nilStr)
result4 := CoalesceOrEmpty(nilStr, str1)
result5 := CoalesceOrEmpty(nilStr, str1, str2)
result6 := CoalesceOrEmpty(str1, str2, nilStr)
result7 := CoalesceOrEmpty(0, 1, 2, 3)
result8 := CoalesceOrEmpty(zeroStruct)
result9 := CoalesceOrEmpty(zeroStruct, struct1)
result10 := CoalesceOrEmpty(zeroStruct, struct1, struct2)

is.Equal(0, result1)
is.Equal(3, result2)
is.Nil(result3)
is.Equal(str1, result4)
is.Equal(str1, result5)
is.Equal(str1, result6)
is.Equal(result7, 1)
is.Equal(result8, zeroStruct)
is.Equal(result9, struct1)
is.Equal(result10, struct1)
}
Loading