diff --git a/str.go b/str.go index 816ba02..227834b 100644 --- a/str.go +++ b/str.go @@ -85,7 +85,13 @@ func MaskChineseName(name string) (masked string) { } // MaskChineseNameEx 中文姓名脱敏,可以指定左右保留字符数量 -// 示例:MaskChineseNameEx("张一二", 1, 1) => "*一*" 左边1位替代、右边1位替代 +// +// 示例1:MaskChineseNameEx("张一二", 1, 1) => "张*二" 左边保留1个字符、右边保留1个字符 +// +// 示例2: +// +// name := "张一二" +// MaskChineseNameEx(name, 0, utf8.RuneCountInString(name)-1) => "*一二" 左边保留0个字符、右边保留所有字符少1个 func MaskChineseNameEx(name string, left, right int) (masked string) { size := utf8.RuneCountInString(name) if size == 0 { diff --git a/str_test.go b/str_test.go index eaef166..ae54868 100644 --- a/str_test.go +++ b/str_test.go @@ -1,6 +1,9 @@ package kgo -import "testing" +import ( + "testing" + "unicode/utf8" +) func TestClean(t *testing.T) { resources := map[string]string{ @@ -169,6 +172,24 @@ func TestMaskChineseNameEx4(t *testing.T) { } } +func TestMaskChineseNameEx5(t *testing.T) { + name := "张一二" + excepted := "张*二" + actual := MaskChineseNameEx(name, 1, 1) + if actual != excepted { + t.Fatalf("预期 %v,实际值:%v", excepted, actual) + } +} + +func TestMaskChineseNameEx6(t *testing.T) { + name := "张一二" + excepted := "*一二" + actual := MaskChineseNameEx(name, 0, utf8.RuneCountInString(name)-1) + if actual != excepted { + t.Fatalf("预期 %v,实际值:%v", excepted, actual) + } +} + func TestMaskChineseMobile(t *testing.T) { tel := "13012345678" excepted := "130****5678"