-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.go
More file actions
64 lines (43 loc) · 1.57 KB
/
string.go
File metadata and controls
64 lines (43 loc) · 1.57 KB
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
package utils
import (
"bytes"
"regexp"
"strings"
"unicode"
"unicode/utf8"
)
type stringUtils struct{}
// single variable acting as the StringUtils "subpackage" inside the legit utils package
var String stringUtils
var camelCaseRegularExpression = regexp.MustCompile("[0-9A-Za-z]+")
// If the given string is empty, offer an alternative to return in its stead
func (dummyReceiver *stringUtils) EmptyAlternative(valueToReturnIfNotEmpty, valueToReturnIfOriginalEmpty string) string {
if valueToReturnIfNotEmpty == "" {
return valueToReturnIfOriginalEmpty
}
return valueToReturnIfNotEmpty
}
func (dummyReceiver *stringUtils) Contains(parentString string, stringContainedInParent string) bool {
return strings.Contains(parentString, stringContainedInParent)
}
func (dummyReceiver *stringUtils) ReplaceAll(parentString string, oldString string, newString string) string {
return strings.Replace(parentString, oldString, newString, -1)
}
func (dummyReceiver *stringUtils) CamelCase(original string) string {
if original == "" {
return ""
}
sections := camelCaseRegularExpression.FindAll([]byte(original), -1)
for i, v := range sections {
sections[i] = bytes.Title(v)
}
// while returning, make sure to lower the first character
return dummyReceiver.LowerFirstChar(string(bytes.Join(sections, nil)))
}
func (dummyReceiver *stringUtils) LowerFirstChar(original string) string {
if original == "" {
return ""
}
r, n := utf8.DecodeRuneInString(original)
return string(unicode.ToLower(r)) + original[n:]
}