This repository has been archived by the owner on May 14, 2023. It is now read-only.
/
transform.go
86 lines (76 loc) · 1.95 KB
/
transform.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Package transform implements functions to manipulate UTF-8 encoded strings.
*/
package transform
import (
"regexp"
"strings"
"unicode"
)
var spaces = regexp.MustCompile(" +")
func removeCase(s string, sep string, t func(rune) rune) string {
out := ""
old := ' '
for i, c := range s {
alpha := unicode.IsLetter(c) || unicode.IsNumber(c)
mat := (i > 1 && unicode.IsLower(old) && unicode.IsUpper(c))
if mat || !alpha || (unicode.IsSpace(c) && c != ' ') {
out += " "
}
if alpha || c == ' ' {
out += string(t(c))
}
old = c
}
return spaces.ReplaceAllString(strings.TrimSpace(out), sep)
}
// Simple returns a space-separated, lower-cased copy of the string s.
func Simple(s string) string {
return removeCase(s, " ", unicode.ToLower)
}
// Dash returns a dash-separated, lower-cased copy of the string s.
func Dash(s string) string {
return removeCase(s, "-", unicode.ToLower)
}
// Snake returns a underscore-separated, lower-cased copy of the string s.
func Snake(s string) string {
return removeCase(s, "_", unicode.ToLower)
}
// Dot returns a period-separated, lower-cased copy of the string s.
func Dot(s string) string {
return removeCase(s, ".", unicode.ToLower)
}
// Constant returns a underscore-separated, upper-cased copy of the string s.
func Constant(s string) string {
return removeCase(s, "_", unicode.ToUpper)
}
// Pascal returns a Pascal-cased copy of the string s.
func Pascal(s string) string {
out := ""
wasSpace := false
for i, c := range removeCase(s, " ", unicode.ToLower) {
if i == 0 || wasSpace {
c = unicode.ToUpper(c)
}
wasSpace = c == ' '
if !wasSpace {
out += string(c)
}
}
return out
}
// Camel returns a Camel-cased copy of the string s.
func Camel(s string) string {
first := ' '
for _, c := range s {
if unicode.IsLetter(c) || unicode.IsNumber(c) {
first = c
break
}
}
body := Pascal(s)
if len(body) > 1 {
return strings.TrimSpace(string(unicode.ToLower(first)) + body[1:])
}
return s
}