diff --git a/libs/auth/user_test.go b/libs/auth/user_test.go index eb579fc988..62b2d29ac6 100644 --- a/libs/auth/user_test.go +++ b/libs/auth/user_test.go @@ -22,7 +22,7 @@ func TestGetShortUserName(t *testing.T) { }, { email: "test$.user@example.com", - expected: "test__user", + expected: "test_user", }, { email: `jöhn.dœ@domain.com`, // Using non-ASCII characters. @@ -38,7 +38,7 @@ func TestGetShortUserName(t *testing.T) { }, { email: `"_quoted"@domain.com`, // Quoted strings can be part of the local-part. - expected: "__quoted_", + expected: "quoted", }, { email: `name-o'mally@website.org`, // Single quote in the local-part. diff --git a/libs/textutil/textutil.go b/libs/textutil/textutil.go index a5d17d55f5..ee9b0f0f1c 100644 --- a/libs/textutil/textutil.go +++ b/libs/textutil/textutil.go @@ -1,6 +1,7 @@ package textutil import ( + "regexp" "strings" "unicode" ) @@ -9,7 +10,14 @@ import ( // including spaces and dots, which are not supported in e.g. experiment names or YAML keys. func NormalizeString(name string) string { name = strings.ToLower(name) - return strings.Map(replaceNonAlphanumeric, name) + s := strings.Map(replaceNonAlphanumeric, name) + + // replacing multiple underscores with a single one + re := regexp.MustCompile(`_+`) + s = re.ReplaceAllString(s, "_") + + // removing leading and trailing underscores + return strings.Trim(s, "_") } func replaceNonAlphanumeric(r rune) rune { diff --git a/libs/textutil/textutil_test.go b/libs/textutil/textutil_test.go index fb8bf0b608..f6834a1ef3 100644 --- a/libs/textutil/textutil_test.go +++ b/libs/textutil/textutil_test.go @@ -46,6 +46,10 @@ func TestNormalizeString(t *testing.T) { { input: "TestTestTest", expected: "testtesttest", + }, + { + input: ".test//test..test", + expected: "test_test_test", }} for _, c := range cases {