1
0
mirror of https://github.com/charlienet/go-mixed.git synced 2025-07-18 08:32:40 +08:00
This commit is contained in:
2023-08-25 15:31:00 +08:00
parent 04aecd4abc
commit b0a97978d8
58 changed files with 1330 additions and 476 deletions

8
stringx/append.go Normal file
View File

@ -0,0 +1,8 @@
package stringx
import "fmt"
func Append[E any](dst []byte, e E) []byte {
toAppend := fmt.Sprintf("%v", e)
return append(dst, []byte(toAppend)...)
}

8
stringx/append_test.go Normal file
View File

@ -0,0 +1,8 @@
package stringx_test
import (
"testing"
)
func TestAppend(t *testing.T) {
}

40
stringx/mask.go Normal file
View File

@ -0,0 +1,40 @@
package stringx
import (
"strings"
)
type Place int
const (
Begin Place = iota
Middle
End
)
func Mask(s string, place Place, length int, mask ...rune) string {
m := '*'
if len(mask) > 0 {
m = mask[0]
}
n := len(s)
if length >= n {
return strings.Repeat(string(m), n)
}
i := 0
if place == Middle {
i = (n - length) / 2
} else if place == End {
i = n - length
}
end := i + length
r := []rune(s)
for ; i < end; i++ {
r[i] = m
}
return string(r)
}

28
stringx/mask_test.go Normal file
View File

@ -0,0 +1,28 @@
package stringx_test
import (
"testing"
"github.com/charlienet/go-mixed/stringx"
"github.com/stretchr/testify/assert"
)
func TestMask(t *testing.T) {
cases := []struct {
origin string
place stringx.Place
length int
excepted string
}{
{"aa", stringx.Begin, 6, "**"},
{"18980832408", stringx.Begin, 4, "****0832408"},
{"18980832408", stringx.End, 4, "1898083****"},
{"18980832408", stringx.Middle, 4, "189****2408"},
}
a := assert.New(t)
for _, c := range cases {
t.Log(stringx.Mask(c.origin, c.place, c.length))
a.Equal(c.excepted, stringx.Mask(c.origin, c.place, c.length))
}
}