Skip to content

Commit e3872a7

Browse files
committed
Add a function to sanitize nicknames
1 parent cebe1ee commit e3872a7

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

pkg/xstring/to_readable.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package xstring
2+
3+
import (
4+
"strings"
5+
"unicode"
6+
7+
"golang.org/x/text/unicode/norm"
8+
)
9+
10+
func ToReadable(s string) string {
11+
plain := norm.NFKD.String(s)
12+
var b strings.Builder
13+
for _, r := range plain {
14+
// Remove symbols (unicode.Symbol), keep everything else (including foreign letters)
15+
if !unicode.IsSymbol(r) {
16+
b.WriteRune(r)
17+
}
18+
}
19+
return strings.Trim(b.String(), " ,\t\n\r")
20+
}

pkg/xstring/to_readable_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package xstring
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestToReadable(t *testing.T) {
8+
tests := []struct {
9+
input string
10+
expected string
11+
}{
12+
{"", ""},
13+
{"Hello, world!", "Hello, world!"},
14+
{"Café", "Café"},
15+
{"e\u0301", "é"},
16+
{"Go语言", "Go语言"},
17+
{"𝔘𝔫𝔦𝔠𝔬𝔡𝔢", "Unicode"},
18+
{"🥛 , 𝑚𝑖𝐼𝐊𝐞𝐔!+", "miIKeU!"},
19+
{"𝕱𝖗𝖆𝖓ç𝖔𝖎𝖘𝖊 & Dᵢₑ𝘴ₑ 🇫🇷", "Françoise & Diese"},
20+
}
21+
22+
for _, tt := range tests {
23+
result := ToReadable(tt.input)
24+
if result != tt.expected {
25+
t.Errorf("ToReadable(%q) = %q; want %q", tt.input, result, tt.expected)
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)