mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 08:32:40 +08:00
update
This commit is contained in:
8
stringx/append.go
Normal file
8
stringx/append.go
Normal 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
8
stringx/append_test.go
Normal file
@ -0,0 +1,8 @@
|
||||
package stringx_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAppend(t *testing.T) {
|
||||
}
|
40
stringx/mask.go
Normal file
40
stringx/mask.go
Normal 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
28
stringx/mask_test.go
Normal 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))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user