-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathhelpers.go
96 lines (78 loc) · 2.01 KB
/
helpers.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
package main
import (
"sort"
"strings"
)
func HasPrefix[s ~string](str s, prefix string) bool {
return strings.HasPrefix(string(str), prefix)
}
func TrimPrefix[s ~string](str s, prefix string) s {
return s(strings.TrimPrefix(string(str), prefix))
}
func HasSuffix[s ~string](str s, suffix string) bool {
return strings.HasSuffix(string(str), suffix)
}
func TrimSuffix[s ~string](str s, suffix string) s {
return s(strings.TrimSuffix(string(str), suffix))
}
func ReplaceAll[s, t, u ~string](str s, old t, new u) s {
return s(strings.ReplaceAll(string(str), string(old), string(new)))
}
func Contains[s ~string](str s, substr string) bool {
return strings.Contains(string(str), substr)
}
func Split[s ~string](str s, sep string) []s {
var ret []s
for _, x := range strings.Split(string(str), sep) {
ret = append(ret, s(x))
}
return ret
}
func Replace[s, ot, nt ~string](str s, old ot, new nt, n int) s {
return s(strings.Replace(string(str), string(old), string(new), n))
}
func Join[s ~string](strs []s, sep string) s {
return s(strings.Join(func() (ret []string) {
for _, x := range strs {
ret = append(ret, string(x))
}
return
}(), sep))
}
func ContainsAny[s ~string](str s, chars string) bool {
return strings.ContainsAny(string(str), chars)
}
func Index[s ~string](str s, substr string) int {
return strings.Index(string(str), substr)
}
func Capitalize[s ~string](str s) s {
return s(strings.ToUpper(string(str[0]))) + str[1:]
}
func SortStrings[s ~string](str []s) {
sort.Slice(str, func(i, j int) bool {
return str[i] < str[j]
})
}
func SliceToMap[T comparable](s []T) map[T]bool {
m := make(map[T]bool, len(s))
for _, v := range s {
m[v] = true
}
return m
}
func MergeMaps[T comparable](maps ...map[T]bool) map[T]bool {
result := make(map[T]bool)
for _, m := range maps {
for k, v := range m {
result[k] = v
}
}
return result
}
func RemoveMapValues[T comparable, S any](m map[T]S) map[T]bool {
result := make(map[T]bool)
for k := range m {
result[k] = true
}
return result
}