/
name_util.go
121 lines (100 loc) · 2.08 KB
/
name_util.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package mcore
import (
"strings"
)
// ToTableName
// example: DemoTable to demo_table
func ToTableName(name string) string {
result := ""
lastIndexDot := strings.LastIndex(name, ".")
if lastIndexDot != -1 {
name = SubRight(name, lastIndexDot+1)
}
b := []byte(name)
for i := 0; i < len(b); i++ {
l := string(b[i])
//first letter
if i == 0 {
result = result + strings.ToLower(l)
continue
}
//Up letter
if IsUpLetter(l) {
result = result + "_" + strings.ToLower(l)
} else {
result = result + l
}
}
return result
}
// ToCamel
// demo_demo > DemoDemo
func ToCamel(name string) string {
result := ""
b := []byte(name)
size := len(b)
//upcase first letter
result = strings.ToUpper(string(b[0]))
for i := 1; i < size; i++ {
l := string(b[i])
bl := string(b[i-1])
if "_" == l {
continue
}
//front letter is _, then upcase add append
if bl == "_" {
result = result + strings.ToUpper(l)
continue
}
//front letter is not _ append it
result = result + l
}
return result
}
// dir such as abc/def or abc\\def for windows, return def
func GetPackageNameFromDir(dir string) string {
return String(dir).ReplaceAll("\\", "/").TrimSuffix("/").SepEnd("/").String()
}
// ToLabel returns space spilt string.
func ToLabel(name string) string {
name = strings.Replace(name, "-", " ", -1)
name = strings.Replace(name, "_", " ", -1)
name = strings.Replace(name, ".", " ", -1)
sb := NewStringBuffer()
for i := 0; i < len(name); i++ {
c := string(name[i])
cb := ""
// before letter
if i > 0 {
cb = string(name[i-1])
}
if IsUpLetter(c) {
if IsUpLetter(cb) {
// cb is upcase, and c is upcase
sb.Append(c)
} else {
// cb not upcase
sb.Append(" ", c)
}
} else {
// other as original
sb.Append(c)
}
}
as := strings.Split(sb.String(), " ")
sb2 := NewStringBuffer()
for i, a := range as {
// skip space.
if strings.TrimSpace(a) == "" {
continue
}
// upcase first letter
a = UpperCaseFirst(a)
if i == len(as) {
sb2.Append(a)
} else {
sb2.Append(a, " ")
}
}
return sb2.String()
}