-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.go
62 lines (56 loc) · 1.31 KB
/
strings.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
package friendly
import (
"strconv"
"strings"
"unicode"
)
// Capitalizes a first (makes first letter capital).
func CapitalizeWord(word string) string {
return string(append([]rune{unicode.ToUpper([]rune(word)[0])}, []rune(word)[1:]...))
}
// Checks if a string contains only provided chars.
func SafeCharsOnly(str string, safe []byte) bool {
for i := range str {
if !ArrayContains(safe, str[i]) {
return false
}
}
return true
}
// Capitalizes every part of between periods of the email address before the @-sign.
// Joins them with " ".
// Example: "[email protected]" -> "John Doe".
func GetFullNameFromMailAddress(address string) string {
parts := strings.Split(strings.Split(address, "@")[0], ".")
res := []string{}
for _, p := range parts {
if len(p) < 2 {
continue
}
res = append(res, CapitalizeWord(p))
}
return strings.Join(res, " ")
}
// Checks if first semantic version is greater than another.
func SemVersionGreater(v1 string, v2 string) bool {
v1a := strings.Split(v1, ".")
v2a := strings.Split(v2, ".")
for i := 0; i < len(v1a); i++ {
num1, err := strconv.Atoi(v1a[i])
if err != nil {
num1 = 0
}
num2, err := strconv.Atoi(v2a[i])
if err != nil {
num2 = 0
}
if num1 == num2 {
continue
}
if num1 > num2 {
return true
}
return false
}
return false
}