diff --git a/README.md b/README.md index 9f6a0e3..7142f24 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/cmp/cmp.go b/cmp/cmp.go index 428e855..323f745 100644 --- a/cmp/cmp.go +++ b/cmp/cmp.go @@ -3,6 +3,7 @@ package cmp import ( + "cmp" "reflect" "golang.org/x/exp/constraints" @@ -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 +} diff --git a/cmp/cmp_test.go b/cmp/cmp_test.go index 7634255..a350532 100644 --- a/cmp/cmp_test.go +++ b/cmp/cmp_test.go @@ -4,6 +4,7 @@ import ( "reflect" "testing" + "github.com/dablelv/cyan/internal" "golang.org/x/exp/constraints" ) @@ -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")) +} diff --git a/slice/crud.go b/slice/crud.go index 92244c8..08ed544 100644 --- a/slice/crud.go +++ b/slice/crud.go @@ -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]) diff --git a/time/time.go b/time/time.go index 40248d8..3c2265c 100644 --- a/time/time.go +++ b/time/time.go @@ -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"