-
Notifications
You must be signed in to change notification settings - Fork 0
/
strcase.go
109 lines (91 loc) · 2.69 KB
/
strcase.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
package strcase
import (
"bytes"
"strings"
)
var (
// ToCapitalCamel is an alias for ToCapital
ToCapitalCamel = ToCapital
// ToCaterpillar is an alias for ToLowerKebab
ToCaterpillar = ToLowerKebab
// ToCobol is an alias for ToUpperKebab
ToCobol = ToUpperKebab
// ToCSS is an alias for ToLowerKebab
ToCSS = ToLowerKebab
// ToDash is an alias for ToLowerKebab
ToDash = ToLowerKebab
// ToHyphen is an alias for ToLowerKebab
ToHyphen = ToLowerKebab
// ToLazy is an alias for ToFlat
ToLazy = ToFlat
// ToLisp is an alias for ToLowerKebab
ToLisp = ToLowerKebab
// ToMacro is an alias for ToUpperSnake
ToMacro = ToUpperSnake
// ToPascal is an alias for ToCapital
ToPascal = ToCapital
)
// ToCamel converts the input string `input` into `camelCase`..
//
// Useful for: variable names in JavaScript/TypeScriptS/Go/C/C++/Java,
// internal method names in Go
func ToCamel(input string) string {
tokens := parse(input)
for i := 1; i < len(tokens); i++ {
tokens[i] = ToCapital(tokens[i])
}
return strings.Join(tokens, "")
}
// ToCapital converts the input string `input` into `CapitalCase`.
//
// Useful for: external method names in Go, classes and modules in Ruby,
// classes in JavaScript/TypeScript
func ToCapital(input string) string {
tokens := parse(input)
for i := 0; i < len(tokens); i++ {
upperFirstChar := bytes.ToUpper([]byte{tokens[i][0]})[0]
tokens[i] = string(append([]byte{upperFirstChar}, tokens[i][1:]...))
}
return strings.Join(tokens, "")
}
// ToFlat converts the input string `input` into `aflatcasestring`.
//
// Useful for: Java package names, Go package names
func ToFlat(input string) string {
tokens := parse(input)
return strings.Join(tokens, "")
}
// ToLowerKebab converts the input string `input` into `lower-kebab-case`.
//
// Useful for: Lisp names
func ToLowerKebab(input string) string {
tokens := parse(input)
return strings.Join(tokens, "-")
}
// ToLowerSnake converts the input string `input` into `lower_snake_case`.
//
// Useful for: variable names in Python
func ToLowerSnake(input string) string {
tokens := parse(input)
return strings.Join(tokens, "_")
}
// ToUpperKebab converts the input string `input` into `UPPER-KEBAB-CASE`.
//
// Useful for: COBOL
func ToUpperKebab(input string) string {
tokens := parse(input)
for i := 0; i < len(tokens); i++ {
tokens[i] = strings.ToUpper(tokens[i])
}
return strings.Join(tokens, "-")
}
// ToUpperSnake converts the input string `input` into `UPPER_SNAKE_CASE`.
//
// Useful for: constants in JavaScript/TypeScript/Python, Shell variables
func ToUpperSnake(input string) string {
tokens := parse(input)
for i := 0; i < len(tokens); i++ {
tokens[i] = strings.ToUpper(tokens[i])
}
return strings.Join(tokens, "_")
}