-
Notifications
You must be signed in to change notification settings - Fork 90
/
utils.go
100 lines (85 loc) · 2.06 KB
/
utils.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
package readability
import (
nurl "net/url"
"strings"
"unicode/utf8"
)
// indexOf returns the position of the first occurrence of a
// specified value in a string array. Returns -1 if the
// value to search for never occurs.
func indexOf(array []string, key string) int {
for i := 0; i < len(array); i++ {
if array[i] == key {
return i
}
}
return -1
}
// wordCount returns number of word in str.
func wordCount(str string) int {
return len(strings.Fields(str))
}
// charCount returns number of char in str.
func charCount(str string) int {
return utf8.RuneCountInString(str)
}
// isValidURL checks if URL is valid.
func isValidURL(s string) bool {
_, err := nurl.ParseRequestURI(s)
return err == nil
}
// toAbsoluteURI convert uri to absolute path based on base.
// However, if uri is prefixed with hash (#), the uri won't be changed.
func toAbsoluteURI(uri string, base *nurl.URL) string {
if uri == "" || base == nil {
return uri
}
// If it is hash tag, return as it is
if strings.HasPrefix(uri, "#") {
return uri
}
// If it is data URI, return as it is
if strings.HasPrefix(uri, "data:") {
return uri
}
// If it is already an absolute URL, return as it is
tmp, err := nurl.ParseRequestURI(uri)
if err == nil && tmp.Scheme != "" && tmp.Hostname() != "" {
return uri
}
// Otherwise, resolve against base URI.
tmp, err = nurl.Parse(uri)
if err != nil {
return uri
}
return base.ResolveReference(tmp).String()
}
// strOr returns the first not empty string in args.
func strOr(args ...string) string {
for i := 0; i < len(args); i++ {
if args[i] != "" {
return args[i]
}
}
return ""
}
func sliceToMap(strings ...string) map[string]struct{} {
result := make(map[string]struct{})
for _, s := range strings {
result[s] = struct{}{}
}
return result
}
func strFilter(strs []string, filter func(string) bool) []string {
var result []string
for _, s := range strs {
if filter(s) {
result = append(result, s)
}
}
return result
}
func trim(s string) string {
s = strings.Join(strings.Fields(s), " ")
return strings.TrimSpace(s)
}