diff --git a/formatter/formatter.go b/formatter/formatter.go index a91c5c3..8e71051 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -5,11 +5,6 @@ package formatter import ( - "fmt" - "reflect" - "strconv" - "strings" - "golang.org/x/exp/constraints" ) @@ -18,54 +13,57 @@ import ( // Comma("12345", "$") => "$12,345", Comma(12345, "$") => "$12,345" // Play: https://go.dev/play/p/eRD5k2vzUVX func Comma[T constraints.Float | constraints.Integer | string](value T, symbol string) string { - s, err := numberToString(value) - if err != nil { - return "" - } + // s, err := numberToString(value) + // if err != nil { + // return "" + // } - dotIndex := strings.Index(s, ".") - if dotIndex != -1 { - return symbol + commaString(s[:dotIndex]) + s[dotIndex:] - } + // dotIndex := strings.Index(s, ".") + // if dotIndex != -1 { + // return symbol + commaString(s[:dotIndex]) + s[dotIndex:] + // } + + // return symbol + commaString(s) + + return "" - return symbol + commaString(s) } -func commaString(s string) string { - if len(s) <= 3 { - return s - } - return commaString(s[:len(s)-3]) + "," + commaString(s[len(s)-3:]) -} +// func commaString(s string) string { +// if len(s) <= 3 { +// return s +// } +// return commaString(s[:len(s)-3]) + "," + commaString(s[len(s)-3:]) +// } -func numberToString(value any) (string, error) { - switch reflect.TypeOf(value).Kind() { - case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, - reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: - return fmt.Sprintf("%v", value), nil +// func numberToString(value any) (string, error) { +// switch reflect.TypeOf(value).Kind() { +// case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, +// reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: +// return fmt.Sprintf("%v", value), nil - // todo: need to handle 12345678.9 => 1.23456789e+07 - case reflect.Float32, reflect.Float64: - return fmt.Sprintf("%v", value), nil +// // todo: need to handle 12345678.9 => 1.23456789e+07 +// case reflect.Float32, reflect.Float64: +// return fmt.Sprintf("%v", value), nil - case reflect.String: - { - sv := fmt.Sprintf("%v", value) - if strings.Contains(sv, ".") { - _, err := strconv.ParseFloat(sv, 64) - if err != nil { - return "", err - } - return sv, nil - } else { - _, err := strconv.ParseInt(sv, 10, 64) - if err != nil { - return "", nil - } - return sv, nil - } - } - default: - return "", nil - } -} +// case reflect.String: +// { +// sv := fmt.Sprintf("%v", value) +// if strings.Contains(sv, ".") { +// _, err := strconv.ParseFloat(sv, 64) +// if err != nil { +// return "", err +// } +// return sv, nil +// } else { +// _, err := strconv.ParseInt(sv, 10, 64) +// if err != nil { +// return "", nil +// } +// return sv, nil +// } +// } +// default: +// return "", nil +// } +// } diff --git a/formatter/formatter_test.go b/formatter/formatter_test.go index 2b2d93d..5f82148 100644 --- a/formatter/formatter_test.go +++ b/formatter/formatter_test.go @@ -16,6 +16,7 @@ func TestComma(t *testing.T) { assert.Equal("12,345", Comma("12345", "")) assert.Equal("12,345.6789", Comma("12345.6789", "")) assert.Equal("123,456,789,000", Comma("123456789000", "")) + assert.Equal("12,345,678.9", Comma("12345678.9", "")) assert.Equal("12,345", Comma(12345, "")) assert.Equal("$12,345", Comma(12345, "$")) diff --git a/validator/validator.go b/validator/validator.go index abc2b5c..fc26b4b 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -369,3 +369,29 @@ func IsGBK(data []byte) bool { return true } + +// IsNumberStr check if the value is Number(integer, float) or not. +// Play: todo +func IsNumber(v any) bool { + return IsInt(v) || IsFloat(v) +} + +// IsFloat check if the value is float(float32, float34) or not. +// Play: todo +func IsFloat(v any) bool { + switch v.(type) { + case float32, float64: + return true + } + return false +} + +// IsInt check if the value is integer(int, unit) or not. +// Play: todo +func IsInt(v any) bool { + switch v.(type) { + case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr: + return true + } + return false +} diff --git a/validator/validator_example_test.go b/validator/validator_example_test.go index 8b36812..e59a0b8 100644 --- a/validator/validator_example_test.go +++ b/validator/validator_example_test.go @@ -449,3 +449,57 @@ func ExampleIsPrintable() { // true // false } + +func ExampleIsInt() { + result1 := IsInt("") + result2 := IsInt("3") + result3 := IsInt(0.1) + result4 := IsInt(0) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // false + // false + // false + // true +} + +func ExampleIsFloat() { + result1 := IsFloat("") + result2 := IsFloat("3") + result3 := IsFloat(0) + result4 := IsFloat(0.1) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // false + // false + // false + // true +} + +func ExampleIsNumber() { + result1 := IsNumber("") + result2 := IsNumber("3") + result3 := IsNumber(0) + result4 := IsNumber(0.1) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // false + // false + // true + // true +} diff --git a/validator/validator_test.go b/validator/validator_test.go index 3e5560e..b8a3f37 100644 --- a/validator/validator_test.go +++ b/validator/validator_test.go @@ -100,6 +100,34 @@ func TestIsJSON(t *testing.T) { assert.Equal(false, IsJSON("&@#$%^&*")) } +func TestIsNumber(t *testing.T) { + assert := internal.NewAssert(t, "TestIsNumber") + + assert.Equal(false, IsNumber("")) + assert.Equal(false, IsNumber("3")) + assert.Equal(true, IsNumber(0)) + assert.Equal(true, IsNumber(0.1)) +} + +func TestIsFloat(t *testing.T) { + assert := internal.NewAssert(t, "TestIsFloat") + + assert.Equal(false, IsFloat("")) + assert.Equal(false, IsFloat("3")) + assert.Equal(false, IsFloat(0)) + assert.Equal(true, IsFloat(0.1)) +} + +func TestIsInt(t *testing.T) { + assert := internal.NewAssert(t, "TestIsInt") + + assert.Equal(false, IsInt("")) + assert.Equal(false, IsInt("3")) + assert.Equal(false, IsInt(0.1)) + assert.Equal(true, IsInt(0)) + assert.Equal(true, IsInt(-1)) +} + func TestIsNumberStr(t *testing.T) { assert := internal.NewAssert(t, "TestIsNumberStr")