-
Notifications
You must be signed in to change notification settings - Fork 0
/
caser.go
49 lines (41 loc) · 1.09 KB
/
caser.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
41
42
43
44
45
46
47
48
49
package neng
import (
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
// caser handles case transformations.
type caser struct {
lower cases.Caser
title cases.Caser
upper cases.Caser
}
// toLower transforms word to lower case.
func (c *caser) toLower(word string) string {
return c.lower.String(word)
}
// toSentence transforms the first word in a space-separated sequence
// to title case and everything that follows to lower case.
func (c *caser) toSentence(words string) string {
space := strings.Index(words, " ")
if space == -1 {
return c.toTitle(words)
}
return c.toTitle(words[:space]) + c.toLower(words[space:])
}
// toTitle transforms word to title case.
func (c *caser) toTitle(word string) string {
return c.title.String(word)
}
// toUpper transforms word to upper case.
func (c *caser) toUpper(word string) string {
return c.upper.String(word)
}
// newCaser returns a pointer to new caser struct.
func newCaser() *caser {
return &caser{
lower: cases.Lower(language.English),
title: cases.Title(language.English),
upper: cases.Upper(language.English),
}
}