-
Notifications
You must be signed in to change notification settings - Fork 153
/
title.go
40 lines (34 loc) · 1.33 KB
/
title.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package strings
import (
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// Title returns a copy of the string s with all Unicode letters that begin
// words mapped to their title case. It uses language.Und for word boundaries.
// For a more general solution, see TitleWithLanguage.
func Title(s string) string {
return TitleWithLanguage(s, language.Und)
}
// TitleWithLanguage returns a copy of the string s with all Unicode letters
// that begin words mapped to their title case.
func TitleWithLanguage(s string, lang language.Tag) string {
return cases.Title(lang, cases.NoLower).String(s)
}
// Normalize returns a copy of the string s with the first word mapped to its
// title case. It uses language.Und for word boundaries.
// For a more general solution, see NormalizeWithLanguage.
func Normalize(s string) string {
return NormalizeWithLanguage(s, language.Und)
}
// NormalizeWithLanguage returns a copy of the string s with the first word
// mapped to its title case. If lang is not nil, it is used to determine the
// language for which the case transformation should be performed. If lang is
// nil, language.Und is used.
func NormalizeWithLanguage(s string, lang language.Tag) string {
words := strings.Fields(s)
if len(words) > 0 {
words[0] = TitleWithLanguage(words[0], lang)
}
return strings.Join(words, " ")
}