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(find): implement maxn & minn for variadic count items #557

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,12 @@ Supported search helpers:
- [FindDuplicates](#findduplicates)
- [FindDuplicatesBy](#findduplicatesby)
- [Min](#min)
- [MinN](#minn)
- [MinBy](#minby)
- [Earliest](#earliest)
- [EarliestBy](#earliestby)
- [Max](#max)
- [MaxN](#maxn)
- [MaxBy](#maxby)
- [Latest](#latest)
- [LatestBy](#latestby)
Expand Down Expand Up @@ -2285,6 +2287,23 @@ min := lo.Min([]time.Duration{time.Second, time.Hour})
// 1s
```

### MinN

Search the minimum value of arbitrary number params.

Returns zero value when the no param are given.

```go
min := lo.MinN(1, 2, 3)
// 1

min := lo.Min([]int{}...)
// 0

min := lo.Min(time.Second, time.Hour...)
// 1s
```

### MinBy

Search the minimum value of a collection using the given comparison function.
Expand Down Expand Up @@ -2350,6 +2369,25 @@ max := lo.Max([]time.Duration{time.Second, time.Hour})
// 1h
```

### MaxN

Search the maximum value of arbitrary number params.

Returns zero value when the no param are given.

```go
max := lo.MaxN(1, 2, 3)
// 3

max := lo.MaxN([]int{}...)
// 0

max := lo.MaxN(time.Second, time.Hour)
// 1h
```



### MaxBy

Search the maximum value of a collection using the given comparison function.
Expand Down
12 changes: 12 additions & 0 deletions find.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ func Min[T constraints.Ordered](collection []T) T {
return min
}

// MinN search the minimum value of arbitrary number params.
// returns zero value when the no param are given.
func MinN[T constraints.Ordered](items ...T) T {
return Min(items)
}

// MinBy search the minimum value of a collection using the given comparison function.
// If several values of the collection are equal to the smallest value, returns the first such value.
// Returns zero value when the collection is empty.
Expand Down Expand Up @@ -332,6 +338,12 @@ func Max[T constraints.Ordered](collection []T) T {
return max
}

// MinN searches the maximum value of arbitrary number params.
// returns zero value when the no param are given.
func MaxN[T constraints.Ordered](items ...T) T {
return Max(items)
}

// MaxBy search the maximum value of a collection using the given comparison function.
// If several values of the collection are equal to the greatest value, returns the first such value.
// Returns zero value when the collection is empty.
Expand Down
20 changes: 20 additions & 0 deletions find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,16 @@ func TestMin(t *testing.T) {
is.Equal(result2, 1)
is.Equal(result3, time.Second)
is.Equal(result4, 0)

result1 = MinN(1, 2, 3)
result2 = MinN(3, 2, 1)
result3 = MinN(time.Second, time.Minute, time.Hour)
result4 = MinN([]int{}...)

is.Equal(result1, 1)
is.Equal(result2, 1)
is.Equal(result3, time.Second)
is.Equal(result4, 0)
}

func TestMinBy(t *testing.T) {
Expand Down Expand Up @@ -375,6 +385,16 @@ func TestMax(t *testing.T) {
is.Equal(result2, 3)
is.Equal(result3, time.Hour)
is.Equal(result4, 0)

result1 = MaxN(1, 2, 3)
result2 = MaxN(3, 2, 1)
result3 = MaxN(time.Second, time.Minute, time.Hour)
result4 = MaxN([]int{}...)

is.Equal(result1, 3)
is.Equal(result2, 3)
is.Equal(result3, time.Hour)
is.Equal(result4, 0)
}

func TestMaxBy(t *testing.T) {
Expand Down