1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-08 22:52:29 +08:00

feat: add IsInt, IsFloat, IsNumber

This commit is contained in:
dudaodong
2023-04-05 14:07:23 +08:00
parent c01c9d14b4
commit 046f3e0bf9
5 changed files with 156 additions and 49 deletions

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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")