Add a function to sanitize nicknames

This commit is contained in:
Dmitrii Okunev
2025-08-15 01:20:45 +01:00
parent cebe1ee21b
commit e3872a74f9
2 changed files with 48 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
package xstring
import (
"strings"
"unicode"
"golang.org/x/text/unicode/norm"
)
func ToReadable(s string) string {
plain := norm.NFKD.String(s)
var b strings.Builder
for _, r := range plain {
// Remove symbols (unicode.Symbol), keep everything else (including foreign letters)
if !unicode.IsSymbol(r) {
b.WriteRune(r)
}
}
return strings.Trim(b.String(), " ,\t\n\r")
}

View File

@@ -0,0 +1,28 @@
package xstring
import (
"testing"
)
func TestToReadable(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"", ""},
{"Hello, world!", "Hello, world!"},
{"Café", "Café"},
{"e\u0301", "é"},
{"Go语言", "Go语言"},
{"𝔘𝔫𝔦𝔠𝔬𝔡𝔢", "Unicode"},
{"🥛 , 𝑚𝑖𝐼𝐊𝐞𝐔!+", "miIKeU!"},
{"𝕱𝖗𝖆𝖓ç𝖔𝖎𝖘𝖊 & Dᵢₑ𝘴ₑ 🇫🇷", "Françoise & Diese"},
}
for _, tt := range tests {
result := ToReadable(tt.input)
if result != tt.expected {
t.Errorf("ToReadable(%q) = %q; want %q", tt.input, result, tt.expected)
}
}
}