Bump github.com/gookit/config/v2 from 2.2.3 to 2.2.4
Bumps [github.com/gookit/config/v2](https://github.com/gookit/config) from 2.2.3 to 2.2.4. - [Release notes](https://github.com/gookit/config/releases) - [Commits](https://github.com/gookit/config/compare/v2.2.3...v2.2.4) --- updated-dependencies: - dependency-name: github.com/gookit/config/v2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com>
This commit is contained in:
committed by
Ralf Haferkamp
parent
30784affc4
commit
0df009eae0
+81
-1
@@ -10,11 +10,91 @@ go get github.com/gookit/goutil/mathutil
|
||||
|
||||
## Go docs
|
||||
|
||||
- [Go docs](https://pkg.go.dev/github.com/gookit/goutil/mathutil)
|
||||
- [Go Docs](https://pkg.go.dev/github.com/gookit/goutil)
|
||||
|
||||
## Usage
|
||||
|
||||
|
||||
## Functions
|
||||
|
||||
```go
|
||||
|
||||
func CompFloat[T comdef.Float](first, second T, op string) (ok bool)
|
||||
func CompInt[T comdef.Xint](first, second T, op string) (ok bool)
|
||||
func CompInt64(first, second int64, op string) bool
|
||||
func CompValue[T comdef.XintOrFloat](first, second T, op string) (ok bool)
|
||||
func Compare(first, second any, op string) (ok bool)
|
||||
func DataSize(size uint64) string
|
||||
func ElapsedTime(startTime time.Time) string
|
||||
func Float(in any) (float64, error)
|
||||
func FloatOr(in any, defVal float64) float64
|
||||
func FloatOrDefault(in any, defVal float64) float64
|
||||
func FloatOrErr(in any) (float64, error)
|
||||
func FloatOrPanic(in any) float64
|
||||
func GreaterOr[T comdef.XintOrFloat](val, min, defVal T) T
|
||||
func GteOr[T comdef.XintOrFloat](val, min, defVal T) T
|
||||
func HowLongAgo(sec int64) string
|
||||
func InRange[T comdef.IntOrFloat](val, min, max T) bool
|
||||
func InUintRange[T comdef.Uint](val, min, max T) bool
|
||||
func Int(in any) (int, error)
|
||||
func Int64(in any) (int64, error)
|
||||
func Int64OrErr(in any) (int64, error)
|
||||
func IntOr(in any, defVal int) int
|
||||
func IntOrDefault(in any, defVal int) int
|
||||
func IntOrErr(in any) (iVal int, err error)
|
||||
func IntOrPanic(in any) int
|
||||
func IsNumeric(c byte) bool
|
||||
func LessOr[T comdef.XintOrFloat](val, max, devVal T) T
|
||||
func LteOr[T comdef.XintOrFloat](val, max, devVal T) T
|
||||
func Max[T comdef.XintOrFloat](x, y T) T
|
||||
func MaxFloat(x, y float64) float64
|
||||
func MaxI64(x, y int64) int64
|
||||
func MaxInt(x, y int) int
|
||||
func Min[T comdef.XintOrFloat](x, y T) T
|
||||
func MustFloat(in any) float64
|
||||
func MustInt(in any) int
|
||||
func MustInt64(in any) int64
|
||||
func MustString(val any) string
|
||||
func MustUint(in any) uint64
|
||||
func OrElse[T comdef.XintOrFloat](val, defVal T) T
|
||||
func OutRange[T comdef.IntOrFloat](val, min, max T) bool
|
||||
func Percent(val, total int) float64
|
||||
func QuietFloat(in any) float64
|
||||
func QuietInt(in any) int
|
||||
func QuietInt64(in any) int64
|
||||
func QuietString(val any) string
|
||||
func QuietUint(in any) uint64
|
||||
func RandInt(min, max int) int
|
||||
func RandIntWithSeed(min, max int, seed int64) int
|
||||
func RandomInt(min, max int) int
|
||||
func RandomIntWithSeed(min, max int, seed int64) int
|
||||
func SafeFloat(in any) float64
|
||||
func SafeInt(in any) int
|
||||
func SafeInt64(in any) int64
|
||||
func SafeUint(in any) uint64
|
||||
func StrInt(s string) int
|
||||
func StrIntOr(s string, defVal int) int
|
||||
func String(val any) string
|
||||
func StringOrErr(val any) (string, error)
|
||||
func StringOrPanic(val any) string
|
||||
func SwapMax[T comdef.XintOrFloat](x, y T) (T, T)
|
||||
func SwapMaxI64(x, y int64) (int64, int64)
|
||||
func SwapMaxInt(x, y int) (int, int)
|
||||
func SwapMin[T comdef.XintOrFloat](x, y T) (T, T)
|
||||
func ToFloat(in any) (f64 float64, err error)
|
||||
func ToFloatWithFunc(in any, usrFn func(any) (float64, error)) (f64 float64, err error)
|
||||
func ToInt(in any) (iVal int, err error)
|
||||
func ToInt64(in any) (i64 int64, err error)
|
||||
func ToString(val any) (string, error)
|
||||
func ToUint(in any) (u64 uint64, err error)
|
||||
func ToUintWithFunc(in any, usrFn func(any) (uint64, error)) (u64 uint64, err error)
|
||||
func TryToString(val any, defaultAsErr bool) (str string, err error)
|
||||
func Uint(in any) (uint64, error)
|
||||
func UintOrErr(in any) (uint64, error)
|
||||
func ZeroOr[T comdef.XintOrFloat](val, defVal T) T
|
||||
|
||||
```
|
||||
|
||||
## Testings
|
||||
|
||||
```shell
|
||||
|
||||
+39
-43
@@ -2,7 +2,12 @@ package mathutil
|
||||
|
||||
import "github.com/gookit/goutil/comdef"
|
||||
|
||||
// Compare any intX,floatX value by given op. returns `srcVal op(=,!=,<,<=,>,>=) dstVal`
|
||||
// IsNumeric returns true if the given character is a numeric, otherwise false.
|
||||
func IsNumeric(c byte) bool {
|
||||
return c >= '0' && c <= '9'
|
||||
}
|
||||
|
||||
// Compare any intX,floatX value by given op. returns `first op(=,!=,<,<=,>,>=) second`
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
@@ -10,70 +15,61 @@ import "github.com/gookit/goutil/comdef"
|
||||
// mathutil.Compare(2, 1.3, ">") // true
|
||||
// mathutil.Compare(2.2, 1.3, ">") // true
|
||||
// mathutil.Compare(2.1, 2, ">") // true
|
||||
func Compare(srcVal, dstVal any, op string) (ok bool) {
|
||||
if srcVal == nil || dstVal == nil {
|
||||
func Compare(first, second any, op string) bool {
|
||||
if first == nil || second == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
// float
|
||||
if srcFlt, ok := srcVal.(float64); ok {
|
||||
if dstFlt, err := ToFloat(dstVal); err == nil {
|
||||
return CompFloat(srcFlt, dstFlt, op)
|
||||
switch fVal := first.(type) {
|
||||
case float64:
|
||||
if sVal, err := ToFloat(second); err == nil {
|
||||
return CompFloat(fVal, sVal, op)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
if srcFlt, ok := srcVal.(float32); ok {
|
||||
if dstFlt, err := ToFloat(dstVal); err == nil {
|
||||
return CompFloat(float64(srcFlt), dstFlt, op)
|
||||
case float32:
|
||||
if sVal, err := ToFloat(second); err == nil {
|
||||
return CompFloat(float64(fVal), sVal, op)
|
||||
}
|
||||
default: // as int64
|
||||
if int1, err := ToInt64(first); err == nil {
|
||||
if int2, err := ToInt64(second); err == nil {
|
||||
return CompInt64(int1, int2, op)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// as int64
|
||||
srcInt, err := ToInt64(srcVal)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
dstInt, err := ToInt64(dstVal)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return CompInt64(srcInt, dstInt, op)
|
||||
return false
|
||||
}
|
||||
|
||||
// CompInt compare int,uint value. returns `srcVal op(=,!=,<,<=,>,>=) dstVal`
|
||||
func CompInt[T comdef.Xint](srcVal, dstVal T, op string) (ok bool) {
|
||||
return CompValue(srcVal, dstVal, op)
|
||||
// CompInt compare all intX,uintX type value. returns `first op(=,!=,<,<=,>,>=) second`
|
||||
func CompInt[T comdef.Xint](first, second T, op string) (ok bool) {
|
||||
return CompValue(first, second, op)
|
||||
}
|
||||
|
||||
// CompInt64 compare int64 value. returns `srcVal op(=,!=,<,<=,>,>=) dstVal`
|
||||
func CompInt64(srcVal, dstVal int64, op string) bool {
|
||||
return CompValue(srcVal, dstVal, op)
|
||||
// CompInt64 compare int64 value. returns `first op(=,!=,<,<=,>,>=) second`
|
||||
func CompInt64(first, second int64, op string) bool {
|
||||
return CompValue(first, second, op)
|
||||
}
|
||||
|
||||
// CompFloat compare float64,float32 value. returns `srcVal op(=,!=,<,<=,>,>=) dstVal`
|
||||
func CompFloat[T comdef.Float](srcVal, dstVal T, op string) (ok bool) {
|
||||
return CompValue(srcVal, dstVal, op)
|
||||
// CompFloat compare float64,float32 value. returns `first op(=,!=,<,<=,>,>=) second`
|
||||
func CompFloat[T comdef.Float](first, second T, op string) (ok bool) {
|
||||
return CompValue(first, second, op)
|
||||
}
|
||||
|
||||
// CompValue compare intX,uintX,floatX value. returns `srcVal op(=,!=,<,<=,>,>=) dstVal`
|
||||
func CompValue[T comdef.XintOrFloat](srcVal, dstVal T, op string) (ok bool) {
|
||||
// CompValue compare intX,uintX,floatX value. returns `first op(=,!=,<,<=,>,>=) second`
|
||||
func CompValue[T comdef.XintOrFloat](first, second T, op string) (ok bool) {
|
||||
switch op {
|
||||
case "<", "lt":
|
||||
ok = srcVal < dstVal
|
||||
ok = first < second
|
||||
case "<=", "lte":
|
||||
ok = srcVal <= dstVal
|
||||
ok = first <= second
|
||||
case ">", "gt":
|
||||
ok = srcVal > dstVal
|
||||
ok = first > second
|
||||
case ">=", "gte":
|
||||
ok = srcVal >= dstVal
|
||||
ok = first >= second
|
||||
case "=", "eq":
|
||||
ok = srcVal == dstVal
|
||||
ok = first == second
|
||||
case "!=", "ne", "neq":
|
||||
ok = srcVal != dstVal
|
||||
ok = first != second
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
package mathutil
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/gookit/goutil/comdef"
|
||||
)
|
||||
|
||||
// Min compare two value and return max value
|
||||
func Min[T comdef.XintOrFloat](x, y T) T {
|
||||
if x < y {
|
||||
return x
|
||||
}
|
||||
return y
|
||||
}
|
||||
|
||||
// Max compare two value and return max value
|
||||
func Max[T comdef.XintOrFloat](x, y T) T {
|
||||
if x > y {
|
||||
return x
|
||||
}
|
||||
return y
|
||||
}
|
||||
|
||||
// SwapMin compare and always return [min, max] value
|
||||
func SwapMin[T comdef.XintOrFloat](x, y T) (T, T) {
|
||||
if x < y {
|
||||
return x, y
|
||||
}
|
||||
return y, x
|
||||
}
|
||||
|
||||
// SwapMax compare and always return [max, min] value
|
||||
func SwapMax[T comdef.XintOrFloat](x, y T) (T, T) {
|
||||
if x > y {
|
||||
return x, y
|
||||
}
|
||||
return y, x
|
||||
}
|
||||
|
||||
// MaxInt compare and return max value
|
||||
func MaxInt(x, y int) int {
|
||||
if x > y {
|
||||
return x
|
||||
}
|
||||
return y
|
||||
}
|
||||
|
||||
// SwapMaxInt compare and return max, min value
|
||||
func SwapMaxInt(x, y int) (int, int) {
|
||||
if x > y {
|
||||
return x, y
|
||||
}
|
||||
return y, x
|
||||
}
|
||||
|
||||
// MaxI64 compare and return max value
|
||||
func MaxI64(x, y int64) int64 {
|
||||
if x > y {
|
||||
return x
|
||||
}
|
||||
return y
|
||||
}
|
||||
|
||||
// SwapMaxI64 compare and return max, min value
|
||||
func SwapMaxI64(x, y int64) (int64, int64) {
|
||||
if x > y {
|
||||
return x, y
|
||||
}
|
||||
return y, x
|
||||
}
|
||||
|
||||
// MaxFloat compare and return max value
|
||||
func MaxFloat(x, y float64) float64 {
|
||||
return math.Max(x, y)
|
||||
}
|
||||
+263
-70
@@ -1,8 +1,8 @@
|
||||
package mathutil
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -10,6 +10,18 @@ import (
|
||||
"github.com/gookit/goutil/comdef"
|
||||
)
|
||||
|
||||
// ToIntFunc convert value to int
|
||||
type ToIntFunc func(any) (int, error)
|
||||
|
||||
// ToInt64Func convert value to int64
|
||||
type ToInt64Func func(any) (int64, error)
|
||||
|
||||
// ToUintFunc convert value to uint
|
||||
type ToUintFunc func(any) (uint64, error)
|
||||
|
||||
// ToFloatFunc convert value to float
|
||||
type ToFloatFunc func(any) (float64, error)
|
||||
|
||||
/*************************************************************
|
||||
* convert value to int
|
||||
*************************************************************/
|
||||
@@ -19,20 +31,19 @@ func Int(in any) (int, error) {
|
||||
return ToInt(in)
|
||||
}
|
||||
|
||||
// QuietInt convert value to int, will ignore error
|
||||
func QuietInt(in any) int {
|
||||
// SafeInt convert value to int, will ignore error
|
||||
func SafeInt(in any) int {
|
||||
val, _ := ToInt(in)
|
||||
return val
|
||||
}
|
||||
|
||||
// QuietInt convert value to int, will ignore error
|
||||
func QuietInt(in any) int {
|
||||
return SafeInt(in)
|
||||
}
|
||||
|
||||
// MustInt convert value to int, will panic on error
|
||||
func MustInt(in any) int {
|
||||
val, _ := ToInt(in)
|
||||
return val
|
||||
}
|
||||
|
||||
// IntOrPanic convert value to int, will panic on error
|
||||
func IntOrPanic(in any) int {
|
||||
val, err := ToInt(in)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
@@ -40,16 +51,38 @@ func IntOrPanic(in any) int {
|
||||
return val
|
||||
}
|
||||
|
||||
// IntOrPanic convert value to int, will panic on error
|
||||
func IntOrPanic(in any) int {
|
||||
return MustInt(in)
|
||||
}
|
||||
|
||||
// IntOrDefault convert value to int, return defaultVal on failed
|
||||
func IntOrDefault(in any, defVal int) int {
|
||||
return IntOr(in, defVal)
|
||||
}
|
||||
|
||||
// IntOr convert value to int, return defaultVal on failed
|
||||
func IntOr(in any, defVal int) int {
|
||||
val, err := ToIntWithFunc(in, nil)
|
||||
if err != nil {
|
||||
return defVal
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// IntOrErr convert value to int, return error on failed
|
||||
func IntOrErr(in any) (iVal int, err error) {
|
||||
return ToInt(in)
|
||||
return ToIntWithFunc(in, nil)
|
||||
}
|
||||
|
||||
// ToInt convert value to int, return error on failed
|
||||
func ToInt(in any) (iVal int, err error) {
|
||||
return ToIntWithFunc(in, nil)
|
||||
}
|
||||
|
||||
// ToIntWithFunc convert value to int, will call usrFn on value type not supported.
|
||||
func ToIntWithFunc(in any, usrFn ToIntFunc) (iVal int, err error) {
|
||||
switch tVal := in.(type) {
|
||||
case nil:
|
||||
iVal = 0
|
||||
case int:
|
||||
iVal = tVal
|
||||
case int8:
|
||||
@@ -59,31 +92,60 @@ func ToInt(in any) (iVal int, err error) {
|
||||
case int32:
|
||||
iVal = int(tVal)
|
||||
case int64:
|
||||
iVal = int(tVal)
|
||||
if tVal > math.MaxInt32 {
|
||||
err = fmt.Errorf("value overflow int32. input: %v", tVal)
|
||||
} else {
|
||||
iVal = int(tVal)
|
||||
}
|
||||
case uint:
|
||||
iVal = int(tVal)
|
||||
if tVal > math.MaxInt32 {
|
||||
err = fmt.Errorf("value overflow int32. input: %v", tVal)
|
||||
} else {
|
||||
iVal = int(tVal)
|
||||
}
|
||||
case uint8:
|
||||
iVal = int(tVal)
|
||||
case uint16:
|
||||
iVal = int(tVal)
|
||||
case uint32:
|
||||
iVal = int(tVal)
|
||||
if tVal > math.MaxInt32 {
|
||||
err = fmt.Errorf("value overflow int32. input: %v", tVal)
|
||||
} else {
|
||||
iVal = int(tVal)
|
||||
}
|
||||
case uint64:
|
||||
iVal = int(tVal)
|
||||
if tVal > math.MaxInt32 {
|
||||
err = fmt.Errorf("value overflow int32. input: %v", tVal)
|
||||
} else {
|
||||
iVal = int(tVal)
|
||||
}
|
||||
case float32:
|
||||
iVal = int(tVal)
|
||||
case float64:
|
||||
iVal = int(tVal)
|
||||
case time.Duration:
|
||||
iVal = int(tVal)
|
||||
if tVal > math.MaxInt32 {
|
||||
err = fmt.Errorf("value overflow int32. input: %v", tVal)
|
||||
} else {
|
||||
iVal = int(tVal)
|
||||
}
|
||||
case string:
|
||||
iVal, err = strconv.Atoi(strings.TrimSpace(tVal))
|
||||
case json.Number:
|
||||
case interface{ Int64() (int64, error) }: // eg: json.Number
|
||||
var i64 int64
|
||||
i64, err = tVal.Int64()
|
||||
iVal = int(i64)
|
||||
if i64, err = tVal.Int64(); err == nil {
|
||||
if i64 > math.MaxInt32 {
|
||||
err = fmt.Errorf("value overflow int32. input: %v", tVal)
|
||||
} else {
|
||||
iVal = int(i64)
|
||||
}
|
||||
}
|
||||
default:
|
||||
err = comdef.ErrConvType
|
||||
if usrFn != nil {
|
||||
return usrFn(in)
|
||||
} else {
|
||||
err = comdef.ErrConvType
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -94,37 +156,71 @@ func StrInt(s string) int {
|
||||
return iVal
|
||||
}
|
||||
|
||||
// StrIntOr convert string to int, return default val on failed
|
||||
func StrIntOr(s string, defVal int) int {
|
||||
iVal, err := strconv.Atoi(strings.TrimSpace(s))
|
||||
if err != nil {
|
||||
return defVal
|
||||
}
|
||||
return iVal
|
||||
}
|
||||
|
||||
/*************************************************************
|
||||
* convert value to uint
|
||||
*************************************************************/
|
||||
|
||||
// Uint convert string to uint, return error on failed
|
||||
// Uint convert any to uint, return error on failed
|
||||
func Uint(in any) (uint64, error) {
|
||||
return ToUint(in)
|
||||
}
|
||||
|
||||
// QuietUint convert string to uint, will ignore error
|
||||
func QuietUint(in any) uint64 {
|
||||
// SafeUint convert any to uint, will ignore error
|
||||
func SafeUint(in any) uint64 {
|
||||
val, _ := ToUint(in)
|
||||
return val
|
||||
}
|
||||
|
||||
// MustUint convert string to uint, will panic on error
|
||||
// QuietUint convert any to uint, will ignore error
|
||||
func QuietUint(in any) uint64 {
|
||||
return SafeUint(in)
|
||||
}
|
||||
|
||||
// MustUint convert any to uint, will panic on error
|
||||
func MustUint(in any) uint64 {
|
||||
val, _ := ToUint(in)
|
||||
val, err := ToUintWithFunc(in, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// UintOrDefault convert any to uint, return default val on failed
|
||||
func UintOrDefault(in any, defVal uint64) uint64 {
|
||||
return UintOr(in, defVal)
|
||||
}
|
||||
|
||||
// UintOr convert any to uint, return default val on failed
|
||||
func UintOr(in any, defVal uint64) uint64 {
|
||||
val, err := ToUintWithFunc(in, nil)
|
||||
if err != nil {
|
||||
return defVal
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// UintOrErr convert value to uint, return error on failed
|
||||
func UintOrErr(in any) (uint64, error) {
|
||||
return ToUint(in)
|
||||
return ToUintWithFunc(in, nil)
|
||||
}
|
||||
|
||||
// ToUint convert value to uint, return error on failed
|
||||
func ToUint(in any) (u64 uint64, err error) {
|
||||
return ToUintWithFunc(in, nil)
|
||||
}
|
||||
|
||||
// ToUintWithFunc convert value to uint, will call usrFn on value type not supported.
|
||||
func ToUintWithFunc(in any, usrFn ToUintFunc) (u64 uint64, err error) {
|
||||
switch tVal := in.(type) {
|
||||
case nil:
|
||||
u64 = 0
|
||||
case int:
|
||||
u64 = uint64(tVal)
|
||||
case int8:
|
||||
@@ -151,14 +247,18 @@ func ToUint(in any) (u64 uint64, err error) {
|
||||
u64 = uint64(tVal)
|
||||
case time.Duration:
|
||||
u64 = uint64(tVal)
|
||||
case json.Number:
|
||||
case interface{ Int64() (int64, error) }: // eg: json.Number
|
||||
var i64 int64
|
||||
i64, err = tVal.Int64()
|
||||
u64 = uint64(i64)
|
||||
case string:
|
||||
u64, err = strconv.ParseUint(strings.TrimSpace(tVal), 10, 0)
|
||||
default:
|
||||
err = comdef.ErrConvType
|
||||
if usrFn != nil {
|
||||
u64, err = usrFn(in)
|
||||
} else {
|
||||
err = comdef.ErrConvType
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -167,41 +267,58 @@ func ToUint(in any) (u64 uint64, err error) {
|
||||
* convert value to int64
|
||||
*************************************************************/
|
||||
|
||||
// Int64 convert string to int64, return error on failed
|
||||
// Int64 convert value to int64, return error on failed
|
||||
func Int64(in any) (int64, error) {
|
||||
return ToInt64(in)
|
||||
}
|
||||
|
||||
// SafeInt64 convert value to int64, will ignore error
|
||||
func SafeInt64(in any) int64 {
|
||||
i64, _ := ToInt64(in)
|
||||
i64, _ := ToInt64WithFunc(in, nil)
|
||||
return i64
|
||||
}
|
||||
|
||||
// QuietInt64 convert value to int64, will ignore error
|
||||
func QuietInt64(in any) int64 {
|
||||
i64, _ := ToInt64(in)
|
||||
return i64
|
||||
return SafeInt64(in)
|
||||
}
|
||||
|
||||
// MustInt64 convert value to int64, will panic on error
|
||||
func MustInt64(in any) int64 {
|
||||
i64, _ := ToInt64(in)
|
||||
i64, err := ToInt64WithFunc(in, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return i64
|
||||
}
|
||||
|
||||
// TODO StrictInt64,AsInt64 strict convert to int64
|
||||
// Int64OrDefault convert value to int64, return default val on failed
|
||||
func Int64OrDefault(in any, defVal int64) int64 {
|
||||
return Int64Or(in, defVal)
|
||||
}
|
||||
|
||||
// Int64OrErr convert string to int64, return error on failed
|
||||
// Int64Or convert value to int64, return default val on failed
|
||||
func Int64Or(in any, defVal int64) int64 {
|
||||
i64, err := ToInt64WithFunc(in, nil)
|
||||
if err != nil {
|
||||
return defVal
|
||||
}
|
||||
return i64
|
||||
}
|
||||
|
||||
// Int64OrErr convert value to int64, return error on failed
|
||||
func Int64OrErr(in any) (int64, error) {
|
||||
return ToInt64(in)
|
||||
}
|
||||
|
||||
// ToInt64 convert string to int64, return error on failed
|
||||
// ToInt64 convert value to int64, return error on failed
|
||||
func ToInt64(in any) (i64 int64, err error) {
|
||||
return ToInt64WithFunc(in, nil)
|
||||
}
|
||||
|
||||
// ToInt64WithFunc convert value to int64, will call usrFn on value type not supported.
|
||||
func ToInt64WithFunc(in any, usrFn ToInt64Func) (i64 int64, err error) {
|
||||
switch tVal := in.(type) {
|
||||
case nil:
|
||||
i64 = 0
|
||||
case string:
|
||||
i64, err = strconv.ParseInt(strings.TrimSpace(tVal), 10, 0)
|
||||
case int:
|
||||
@@ -230,10 +347,14 @@ func ToInt64(in any) (i64 int64, err error) {
|
||||
i64 = int64(tVal)
|
||||
case time.Duration:
|
||||
i64 = int64(tVal)
|
||||
case json.Number:
|
||||
case interface{ Int64() (int64, error) }: // eg: json.Number
|
||||
i64, err = tVal.Int64()
|
||||
default:
|
||||
err = comdef.ErrConvType
|
||||
if usrFn != nil {
|
||||
i64, err = usrFn(in)
|
||||
} else {
|
||||
err = comdef.ErrConvType
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -242,42 +363,63 @@ func ToInt64(in any) (i64 int64, err error) {
|
||||
* convert value to float
|
||||
*************************************************************/
|
||||
|
||||
// QuietFloat convert value to float64, will ignore error
|
||||
// QuietFloat convert value to float64, will ignore error. alias of SafeFloat
|
||||
func QuietFloat(in any) float64 {
|
||||
val, _ := ToFloat(in)
|
||||
return SafeFloat(in)
|
||||
}
|
||||
|
||||
// SafeFloat convert value to float64, will ignore error
|
||||
func SafeFloat(in any) float64 {
|
||||
val, _ := ToFloatWithFunc(in, nil)
|
||||
return val
|
||||
}
|
||||
|
||||
// FloatOrPanic convert value to float64, will panic on error
|
||||
func FloatOrPanic(in any) float64 {
|
||||
val, err := ToFloat(in)
|
||||
return MustFloat(in)
|
||||
}
|
||||
|
||||
// MustFloat convert value to float64, will panic on error
|
||||
func MustFloat(in any) float64 {
|
||||
val, err := ToFloatWithFunc(in, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// MustFloat convert value to float64 TODO will panic on error
|
||||
func MustFloat(in any) float64 {
|
||||
val, _ := ToFloat(in)
|
||||
// FloatOrDefault convert value to float64, will return default value on error
|
||||
func FloatOrDefault(in any, defVal float64) float64 {
|
||||
return FloatOr(in, defVal)
|
||||
}
|
||||
|
||||
// FloatOr convert value to float64, will return default value on error
|
||||
func FloatOr(in any, defVal float64) float64 {
|
||||
val, err := ToFloatWithFunc(in, nil)
|
||||
if err != nil {
|
||||
return defVal
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// Float convert value to float64, return error on failed
|
||||
func Float(in any) (float64, error) {
|
||||
return ToFloat(in)
|
||||
return ToFloatWithFunc(in, nil)
|
||||
}
|
||||
|
||||
// FloatOrErr convert value to float64, return error on failed
|
||||
func FloatOrErr(in any) (float64, error) {
|
||||
return ToFloat(in)
|
||||
return ToFloatWithFunc(in, nil)
|
||||
}
|
||||
|
||||
// ToFloat convert value to float64, return error on failed
|
||||
func ToFloat(in any) (f64 float64, err error) {
|
||||
return ToFloatWithFunc(in, nil)
|
||||
}
|
||||
|
||||
// ToFloatWithFunc convert value to float64, will call usrFn if value type not supported.
|
||||
func ToFloatWithFunc(in any, usrFn ToFloatFunc) (f64 float64, err error) {
|
||||
switch tVal := in.(type) {
|
||||
case nil:
|
||||
f64 = 0
|
||||
case string:
|
||||
f64, err = strconv.ParseFloat(strings.TrimSpace(tVal), 64)
|
||||
case int:
|
||||
@@ -306,10 +448,14 @@ func ToFloat(in any) (f64 float64, err error) {
|
||||
f64 = tVal
|
||||
case time.Duration:
|
||||
f64 = float64(tVal)
|
||||
case json.Number:
|
||||
case interface{ Float64() (float64, error) }: // eg: json.Number
|
||||
f64, err = tVal.Float64()
|
||||
default:
|
||||
err = comdef.ErrConvType
|
||||
if usrFn != nil {
|
||||
f64, err = usrFn(in)
|
||||
} else {
|
||||
err = comdef.ErrConvType
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -318,34 +464,45 @@ func ToFloat(in any) (f64 float64, err error) {
|
||||
* convert intX/floatX to string
|
||||
*************************************************************/
|
||||
|
||||
// StringOrPanic convert intX/floatX value to string, will panic on error
|
||||
func StringOrPanic(val any) string {
|
||||
str, err := TryToString(val, true)
|
||||
// MustString convert intX/floatX value to string, will panic on error
|
||||
func MustString(val any) string {
|
||||
str, err := ToStringWithFunc(val, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// MustString convert intX/floatX value to string, will panic on error
|
||||
func MustString(val any) string {
|
||||
return StringOrPanic(val)
|
||||
// StringOrPanic convert intX/floatX value to string, will panic on error
|
||||
func StringOrPanic(val any) string { return MustString(val) }
|
||||
|
||||
// StringOrDefault convert intX/floatX value to string, will return default value on error
|
||||
func StringOrDefault(val any, defVal string) string {
|
||||
return StringOr(val, defVal)
|
||||
}
|
||||
|
||||
// StringOr convert intX/floatX value to string, will return default value on error
|
||||
func StringOr(val any, defVal string) string {
|
||||
str, err := ToStringWithFunc(val, nil)
|
||||
if err != nil {
|
||||
return defVal
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
// ToString convert intX/floatX value to string, return error on failed
|
||||
func ToString(val any) (string, error) {
|
||||
return TryToString(val, true)
|
||||
return ToStringWithFunc(val, nil)
|
||||
}
|
||||
|
||||
// StringOrErr convert intX/floatX value to string, return error on failed
|
||||
func StringOrErr(val any) (string, error) {
|
||||
return TryToString(val, true)
|
||||
return ToStringWithFunc(val, nil)
|
||||
}
|
||||
|
||||
// QuietString convert intX/floatX value to string, other type convert by fmt.Sprint
|
||||
func QuietString(val any) string {
|
||||
str, _ := TryToString(val, false)
|
||||
return str
|
||||
return SafeString(val)
|
||||
}
|
||||
|
||||
// String convert intX/floatX value to string, other type convert by fmt.Sprint
|
||||
@@ -354,14 +511,33 @@ func String(val any) string {
|
||||
return str
|
||||
}
|
||||
|
||||
// SafeString convert intX/floatX value to string, other type convert by fmt.Sprint
|
||||
func SafeString(val any) string {
|
||||
str, _ := TryToString(val, false)
|
||||
return str
|
||||
}
|
||||
|
||||
// TryToString try convert intX/floatX value to string
|
||||
//
|
||||
// if defaultAsErr is False, will use fmt.Sprint convert other type
|
||||
func TryToString(val any, defaultAsErr bool) (str string, err error) {
|
||||
if val == nil {
|
||||
return
|
||||
var usrFn comdef.ToStringFunc
|
||||
if !defaultAsErr {
|
||||
usrFn = func(v any) (string, error) {
|
||||
if val == nil {
|
||||
return "", nil
|
||||
}
|
||||
return fmt.Sprint(v), nil
|
||||
}
|
||||
}
|
||||
|
||||
return ToStringWithFunc(val, usrFn)
|
||||
}
|
||||
|
||||
// ToStringWithFunc try convert intX/floatX value to string, will call usrFn if value type not supported.
|
||||
//
|
||||
// if defaultAsErr is False, will use fmt.Sprint convert other type
|
||||
func ToStringWithFunc(val any, usrFn comdef.ToStringFunc) (str string, err error) {
|
||||
switch value := val.(type) {
|
||||
case int:
|
||||
str = strconv.Itoa(value)
|
||||
@@ -389,14 +565,31 @@ func TryToString(val any, defaultAsErr bool) (str string, err error) {
|
||||
str = strconv.FormatFloat(value, 'f', -1, 64)
|
||||
case time.Duration:
|
||||
str = strconv.FormatInt(int64(value), 10)
|
||||
case string:
|
||||
str = value
|
||||
case fmt.Stringer:
|
||||
str = value.String()
|
||||
default:
|
||||
if defaultAsErr {
|
||||
err = comdef.ErrConvType
|
||||
if usrFn != nil {
|
||||
str, err = usrFn(val)
|
||||
} else {
|
||||
str = fmt.Sprint(value)
|
||||
err = comdef.ErrConvType
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Percent returns a values percent of the total
|
||||
func Percent(val, total int) float64 {
|
||||
if total == 0 {
|
||||
return float64(0)
|
||||
}
|
||||
return (float64(val) / float64(total)) * 100
|
||||
}
|
||||
|
||||
// ElapsedTime calc elapsed time 计算运行时间消耗 单位 ms(毫秒)
|
||||
//
|
||||
// Deprecated: use timex.ElapsedTime()
|
||||
func ElapsedTime(startTime time.Time) string {
|
||||
return fmt.Sprintf("%.3f", time.Since(startTime).Seconds()*1000)
|
||||
}
|
||||
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package mathutil
|
||||
|
||||
import "fmt"
|
||||
|
||||
// DataSize format bytes number friendly. eg: 1024 => 1KB, 1024*1024 => 1MB
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// file, err := os.Open(path)
|
||||
// fl, err := file.Stat()
|
||||
// fmtSize := DataSize(fl.Size())
|
||||
func DataSize(size uint64) string {
|
||||
switch {
|
||||
case size < 1024:
|
||||
return fmt.Sprintf("%dB", size)
|
||||
case size < 1024*1024:
|
||||
return fmt.Sprintf("%.2fK", float64(size)/1024)
|
||||
case size < 1024*1024*1024:
|
||||
return fmt.Sprintf("%.2fM", float64(size)/1024/1024)
|
||||
default:
|
||||
return fmt.Sprintf("%.2fG", float64(size)/1024/1024/1024)
|
||||
}
|
||||
}
|
||||
|
||||
var timeFormats = [][]int{
|
||||
{0},
|
||||
{1},
|
||||
{2, 1},
|
||||
{60},
|
||||
{120, 60},
|
||||
{3600},
|
||||
{7200, 3600},
|
||||
{86400},
|
||||
{172800, 86400}, // second elem is unit.
|
||||
{2592000},
|
||||
{2592000 * 2, 2592000},
|
||||
}
|
||||
|
||||
var timeMessages = []string{
|
||||
"< 1 sec", "1 sec", "secs", "1 min", "mins", "1 hr", "hrs", "1 day", "days", "1 month", "months",
|
||||
}
|
||||
|
||||
// HowLongAgo format a seconds, get how lang ago. eg: 1 day, 1 week
|
||||
func HowLongAgo(sec int64) string {
|
||||
intVal := int(sec)
|
||||
length := len(timeFormats)
|
||||
|
||||
for i, item := range timeFormats {
|
||||
if intVal >= item[0] {
|
||||
ni := i + 1
|
||||
match := false
|
||||
|
||||
if ni < length { // next exists
|
||||
next := timeFormats[ni]
|
||||
if intVal < next[0] { // current <= intVal < next
|
||||
match = true
|
||||
}
|
||||
} else if ni == length { // current is last
|
||||
match = true
|
||||
}
|
||||
|
||||
if match { // match success
|
||||
if len(item) == 1 {
|
||||
return timeMessages[i]
|
||||
}
|
||||
return fmt.Sprintf("%d %s", intVal/item[1], timeMessages[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "unknown" // He should never happen
|
||||
}
|
||||
+50
-62
@@ -2,84 +2,72 @@
|
||||
package mathutil
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/gookit/goutil/comdef"
|
||||
)
|
||||
|
||||
// Min compare two value and return max value
|
||||
func Min[T comdef.XintOrFloat](x, y T) T {
|
||||
if x < y {
|
||||
return x
|
||||
}
|
||||
return y
|
||||
// OrElse return default value on val is zero, else return val
|
||||
func OrElse[T comdef.XintOrFloat](val, defVal T) T {
|
||||
return ZeroOr(val, defVal)
|
||||
}
|
||||
|
||||
// Max compare two value and return max value
|
||||
func Max[T comdef.XintOrFloat](x, y T) T {
|
||||
if x > y {
|
||||
return x
|
||||
// ZeroOr return default value on val is zero, else return val
|
||||
func ZeroOr[T comdef.XintOrFloat](val, defVal T) T {
|
||||
if val != 0 {
|
||||
return val
|
||||
}
|
||||
return y
|
||||
return defVal
|
||||
}
|
||||
|
||||
// SwapMin compare and always return [min, max] value
|
||||
func SwapMin[T comdef.XintOrFloat](x, y T) (T, T) {
|
||||
if x < y {
|
||||
return x, y
|
||||
// LessOr return val on val < max, else return default value.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// LessOr(11, 10, 1) // 1
|
||||
// LessOr(2, 10, 1) // 2
|
||||
// LessOr(10, 10, 1) // 1
|
||||
func LessOr[T comdef.XintOrFloat](val, max, devVal T) T {
|
||||
if val < max {
|
||||
return val
|
||||
}
|
||||
return y, x
|
||||
return devVal
|
||||
}
|
||||
|
||||
// SwapMax compare and always return [max, min] value
|
||||
func SwapMax[T comdef.XintOrFloat](x, y T) (T, T) {
|
||||
if x > y {
|
||||
return x, y
|
||||
// LteOr return val on val <= max, else return default value.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// LteOr(11, 10, 1) // 11
|
||||
// LteOr(2, 10, 1) // 2
|
||||
// LteOr(10, 10, 1) // 10
|
||||
func LteOr[T comdef.XintOrFloat](val, max, devVal T) T {
|
||||
if val <= max {
|
||||
return val
|
||||
}
|
||||
return y, x
|
||||
return devVal
|
||||
}
|
||||
|
||||
// MaxInt compare and return max value
|
||||
func MaxInt(x, y int) int {
|
||||
if x > y {
|
||||
return x
|
||||
// GreaterOr return val on val > max, else return default value.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// GreaterOr(23, 0, 2) // 23
|
||||
// GreaterOr(0, 0, 2) // 2
|
||||
func GreaterOr[T comdef.XintOrFloat](val, min, defVal T) T {
|
||||
if val > min {
|
||||
return val
|
||||
}
|
||||
return y
|
||||
return defVal
|
||||
}
|
||||
|
||||
// SwapMaxInt compare and return max, min value
|
||||
func SwapMaxInt(x, y int) (int, int) {
|
||||
if x > y {
|
||||
return x, y
|
||||
// GteOr return val on val >= max, else return default value.
|
||||
//
|
||||
// Example:
|
||||
//
|
||||
// GteOr(23, 0, 2) // 23
|
||||
// GteOr(0, 0, 2) // 0
|
||||
func GteOr[T comdef.XintOrFloat](val, min, defVal T) T {
|
||||
if val >= min {
|
||||
return val
|
||||
}
|
||||
return y, x
|
||||
}
|
||||
|
||||
// MaxI64 compare and return max value
|
||||
func MaxI64(x, y int64) int64 {
|
||||
if x > y {
|
||||
return x
|
||||
}
|
||||
return y
|
||||
}
|
||||
|
||||
// SwapMaxI64 compare and return max, min value
|
||||
func SwapMaxI64(x, y int64) (int64, int64) {
|
||||
if x > y {
|
||||
return x, y
|
||||
}
|
||||
return y, x
|
||||
}
|
||||
|
||||
// MaxFloat compare and return max value
|
||||
func MaxFloat(x, y float64) float64 {
|
||||
return math.Max(x, y)
|
||||
}
|
||||
|
||||
// OrElse return s OR nv(new-value) on s is empty
|
||||
func OrElse[T comdef.XintOrFloat](in, nv T) T {
|
||||
if in != 0 {
|
||||
return in
|
||||
}
|
||||
return nv
|
||||
return defVal
|
||||
}
|
||||
|
||||
-37
@@ -1,37 +0,0 @@
|
||||
package mathutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gookit/goutil/basefn"
|
||||
)
|
||||
|
||||
// IsNumeric returns true if the given character is a numeric, otherwise false.
|
||||
func IsNumeric(c byte) bool {
|
||||
return c >= '0' && c <= '9'
|
||||
}
|
||||
|
||||
// Percent returns a values percent of the total
|
||||
func Percent(val, total int) float64 {
|
||||
if total == 0 {
|
||||
return float64(0)
|
||||
}
|
||||
return (float64(val) / float64(total)) * 100
|
||||
}
|
||||
|
||||
// ElapsedTime calc elapsed time 计算运行时间消耗 单位 ms(毫秒)
|
||||
func ElapsedTime(startTime time.Time) string {
|
||||
return fmt.Sprintf("%.3f", time.Since(startTime).Seconds()*1000)
|
||||
}
|
||||
|
||||
// DataSize format value to data size string. eg: 1024 => 1KB, 1024*1024 => 1MB
|
||||
// alias format.DataSize()
|
||||
func DataSize(size uint64) string {
|
||||
return basefn.DataSize(size)
|
||||
}
|
||||
|
||||
// HowLongAgo calc time. alias format.HowLongAgo()
|
||||
func HowLongAgo(sec int64) string {
|
||||
return basefn.HowLongAgo(sec)
|
||||
}
|
||||
+4
-4
@@ -13,8 +13,8 @@ import (
|
||||
// RandomInt(100, 999)
|
||||
// RandomInt(1000, 9999)
|
||||
func RandomInt(min, max int) int {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
return min + rand.Intn(max-min)
|
||||
rr := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
return min + rr.Intn(max-min)
|
||||
}
|
||||
|
||||
// RandInt alias of RandomInt()
|
||||
@@ -32,6 +32,6 @@ func RandIntWithSeed(min, max int, seed int64) int {
|
||||
// seed := time.Now().UnixNano()
|
||||
// RandomIntWithSeed(1000, 9999, seed)
|
||||
func RandomIntWithSeed(min, max int, seed int64) int {
|
||||
rand.Seed(seed)
|
||||
return min + rand.Intn(max-min)
|
||||
rr := rand.New(rand.NewSource(seed))
|
||||
return min + rr.Intn(max-min)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user