-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring_utils.go
More file actions
178 lines (143 loc) · 4.63 KB
/
string_utils.go
File metadata and controls
178 lines (143 loc) · 4.63 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package utils
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"unicode/utf8"
"github.com/chzyer/readline/runes"
"github.com/iancoleman/strcase"
)
func IntToStr(val int) string {
return strconv.Itoa(val)
}
func Reverse(targetArray []string) []string {
newArray := make([]string, 0, len(targetArray))
for i := len(targetArray) - 1; i >= 0; i-- {
newArray = append(newArray, targetArray[i])
}
return newArray
}
func UnicodeUnquote(bs []byte) []byte {
unicodeEscapeRx := regexp.MustCompile(`\\u[0-9a-fA-F]{4}`)
return unicodeEscapeRx.ReplaceAllFunc(bs, func(code []byte) []byte {
rune, _, _, _ := strconv.UnquoteChar(string(code), 0)
width := runes.Width(rune)
runeBytes := make([]byte, width)
utf8.EncodeRune(runeBytes, rune)
return runeBytes
})
}
func ReplaceAllSubstrings(input string, target string, replacement string) string {
if target == "" {
fmt.Println("Target substring cannot be empty.")
return input
}
// Replace all occurrences using strings.ReplaceAll
result := strings.ReplaceAll(input, target, replacement)
return result
}
type TruncateStringByRegexOptions struct {
InputString string
RegexPattern string
Predicate func(int) bool
}
func TruncateStringByRegex(options TruncateStringByRegexOptions) string {
regex := regexp.MustCompile(options.RegexPattern)
matches := regex.FindAllStringIndex(options.InputString, -1)
currentIndex := 0
var modifiedString string
for i := 0; i < len(matches); i++ {
matchStart, matchEnd := matches[i][0], matches[i][1]
shouldRemove := true
if options.Predicate != nil {
shouldRemove = options.Predicate(i)
}
if shouldRemove {
modifiedString += options.InputString[currentIndex:matchStart]
currentIndex = matchEnd
}
}
modifiedString += options.InputString[currentIndex:]
return modifiedString
}
func CreateStringObject(myStr string, entitySuffix string) (CreateStringObjectType, error) {
if myStr == "" {
return CreateStringObjectType{}, errors.New("class name is missing or misspelled or the script is having issues finding the class name")
}
result := CreateStringObjectType{
Orig: myStr,
Prefix: func() string {
return strings.Split(myStr, entitySuffix)[0]
},
}
result.CamelCase = func(stripSuffix bool, suffix string) string {
return strcase.ToLowerCamel(grabString(stripSuffix, result))+suffix
}
result.Classify = func(stripSuffix bool, suffix string) string {
return strcase.ToCamel(grabString(stripSuffix, result))+suffix
}
result.Capitalize = func(stripSuffix bool, suffix string) string {
str := grabString(stripSuffix, result)
if len(str) == 0 {
return suffix // Return only the suffix if the string is empty
}
return strings.ToUpper(str[:1]) + str[1:] + suffix
}
result.Dasherize = func(stripSuffix bool, suffix string) string {
return strcase.ToKebab(grabString(stripSuffix, result))+suffix
}
result.Lowercase = func(stripSuffix bool, suffix string) string {
return strings.ToLower(grabString(stripSuffix, result))+suffix
}
result.Uppercase = func(stripSuffix bool, suffix string) string {
return strings.ToUpper(grabString(stripSuffix, result))+suffix
}
result.Snakecase = func(stripSuffix bool, suffix string) string {
return strcase.ToSnake(grabString(stripSuffix, result))+suffix
}
result.KebabCase = func(stripSuffix bool, suffix string) string {
return strcase.ToKebab(grabString(stripSuffix, result)) + suffix
}
result.PascalCase = func(stripSuffix bool, suffix string) string {
return strcase.ToCamel(grabString(stripSuffix, result)) + suffix
}
return result, nil
}
func grabString(stripSuffix bool, result CreateStringObjectType) (string) {
if stripSuffix {
return result.Prefix()
} else {
return result.Orig
}
}
// CreateStringObjectType represents the structure of the string object.
type CreateStringObjectType struct {
Orig string
Prefix func() string
CamelCase func(stripSuffix bool, suffix string) string
Classify func(stripSuffix bool, suffix string) string
Capitalize func(stripSuffix bool, suffix string) string
Dasherize func(stripSuffix bool, suffix string) string
Lowercase func(stripSuffix bool, suffix string) string
Uppercase func(stripSuffix bool, suffix string) string
Snakecase func(stripSuffix bool, suffix string) string
KebabCase func(stripSuffix bool, suffix string) string
PascalCase func(stripSuffix bool, suffix string) string
}
func ContainsAny(s string, substrs []string) bool {
for _, substr := range substrs {
if strings.Contains(s, substr) {
return true
}
}
return false
}
func JoinArgs(args []string) string {
var result string
for _, arg := range args {
result += fmt.Sprintf(`"%s" `, arg)
}
return result
}