Add more uppercase words to titlecase

This commit is contained in:
Patrick O'brien 2016-07-17 13:33:16 +10:00
parent 488e203c0b
commit 8a2373b354
2 changed files with 49 additions and 11 deletions

View file

@ -14,10 +14,10 @@ import (
var (
idAlphabet = []byte("abcdefghijklmnopqrstuvwxyz")
uppercaseWords = []string{"id", "uid", "uuid", "guid", "ssn", "tz"}
)
// Identifier creates an identifier useful for a query
// This is essentially a base conversion from Base 10 integers to Base 26
// Identifier is a base conversion from Base 10 integers to Base 26
// integers that are represented by an alphabet from a-z
// See tests for example outputs.
func Identifier(in int) string {
@ -64,8 +64,16 @@ func TitleCase(name string) string {
splits := strings.Split(name, "_")
for i, split := range splits {
if split == "id" {
splits[i] = "ID"
found := false
for _, uc := range uppercaseWords {
if split == uc {
splits[i] = strings.ToUpper(uc)
found = true
break
}
}
if found {
continue
}
@ -83,12 +91,22 @@ func CamelCase(name string) string {
splits := strings.Split(name, "_")
for i, split := range splits {
if split == "id" && i > 0 {
splits[i] = "ID"
if i == 0 {
continue
}
if i == 0 {
found := false
if i > 0 {
for _, uc := range uppercaseWords {
if split == uc {
splits[i] = strings.ToUpper(uc)
found = true
break
}
}
}
if found {
continue
}

View file

@ -96,6 +96,15 @@ func TestTitleCase(t *testing.T) {
{"hello_there", "HelloThere"},
{"", ""},
{"fun_id", "FunID"},
{"uid", "UID"},
{"guid", "GUID"},
{"uid", "UID"},
{"uuid", "UUID"},
{"ssn", "SSN"},
{"tz", "TZ"},
{"thing_guid", "ThingGUID"},
{"guid_thing", "GUIDThing"},
{"thing_guid_thing", "ThingGUIDThing"},
}
for i, test := range tests {
@ -115,6 +124,15 @@ func TestCamelCase(t *testing.T) {
{"hello_there_sunny", "helloThereSunny"},
{"", ""},
{"fun_id_times", "funIDTimes"},
{"uid", "uid"},
{"guid", "guid"},
{"uid", "uid"},
{"uuid", "uuid"},
{"ssn", "ssn"},
{"tz", "tz"},
{"thing_guid", "thingGUID"},
{"guid_thing", "guidThing"},
{"thing_guid_thing", "thingGUIDThing"},
}
for i, test := range tests {
@ -140,10 +158,12 @@ func TestMakeStringMap(t *testing.T) {
}
r = MakeStringMap(m)
e := `"TestOne": "interval", "TestTwo": "integer"`
if r != e {
t.Errorf("Expected %s, got %s", e, r)
e1 := `"TestOne": "interval", "TestTwo": "integer"`
e2 := `"TestTwo": "integer", "TestOne": "interval"`
if r != e1 && r != e2 {
t.Errorf("Got %s", r)
}
}