Skip to content

Commit

Permalink
Merge pull request #39 from ccpwcn/dev
Browse files Browse the repository at this point in the history
中国姓名脱敏增强方法改进
  • Loading branch information
ccpwcn authored Dec 19, 2023
2 parents 66b85e3 + ecfb70e commit f454d76
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
- B2S byte切片转为string
- S2B string转为byte切片
- MaskChineseName 中文姓名脱敏
- MaskChineseNameEx 中文姓名脱敏扩展方法,可指定脱敏位,与身份证号、手机号的脱敏不同,姓名脱敏函数中指定数量的字符会被以星号替代
- MaskChineseNameEx 中文姓名脱敏扩展方法,可指定脱敏位
- MaskChineseMobile 中国手机号脱敏
- MaskChineseIdCard 中国身份证号脱敏
- MaskChineseIdCard34 中国身份证号脱敏,MaskChineseMobile简化版,保留前3位后4位
Expand Down
6 changes: 3 additions & 3 deletions str.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func MaskChineseName(name string) (masked string) {
return masked
}

// MaskChineseNameEx 中文姓名脱敏,指定的左右脱敏长度会被以星号取代
// MaskChineseNameEx 中文姓名脱敏,可以指定左右保留字符数量
// 示例:MaskChineseNameEx("张一二", 1, 1) => "*一*" 左边1位替代、右边1位替代
func MaskChineseNameEx(name string, left, right int) (masked string) {
size := utf8.RuneCountInString(name)
Expand All @@ -94,9 +94,9 @@ func MaskChineseNameEx(name string, left, right int) (masked string) {
i := 0
for _, n := range name {
if i < left || i >= size-right {
masked += "*"
} else {
masked += fmt.Sprintf("%c", n)
} else {
masked += "*"
}
i++
}
Expand Down
12 changes: 6 additions & 6 deletions str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestMaskChineseName4(t *testing.T) {
func TestMaskChineseNameEx(t *testing.T) {
name := "张三"
excepted := "*三"
actual := MaskChineseNameEx(name, 1, 0)
actual := MaskChineseNameEx(name, 0, 1)
if actual != excepted {
t.Fatalf("预期 %v,实际值:%v", excepted, actual)
}
Expand All @@ -145,25 +145,25 @@ func TestMaskChineseNameEx(t *testing.T) {
func TestMaskChineseNameEx1(t *testing.T) {
name := "张三一"
excepted := "*三一"
actual := MaskChineseNameEx(name, 1, 0)
actual := MaskChineseNameEx(name, 0, 2)
if actual != excepted {
t.Fatalf("预期 %v,实际值:%v", excepted, actual)
}
}

func TestMaskChineseNameEx2(t *testing.T) {
name := "张三三三"
excepted := "*三三*"
actual := MaskChineseNameEx(name, 1, 1)
excepted := "*三三三"
actual := MaskChineseNameEx(name, 0, 3)
if actual != excepted {
t.Fatalf("预期 %v,实际值:%v", excepted, actual)
}
}

func TestMaskChineseNameEx4(t *testing.T) {
name := "hello"
excepted := "*ell*"
actual := MaskChineseNameEx(name, 1, 1)
excepted := "*ello"
actual := MaskChineseNameEx(name, 0, 4)
if actual != excepted {
t.Fatalf("预期 %v,实际值:%v", excepted, actual)
}
Expand Down

0 comments on commit f454d76

Please sign in to comment.