Skip to content

Commit b66ce30

Browse files
moving small word formatter to CaptializeResourceName() (#227)
1 parent ddef5ea commit b66ce30

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

flags/pokemonflagset.go

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -106,32 +106,8 @@ func AbilitiesFlag(w io.Writer, endpoint string, pokemonName string) error {
106106
return err
107107
}
108108

109-
// Anonymous function to format ability names
110-
formatAbilityName := func(name string) string {
111-
exceptions := map[string]bool{
112-
"of": true,
113-
"the": true,
114-
"to": true,
115-
"as": true,
116-
}
117-
118-
name = strings.ReplaceAll(name, "-", " ")
119-
words := strings.Split(name, " ")
120-
titleCaser := cases.Title(language.English)
121-
122-
// Process each word
123-
for i, word := range words {
124-
if _, found := exceptions[strings.ToLower(word)]; found && i != 0 {
125-
words[i] = strings.ToLower(word)
126-
} else {
127-
words[i] = titleCaser.String(word)
128-
}
129-
}
130-
return strings.Join(words, " ")
131-
}
132-
133109
for _, pokeAbility := range pokemonStruct.Abilities {
134-
formattedName := formatAbilityName(pokeAbility.Ability.Name)
110+
formattedName := styling.CapitalizeResourceName(pokeAbility.Ability.Name)
135111

136112
switch pokeAbility.Slot {
137113
case 1, 2:

styling/styling.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,29 @@ func StripANSI(input string) string {
8484
// titleCaser is a reusable title caser for English
8585
var titleCaser = cases.Title(language.English)
8686

87+
// smallWords are words that should remain lowercase in titles (unless first word)
88+
var smallWords = map[string]bool{
89+
"of": true,
90+
"the": true,
91+
"to": true,
92+
"as": true,
93+
}
94+
8795
// CapitalizeResourceName converts hyphenated resource names to title case
88-
// Example: "strong-jaw" -> "Strong Jaw"
96+
// Example: "strong-jaw" -> "Strong Jaw", "sword-of-ruin" -> "Sword of Ruin"
8997
func CapitalizeResourceName(name string) string {
90-
return titleCaser.String(strings.ReplaceAll(name, "-", " "))
98+
name = strings.ReplaceAll(name, "-", " ")
99+
words := strings.Split(name, " ")
100+
101+
for i, word := range words {
102+
if _, found := smallWords[strings.ToLower(word)]; found && i != 0 {
103+
words[i] = strings.ToLower(word)
104+
} else {
105+
words[i] = titleCaser.String(word)
106+
}
107+
}
108+
109+
return strings.Join(words, " ")
91110
}
92111

93112
// Color To avoid unnecessary dependencies, I adapted the MakeColor function from

0 commit comments

Comments
 (0)