Skip to content

Commit

Permalink
fix id normalization to not drop characters
Browse files Browse the repository at this point in the history
  • Loading branch information
conorpp committed Aug 23, 2024
1 parent b163d15 commit 20433b7
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions normalize/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package normalize_test

import (
"strings"
"testing"
"unicode"

"github.com/stretchr/testify/require"
)

var punctuationReplacer = strings.NewReplacer(
Expand Down Expand Up @@ -34,14 +37,22 @@ func normalizeResourceId(id string) string {
// drop everything else that is not valid
var sb strings.Builder
for _, c := range id {
if c < unicode.MaxASCII {
if unicode.IsLetter(c) || unicode.IsDigit(c) || c == '-' || c == '_' {
sb.WriteRune(c)
} else {
// drop
}
if unicode.IsLetter(c) || unicode.IsDigit(c) || c == '-' || c == '_' {
sb.WriteRune(c)
} else {
// replace with -
sb.WriteRune('-')
}
}

return sb.String()
}

func TestNormalize(t *testing.T) {
require.Equal(t, "abc1234", normalizeResourceId("abc1234"))
require.Equal(t, "abc-1234", normalizeResourceId("abc.1234"))
require.Equal(t, "abc-1234", normalizeResourceId("abc/1234"))
require.Equal(t, "abc-1234", normalizeResourceId("abc🙊1234"))
require.Equal(t, "abc----1234", normalizeResourceId("abc/**/1234"))
require.Equal(t, "-_abc----1234", normalizeResourceId(" . abc/**/1234"))
}

0 comments on commit 20433b7

Please sign in to comment.