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

feat: move new functions from v2 version

This commit is contained in:
dudaodong
2023-04-19 15:49:07 +08:00
parent 6576b1f0cb
commit ee44526d9e
26 changed files with 2105 additions and 119 deletions

View File

@@ -316,3 +316,49 @@ func IsGBK(data []byte) bool {
return true
}
// IsNumberStr check if the value is number(integer, float) or not.
func IsNumber(v interface{}) bool {
return IsInt(v) || IsFloat(v)
}
// IsFloat check if the value is float(float32, float34) or not.
func IsFloat(v interface{}) bool {
switch v.(type) {
case float32, float64:
return true
}
return false
}
// IsInt check if the value is integer(int, unit) or not.
func IsInt(v interface{}) bool {
switch v.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr:
return true
}
return false
}
// IsASCII checks if string is all ASCII char.
func IsASCII(str string) bool {
for i := 0; i < len(str); i++ {
if str[i] > unicode.MaxASCII {
return false
}
}
return true
}
// IsPrintable checks if string is all printable chars.
func IsPrintable(str string) bool {
for _, r := range str {
if !unicode.IsPrint(r) {
if r == '\n' || r == '\r' || r == '\t' || r == '`' {
continue
}
return false
}
}
return true
}

View File

@@ -400,3 +400,51 @@ func TestIsGBK(t *testing.T) {
assert.Equal(true, IsGBK(gbkData))
assert.Equal(false, utf8.Valid(gbkData))
}
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 TestIsASCII(t *testing.T) {
assert := internal.NewAssert(t, "TestIsASCII")
assert.Equal(true, IsASCII("ABC"))
assert.Equal(true, IsASCII("123"))
assert.Equal(true, IsASCII(""))
assert.Equal(false, IsASCII("😄"))
assert.Equal(false, IsASCII("你好"))
}
func TestIsPrintable(t *testing.T) {
assert := internal.NewAssert(t, "TestIsPrintable")
assert.Equal(true, IsPrintable("ABC"))
assert.Equal(true, IsPrintable("{id: 123}"))
assert.Equal(true, IsPrintable(""))
assert.Equal(true, IsPrintable("😄"))
assert.Equal(false, IsPrintable("\u0000"))
}