mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-08-23 12:12:04 +02:00
nip54: improve normalization and add tests.
This commit is contained in:
@@ -1,17 +1,26 @@
|
||||
package nip54
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"golang.org/x/text/transform"
|
||||
"golang.org/x/text/unicode/norm"
|
||||
)
|
||||
|
||||
var nonLetter = regexp.MustCompile(`\W`)
|
||||
|
||||
func NormalizeIdentifier(name string) string {
|
||||
name = strings.ToLower(name)
|
||||
res, _, _ := transform.Bytes(norm.NFKC, []byte(name))
|
||||
str := nonLetter.ReplaceAllString(string(res), "-")
|
||||
return strings.ToLower(str)
|
||||
runes := []rune(string(res))
|
||||
|
||||
b := make([]rune, len(runes))
|
||||
for i, letter := range runes {
|
||||
if unicode.IsLetter(letter) {
|
||||
b[i] = letter
|
||||
} else {
|
||||
b[i] = '-'
|
||||
}
|
||||
}
|
||||
|
||||
return string(b)
|
||||
}
|
||||
|
Reference in New Issue
Block a user