Skip to content

Commit 3834da1

Browse files
committed
fix: capital all to first letter only
1 parent 73eb325 commit 3834da1

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

darkstat/front/types/types.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,50 @@ type SharedData struct {
8787
ShipNames ShipNames
8888
}
8989

90-
func ToTitle(s string) string { return strings.Map(unicode.ToTitle, s) }
90+
// COPY PASTE FROM https://cs.opensource.google/go/go/+/refs/tags/go1.21.10:src/strings/strings.go;l=752
91+
// isSeparator reports whether the rune could mark a word boundary.
92+
// TODO: update when package unicode captures more of the properties.
93+
func isSeparator(r rune) bool {
94+
// ASCII alphanumerics and underscore are not separators
95+
if r <= 0x7F {
96+
switch {
97+
case '0' <= r && r <= '9':
98+
return false
99+
case 'a' <= r && r <= 'z':
100+
return false
101+
case 'A' <= r && r <= 'Z':
102+
return false
103+
case r == '_':
104+
return false
105+
}
106+
return true
107+
}
108+
// Letters and digits are not separators
109+
if unicode.IsLetter(r) || unicode.IsDigit(r) {
110+
return false
111+
}
112+
// Otherwise, all we can do for now is treat spaces as separators.
113+
return unicode.IsSpace(r)
114+
}
115+
116+
// COPY PASTE FROM https://cs.opensource.google/go/go/+/refs/tags/go1.21.10:src/strings/strings.go;l=782
117+
func Title(s string) string {
118+
// Use a closure here to remember state.
119+
// Hackish but effective. Depends on Map scanning in order and calling
120+
// the closure once per rune.
121+
prev := ' '
122+
return strings.Map(
123+
func(r rune) rune {
124+
if isSeparator(prev) {
125+
prev = r
126+
return unicode.ToTitle(r)
127+
}
128+
prev = r
129+
return r
130+
},
131+
s)
132+
}
91133

92134
func ToCapital(value string) string {
93-
return ToTitle(value)
135+
return Title(value)
94136
}

0 commit comments

Comments
 (0)