Skip to content

Commit

Permalink
feat:add Max and Min generic functions
Browse files Browse the repository at this point in the history
  • Loading branch information
R22627 authored and R22627 committed Dec 28, 2023
1 parent 2fd7181 commit 6d5d2f5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ A modern, comprehensive and efficient utility library of Golang.

# Features
- Comprehensive, efficient and reusable.
- Numerous util functions, support string, slice, map, datetime, crypto...
- Numerous utility functions, support string, slice, map, datetime, crypto...
- Only depend on the go standard and golang.org/x library.
- High unit test coverage for exported functions.

Expand Down
17 changes: 17 additions & 0 deletions cmp/cmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package cmp

import (
"cmp"
"reflect"

"golang.org/x/exp/constraints"
Expand Down Expand Up @@ -113,3 +114,19 @@ func isComparable(lhs, rhs any) bool {
r := reflect.ValueOf(rhs)
return l.Kind() == r.Kind()
}

// Min compare two values and return smaller value.
func Min[T cmp.Ordered](x, y T) T {
if x < y {
return x
}
return y
}

// Max compare two values and return larger value.
func Max[T cmp.Ordered](x, y T) T {
if x > y {
return x
}
return y
}
17 changes: 17 additions & 0 deletions cmp/cmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"reflect"
"testing"

"github.com/dablelv/cyan/internal"
"golang.org/x/exp/constraints"
)

Expand Down Expand Up @@ -94,3 +95,19 @@ func TestCompare(t *testing.T) {
})
}
}

func TestMin(t *testing.T) {
assert := internal.NewAssert(t, "TestMin")

assert.Equal(1, Min(1, 2))
assert.Equal(1.1, Min(2.2, 1.1))
assert.Equal("abc", Min("abc", "cba"))
}

func TestMax(t *testing.T) {
assert := internal.NewAssert(t, "TestMax")

assert.Equal(2, Max(1, 2))
assert.Equal(2.2, Max(2.2, 1.1))
assert.Equal("cba", Max("abc", "cba"))
}
4 changes: 2 additions & 2 deletions slice/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
//
// All of the functions in this file are the supplement to the standard library slices package.
// The standard library functions Insert, Delete, DeleteFunc, Replace, Index, IndexFunc
// implemented by generics in the https://pkg.go.dev/golang.org/x/exp/slices package should be used first.
// implemented by generics in the https://pkg.go.dev/slices package should be used first.
//

// Insert inserts the values v... into s at index i and returns the result slice.
// Unlike the standard library function Insert, Insert won't modify the original slice.
// Unlike the standard library, Insert won't modify the original slice.
func Insert[S ~[]E, E any](s S, i int, v ...E) S {
r := make(S, len(s)+len(v))
copy(r, s[:i])
Expand Down
22 changes: 12 additions & 10 deletions time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import (
)

const (
YFormatNum = "2006"
YMFormatNum = "200601"
DateFormatNum = "20060102"
DateHFormatNum = "2006010215"
DateHMFormatNum = "200601021504"
DateTimeFormatNum = "20060102150405"
HFormatNum = "15"
HMFormatNum = "1504"
TimeFormatNum = "150405"
DateFormat = "2006-01-02"
YFormatNum = "2006"
YMFormatNum = "200601"
DateFormatNum = "20060102"
DateHFormatNum = "2006010215"
DateHMFormatNum = "200601021504"
DateTimeFormatNum = "20060102150405"
HFormatNum = "15"
HMFormatNum = "1504"
TimeFormatNum = "150405"
// Deprecated: please use standard library time.Dateonly
DateFormat = "2006-01-02"
// Deprecated: please use standard library time.TimeOnly
TimeFormat = "15:04:05"
DateTimeFormat = "2006-01-02 15:04:05"
DateTimeFormatMilli = "2006-01-02 15:04:05.000"
Expand Down

0 comments on commit 6d5d2f5

Please sign in to comment.