Skip to content

Commit

Permalink
Trim leading invalid chars from names (#151)
Browse files Browse the repository at this point in the history
Signed-off-by: Reinhard Nägele <[email protected]>
  • Loading branch information
unguiculus authored Apr 24, 2019
1 parent 961a3ab commit f1712a6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,13 @@ func PrintDelimiterLine(delimiterChar string) {

func SanitizeName(s string, maxLength int) string {
reg := regexp.MustCompile("^[^a-zA-Z0-9]+")
processed := reg.ReplaceAllString(s, "")

excess := len(processed) - maxLength
excess := len(s) - maxLength
result := s
if excess > 0 {
return processed[excess:]
result = s[excess:]
}
return processed
return reg.ReplaceAllString(result, "")
}

func GetRandomPort() (int, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestSanitizeName(t *testing.T) {
{"way-longer-than-max-length", 10, "max-length"},
{"one-shorter-than-max-length", len("one-shorter-than-max-length") + 1, "one-shorter-than-max-length"},
{"oone-longer-than-max-length", len("oone-longer-than-max-length") - 1, "one-longer-than-max-length"},
{"-starts-with-invalid-char", 63, "starts-with-invalid-char"},
{"foo-would-start-with-hyphen-after-trimming", len("foo-would-start-with-hyphen-after-trimming") - 3, "would-start-with-hyphen-after-trimming"},
}

for index, testData := range testDataSlice {
Expand Down

0 comments on commit f1712a6

Please sign in to comment.