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

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

View file

@ -96,6 +96,15 @@ func TestTitleCase(t *testing.T) {
{"hello_there", "HelloThere"}, {"hello_there", "HelloThere"},
{"", ""}, {"", ""},
{"fun_id", "FunID"}, {"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 { for i, test := range tests {
@ -115,6 +124,15 @@ func TestCamelCase(t *testing.T) {
{"hello_there_sunny", "helloThereSunny"}, {"hello_there_sunny", "helloThereSunny"},
{"", ""}, {"", ""},
{"fun_id_times", "funIDTimes"}, {"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 { for i, test := range tests {
@ -140,10 +158,12 @@ func TestMakeStringMap(t *testing.T) {
} }
r = MakeStringMap(m) r = MakeStringMap(m)
e := `"TestOne": "interval", "TestTwo": "integer"`
if r != e { e1 := `"TestOne": "interval", "TestTwo": "integer"`
t.Errorf("Expected %s, got %s", e, r) e2 := `"TestTwo": "integer", "TestOne": "interval"`
if r != e1 && r != e2 {
t.Errorf("Got %s", r)
} }
} }