From 19fdadd3ecbe57e5553b28e51fd1c80d5d2f1c21 Mon Sep 17 00:00:00 2001 From: VicRen Date: Fri, 15 Jan 2021 09:04:30 +0800 Subject: [PATCH] 20210115 --- 20210115/main.go | 47 +++++++++++++++++++++++++++++++++++++++++++ 20210115/main_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 20210115/main.go create mode 100644 20210115/main_test.go diff --git a/20210115/main.go b/20210115/main.go new file mode 100644 index 0000000..0ad14c6 --- /dev/null +++ b/20210115/main.go @@ -0,0 +1,47 @@ +package main + +import "strings" + +func main() { + +} + +func maskPII(s string) string { + if strings.Contains(s, "@") { + return maskEmail(s) + } + return maskPhone(s) +} + +func maskEmail(s string) string { + ss := strings.Split(s, "@") + name := ss[0] + name = strings.ToLower(name) + sb := strings.Builder{} + sb.WriteByte(name[0]) + sb.WriteString("*****") + sb.WriteByte(name[len(name)-1]) + return sb.String() + "@" + strings.ToLower(ss[1]) +} + +func maskPhone(s string) string { + sb := strings.Builder{} + for _, c := range s { + if c >= '0' && c <= '9' { + sb.WriteByte(uint8(c)) + } + } + phone := sb.String() + sb = strings.Builder{} + ccLen := len(phone) - 10 + if ccLen > 0 { + sb.WriteString("+") + for i := 0; i < ccLen; i++ { + sb.WriteString("*") + } + sb.WriteString("-") + } + sb.WriteString("***-***-") + sb.WriteString(phone[len(phone)-4:]) + return sb.String() +} diff --git a/20210115/main_test.go b/20210115/main_test.go new file mode 100644 index 0000000..3e1a4da --- /dev/null +++ b/20210115/main_test.go @@ -0,0 +1,47 @@ +package main + +import "testing" + +func Test_maskPII(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want string + }{ + { + "test1", + args{"LeetCode@LeetCode.com"}, + "l*****e@leetcode.com", + }, + { + "test2", + args{"AB@qq.com"}, + "a*****b@qq.com", + }, + { + "test3", + args{"1(234)567-890"}, + "***-***-7890", + }, + { + "test4", + args{"86-(10)12345678"}, + "+**-***-***-5678", + }, + { + "test5", + args{"+(501321)-50-23431"}, + "+***-***-***-3431", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := maskPII(tt.args.s); got != tt.want { + t.Errorf("maskPII() = %v, want %v", got, tt.want) + } + }) + } +}