nip54: improve normalization and add tests.

This commit is contained in:
fiatjaf
2024-03-13 16:36:08 -03:00
parent c0f1c4f510
commit 6f32e4da63
2 changed files with 37 additions and 5 deletions

View File

@@ -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)
}