Skip to content

Commit

Permalink
Adding FindOrElse to doc
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Feb 20, 2023
1 parent 209668a commit 417e490
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ This library is v1 and follows SemVer strictly.

No breaking changes will be made to exported APIs before v2.0.0.

This library has no dependencies except the Go std lib.

## 💡 Usage

You can import `lo` using:
Expand Down Expand Up @@ -188,6 +190,7 @@ Supported search helpers:
- [Find](#find)
- [FindIndexOf](#findindexof)
- [FindLastIndexOf](#findlastindexof)
- [FindOrElse](#findorelse)
- [FindKey](#findkey)
- [FindKeyBy](#findkeyby)
- [FindUniques](#finduniques)
Expand Down Expand Up @@ -1798,6 +1801,22 @@ str, index, ok := lo.FindLastIndexOf[string]([]string{"foobar"}, func(i string)
// "", -1, false
```

### FindOrElse

Search an element in a slice based on a predicate. It returns element and true if element was found.

```go
str := lo.FindOrElse[string]([]string{"a", "b", "c", "d"}, "x", func(i string) bool {
return i == "b"
})
// "b"

str := lo.FindOrElse[string]([]string{"foobar"}, "x", func(i string) bool {
return i == "b"
})
// "x"
```

### FindKey

Returns the key of the first value matching.
Expand Down
49 changes: 33 additions & 16 deletions find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ func TestFind(t *testing.T) {
t.Parallel()
is := assert.New(t)

result1, ok1 := Find([]string{"a", "b", "c", "d"}, func(i string) bool {
return i == "b"
index := 0
result1, ok1 := Find([]string{"a", "b", "c", "d"}, func(item string) bool {
is.Equal([]string{"a", "b", "c", "d"}[index], item)
index++
return item == "b"
})
result2, ok2 := Find([]string{"foobar"}, func(i string) bool {
return i == "b"

result2, ok2 := Find([]string{"foobar"}, func(item string) bool {
is.Equal("foobar", item)
return item == "b"
})

is.Equal(ok1, true)
Expand All @@ -53,11 +58,15 @@ func TestFindIndexOf(t *testing.T) {
t.Parallel()
is := assert.New(t)

item1, index1, ok1 := FindIndexOf([]string{"a", "b", "c", "d", "b"}, func(i string) bool {
return i == "b"
index := 0
item1, index1, ok1 := FindIndexOf([]string{"a", "b", "c", "d", "b"}, func(item string) bool {
is.Equal([]string{"a", "b", "c", "d", "b"}[index], item)
index++
return item == "b"
})
item2, index2, ok2 := FindIndexOf([]string{"foobar"}, func(i string) bool {
return i == "b"
item2, index2, ok2 := FindIndexOf([]string{"foobar"}, func(item string) bool {
is.Equal("foobar", item)
return item == "b"
})

is.Equal(item1, "b")
Expand All @@ -72,11 +81,15 @@ func TestFindLastIndexOf(t *testing.T) {
t.Parallel()
is := assert.New(t)

item1, index1, ok1 := FindLastIndexOf([]string{"a", "b", "c", "d", "b"}, func(i string) bool {
return i == "b"
index := 0
item1, index1, ok1 := FindLastIndexOf([]string{"a", "b", "c", "d", "b"}, func(item string) bool {
is.Equal([]string{"b", "d", "c", "b", "a"}[index], item)
index++
return item == "b"
})
item2, index2, ok2 := FindLastIndexOf([]string{"foobar"}, func(i string) bool {
return i == "b"
item2, index2, ok2 := FindLastIndexOf([]string{"foobar"}, func(item string) bool {
is.Equal("foobar", item)
return item == "b"
})

is.Equal(item1, "b")
Expand All @@ -91,11 +104,15 @@ func TestFindOrElse(t *testing.T) {
t.Parallel()
is := assert.New(t)

result1 := FindOrElse([]string{"a", "b", "c", "d"}, "x", func(i string) bool {
return i == "b"
index := 0
result1 := FindOrElse([]string{"a", "b", "c", "d"}, "x", func(item string) bool {
is.Equal([]string{"a", "b", "c", "d"}[index], item)
index++
return item == "b"
})
result2 := FindOrElse([]string{"foobar"}, "x", func(i string) bool {
return i == "b"
result2 := FindOrElse([]string{"foobar"}, "x", func(item string) bool {
is.Equal("foobar", item)
return item == "b"
})

is.Equal(result1, "b")
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module github.com/samber/lo

go 1.18

//
// Dependencies are excluded from releases. Please check CI.
//

require (
github.com/stretchr/testify v1.8.0
github.com/thoas/go-funk v0.9.1
Expand Down

0 comments on commit 417e490

Please sign in to comment.