-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
93 lines (81 loc) · 1.62 KB
/
util.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
package goweb
import (
"bytes"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
)
func ToString(str string, defaultvalue string) string {
if str == "" {
return defaultvalue
}
return str
}
func ToInt(str string, defaultvalue int) int {
val, err := strconv.Atoi(str)
if err != nil {
return defaultvalue
}
return val
}
func ToBool(str string, defaultvalue bool) bool {
val, err := strconv.ParseBool(str)
if err != nil {
return defaultvalue
}
return val
}
func webTime(t time.Time) string {
ftime := t.Format(time.RFC1123)
if strings.HasSuffix(ftime, "UTC") {
ftime = ftime[0:len(ftime)-3] + "GMT"
}
return ftime
}
func dirExists(dir string) bool {
d, e := os.Stat(dir)
switch {
case e != nil:
return false
case !d.IsDir():
return false
}
return true
}
func fileExists(dir string) bool {
info, err := os.Stat(dir)
if err != nil {
return false
}
return !info.IsDir()
}
func Urlencode(data map[string]string) string {
var buf bytes.Buffer
for k, v := range data {
buf.WriteString(url.QueryEscape(k))
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(v))
buf.WriteByte('&')
}
s := buf.String()
return s[0 : len(s)-1]
}
var slugRegex = regexp.MustCompile(`(?i:[^a-z0-9\-_])`)
func Slug(s string, sep string) string {
if s == "" {
return ""
}
slug := slugRegex.ReplaceAllString(s, sep)
if slug == "" {
return ""
}
quoted := regexp.QuoteMeta(sep)
sepRegex := regexp.MustCompile("(" + quoted + "){2,}")
slug = sepRegex.ReplaceAllString(slug, sep)
sepEnds := regexp.MustCompile("^" + quoted + "|" + quoted + "$")
slug = sepEnds.ReplaceAllString(slug, "")
return strings.ToLower(slug)
}