diff --git a/pkg/handle/handle.go b/pkg/handle/handle.go new file mode 100644 index 000000000..054e846c6 --- /dev/null +++ b/pkg/handle/handle.go @@ -0,0 +1,13 @@ +package handle + +import ( + "regexp" +) + +var ( + c = regexp.MustCompile(`^[A-Za-z][0-9A-Za-z_\-.]*[A-Za-z0-9]$`) +) + +func IsValid(s string) bool { + return s == "" || (len(s) >= 2 && c.MatchString(s)) +} diff --git a/pkg/handle/handle_test.go b/pkg/handle/handle_test.go new file mode 100644 index 000000000..5628e4271 --- /dev/null +++ b/pkg/handle/handle_test.go @@ -0,0 +1,30 @@ +package handle + +import ( + "testing" +) + +func TestIsValid(t *testing.T) { + tests := []struct { + name string + handle string + want bool + }{ + // TODO: Add test cases. + {"empty", "", true}, + {"alphanum", "a1", true}, + {"alpha", "abc", true}, + {"num", "123", false}, + {"valid1", "Valid1Handle", true}, + {"valid2", "Valid-Handle", true}, + {"valid3", "Valid_Handle", true}, + {"weirdo", "a$&_.!", false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := IsValid(tt.handle); got != tt.want { + t.Errorf("IsValid() = %v, want %v", got, tt.want) + } + }) + } +}