From 6f32e4da63bb7a14b1903a5c3d5d25abead334f2 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Wed, 13 Mar 2024 16:36:08 -0300 Subject: [PATCH] nip54: improve normalization and add tests. --- nip54/nip54.go | 19 ++++++++++++++----- nip54/nip54_test.go | 23 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 nip54/nip54_test.go diff --git a/nip54/nip54.go b/nip54/nip54.go index 4b06f14..1e0a09d 100644 --- a/nip54/nip54.go +++ b/nip54/nip54.go @@ -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) } diff --git a/nip54/nip54_test.go b/nip54/nip54_test.go new file mode 100644 index 0000000..9788fd8 --- /dev/null +++ b/nip54/nip54_test.go @@ -0,0 +1,23 @@ +package nip54 + +import ( + "fmt" + "testing" +) + +func TestNormalization(t *testing.T) { + for _, vector := range []struct { + before string + after string + }{ + {"hello", "hello"}, + {"Goodbye", "goodbye"}, + {"the long and winding road / that leads to your door", "the-long-and-winding-road---that-leads-to-your-door"}, + {"it's 平仮名", "it-s-平仮名"}, + } { + if norm := NormalizeIdentifier(vector.before); norm != vector.after { + fmt.Println([]byte(vector.after), []byte(norm)) + t.Fatalf("%s: %s != %s", vector.before, norm, vector.after) + } + } +}