-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
260 lines (238 loc) · 5.24 KB
/
string.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package tox
import (
"fmt"
"github.com/goccy/go-json"
"math"
"reflect"
"strings"
"time"
"unicode"
"unicode/utf8"
)
// ToString converts any data type to a string, it uses fmt.Sprintf() to convert unknown types.
func ToString(v interface{}) string {
return ToStringOpts(v, nil)
}
func ToStringOpts(v interface{}, options *Options) string {
switch v := v.(type) {
case bool:
if v {
return "true"
}
return "false"
case nil:
return ""
case string:
return v
case float64:
if options != nil && options.FloatToInt {
if v == math.Floor(v) {
return fmt.Sprintf("%.0f", v)
}
}
if options != nil && options.FloatPrecision > 0 {
return fmt.Sprintf("%.*f", options.FloatPrecision, v)
} else {
return fmt.Sprintf("%v", v)
}
case float32:
return ToStringOpts(float64(v), options)
case int, int64, uint, uint64, int8, int16, uint8, uint16:
return fmt.Sprintf("%v", v)
case time.Duration:
return ToPrettyDuration(v, FormatShort)
case time.Time:
return v.Format(time.RFC3339Nano)
case []byte:
if utf8.Valid(v) {
return string(v)
} else {
return fmt.Sprintf("%v", v)
}
case map[string]interface{}:
b, err := json.Marshal(v)
if err != nil {
return fmt.Sprintf("%v", v)
} else {
return string(b)
}
default:
switch reflect.TypeOf(v).Name() {
case "DateTime":
if reflect.TypeOf(v).Kind() == reflect.Int64 {
return time.Unix(reflect.ValueOf(v).Int()/1000, 0).Format(time.RFC3339Nano)
}
}
b, err := json.Marshal(v)
if err != nil {
return fmt.Sprintf("%v", v)
} else {
return string(b)
}
}
}
func ToJson(v interface{}) string {
b, err := json.Marshal(v)
if err == nil {
return string(b)
} else {
return fmt.Sprintf("%v", v)
}
}
func ToPrettyJson(v interface{}) string {
b, err := json.MarshalIndent(v, "", " ")
if err == nil {
return string(b)
} else {
return fmt.Sprintf("%v", v)
}
}
// ToStringArray can convert a single string to an array, useful if interface could be a string or array of strings.
func ToStringArray(v interface{}) []string {
switch v := v.(type) {
case nil:
return nil
case string:
return []string{v}
case []string:
return v
case []any:
var ret = make([]string, len(v))
for ii, vv := range v {
ret[ii] = ToString(vv)
}
return ret
default:
aVal := reflect.ValueOf(v)
if aVal.Kind() == reflect.Array || aVal.Kind() == reflect.Slice {
var ret = make([]string, aVal.Len())
for i := 0; i < aVal.Len(); i++ {
ret[i] = ToString(aVal.Index(i).Interface())
}
return ret
}
return nil
}
}
type StringFormat int
const (
FormatShort StringFormat = 1
FormatMedium StringFormat = 2
FormatLong StringFormat = 3
)
func ToPrettyDuration(v time.Duration, format StringFormat) string {
var r []string
isNegative := false
if v < 0 {
v = -v
isNegative = true
}
days := v / (24 * time.Hour)
if days > 0 {
switch format {
case FormatMedium:
r = append(r, fmt.Sprintf("%dd", days))
case FormatLong:
r = append(r, fmt.Sprintf("%d days", days))
}
}
v -= days * 24 * time.Hour
hours := v / time.Hour
if hours > 0 {
switch format {
case FormatMedium:
r = append(r, fmt.Sprintf("%dh", hours))
case FormatLong:
r = append(r, fmt.Sprintf("%d hours", hours))
}
}
v -= hours * time.Hour
minutes := v / time.Minute
if minutes > 0 {
switch format {
case FormatMedium:
r = append(r, fmt.Sprintf("%dm", minutes))
case FormatLong:
r = append(r, fmt.Sprintf("%d minutes", minutes))
}
}
v -= minutes * time.Minute
seconds := v / time.Second
if seconds > 0 {
switch format {
case FormatMedium:
r = append(r, fmt.Sprintf("%ds", seconds))
case FormatLong:
r = append(r, fmt.Sprintf("%d seconds", seconds))
}
}
var ret string
if format == FormatShort {
ret = fmt.Sprintf("%d.%d:%d:%d", days, hours, minutes, seconds)
} else {
ret = strings.Join(r, ", ")
}
if isNegative {
ret = "-" + ret
}
return ret
}
func ToStringPtr(v interface{}) *string {
if v == nil {
return nil
}
ret := ToString(v)
return &ret
}
func ToStringPtrOpts(v interface{}, options *Options) *string {
if v == nil {
return nil
}
ret := ToString(v)
if options != nil && options.EmptyStringAsNull && ret == "" {
return nil
}
return &ret
}
func TruncateString(s string, length int) string {
if len(s) > length {
return s[:length]
}
return s
}
func CapitalizeString(s string) string {
// Split the input string into words
words := strings.Fields(s)
// Initialize an empty slice to store the capitalized words
capitalizedWords := make([]string, len(words))
// Capitalize the first letter of each word and make the rest lowercase
for i, word := range words {
// Check if the word is empty
if len(word) == 0 {
continue
}
// Convert the first letter to uppercase and the rest to lowercase
capitalizedWord := string(unicode.ToUpper(rune(word[0]))) + strings.ToLower(word[1:])
capitalizedWords[i] = capitalizedWord
}
// Join the capitalized words back into a single string
result := strings.Join(capitalizedWords, " ")
return result
}
func StringInArray(s string, arr []string, ignoreCase bool) bool {
if arr == nil {
return false
}
for _, v := range arr {
if ignoreCase {
if strings.EqualFold(v, s) {
return true
}
} else {
if v == s {
return true
}
}
}
return false
}