mirror of
https://github.com/xaionaro-go/streamctl.git
synced 2025-10-15 12:00:41 +08:00
Add a function to sanitize nicknames
This commit is contained in:
20
pkg/xstring/to_readable.go
Normal file
20
pkg/xstring/to_readable.go
Normal 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")
|
||||||
|
}
|
28
pkg/xstring/to_readable_test.go
Normal file
28
pkg/xstring/to_readable_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user