From ecfb70eaaed2bc0707dc53003e04882747109668 Mon Sep 17 00:00:00 2001 From: ccpwcn Date: Tue, 19 Dec 2023 18:13:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=AD=E5=9B=BD=E5=A7=93=E5=90=8D=E8=84=B1?= =?UTF-8?q?=E6=95=8F=E5=A2=9E=E5=BC=BA=E6=96=B9=E6=B3=95=E6=94=B9=E8=BF=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- str.go | 6 +++--- str_test.go | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d936d0b..fc838b1 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ - B2S byte切片转为string - S2B string转为byte切片 - MaskChineseName 中文姓名脱敏 - - MaskChineseNameEx 中文姓名脱敏扩展方法,可指定脱敏位,与身份证号、手机号的脱敏不同,姓名脱敏函数中指定数量的字符会被以星号替代 + - MaskChineseNameEx 中文姓名脱敏扩展方法,可指定脱敏位 - MaskChineseMobile 中国手机号脱敏 - MaskChineseIdCard 中国身份证号脱敏 - MaskChineseIdCard34 中国身份证号脱敏,MaskChineseMobile简化版,保留前3位后4位 diff --git a/str.go b/str.go index 5fda93f..816ba02 100644 --- a/str.go +++ b/str.go @@ -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) @@ -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++ } diff --git a/str_test.go b/str_test.go index 7e35c52..eaef166 100644 --- a/str_test.go +++ b/str_test.go @@ -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) } @@ -145,7 +145,7 @@ 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) } @@ -153,8 +153,8 @@ func TestMaskChineseNameEx1(t *testing.T) { 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) } @@ -162,8 +162,8 @@ func TestMaskChineseNameEx2(t *testing.T) { 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) }