forked from orijtech/otils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
44 lines (40 loc) · 925 Bytes
/
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
package otils
import "strings"
// UniqStrings returns a slice contains unique element
// from given input strings.
func UniqStrings(strs ...string) []string {
uniqs := make([]string, 0, len(strs))
seen := make(map[string]struct{})
for _, str := range strs {
if _, ok := seen[str]; !ok {
seen[str] = struct{}{}
uniqs = append(uniqs, str)
}
}
return uniqs
}
// FirstNonEmptyString iterates through its
// arguments trying to find the first string
// that is not blank or consists entirely of spaces.
func FirstNonEmptyString(args ...string) string {
for _, arg := range args {
if arg == "" {
continue
}
if strings.TrimSpace(arg) != "" {
return arg
}
}
return ""
}
func NonEmptyStrings(args ...string) (nonEmpties []string) {
for _, arg := range args {
if arg == "" {
continue
}
if strings.TrimSpace(arg) != "" {
nonEmpties = append(nonEmpties, arg)
}
}
return nonEmpties
}