Skip to content

Commit 6d3ee44

Browse files
committed
fix: issue with acronyms in name generator
1 parent 4b7c2db commit 6d3ee44

1 file changed

Lines changed: 32 additions & 18 deletions

File tree

pkg/util/naming.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ import (
1212
var replaceChars = []int32{'-', '_', ':', ' '}
1313

1414
var acronyms = []string{
15-
"id",
16-
"api",
17-
"vcs",
18-
"git",
15+
// General Tech & Web
16+
"ID", "API", "GIT", "VCS", "UUID", "GUID", "REST", "SOAP",
17+
18+
// Business
19+
"VAT", "SLA", "SLO", "IBAN", "BIC",
20+
21+
// TM Forum (TMF) & Telecom Specific
22+
"TMF", "SID", "TAM", "eTOM", "IP", "VPN", "SIM", "IMEI", "IMSI", "CPE",
23+
"FVO", "MVO", // first-value object and mutation-value object
1924
}
2025

2126
func init() {
@@ -75,23 +80,32 @@ func ToSlug(input string) string {
7580

7681
// UppercaseAcronyms replaces acronyms in the input string with their uppercase form
7782
func UppercaseAcronyms(input string) string {
78-
for i, _ := range input {
79-
// check if any acronym starts at this index
80-
for _, acronym := range acronyms {
81-
if strings.HasPrefix(strings.ToLower(input[i:]), strings.ToLower(acronym)) {
82-
acronymEnd := i + len(acronym)
83-
if acronymEnd < len(input) {
84-
// uppercase acronym
85-
input = input[:i] + strings.ToUpper(acronym) + input[acronymEnd:]
86-
87-
// uppercase following character, if lowercase
88-
if acronymEnd < len(input) && unicode.IsLower(rune(input[acronymEnd])) {
89-
input = input[:acronymEnd] + strings.ToUpper(string(input[acronymEnd])) + input[acronymEnd+1:]
90-
}
83+
for _, acronym := range acronyms {
84+
upperAcro := strings.ToUpper(acronym)
85+
86+
search := input
87+
var result strings.Builder
88+
i := 0
89+
for i < len(search) {
90+
if strings.HasPrefix(strings.ToLower(search[i:]), strings.ToLower(acronym)) {
91+
endIdx := i + len(acronym)
92+
93+
// Boundary Check:
94+
// Is it the start of the string OR was the previous char NOT a lowercase letter?
95+
// Is it the end of the string OR is the next char NOT a lowercase letter?
96+
isStart := i == 0 || !unicode.IsLower(rune(search[i-1]))
97+
isEnd := endIdx == len(search) || !unicode.IsLower(rune(search[endIdx]))
98+
99+
if isStart && isEnd {
100+
result.WriteString(upperAcro)
101+
i = endIdx
102+
continue
91103
}
92104
}
105+
result.WriteByte(search[i])
106+
i++
93107
}
108+
input = result.String()
94109
}
95-
96110
return input
97111
}

0 commit comments

Comments
 (0)