-
Notifications
You must be signed in to change notification settings - Fork 1
/
template_funcs.go
143 lines (118 loc) · 3.04 KB
/
template_funcs.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"fmt"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
"text/template"
"time"
"github.com/oodzchen/dproject/utils"
)
var TmplFuncs = template.FuncMap{
// "timeAgo": formatTimeAgo,
"timeFormat": utils.FormatTime,
"intRange": intRange,
"placehold": placehold,
"joinStrArr": joinStrArr,
"upHead": upCaseHead,
"downHead": downCaseHead,
"pageStr": pageStr,
"calcDuration": calcDuration,
"replaceLink": utils.ReplaceLink,
"getDomain": getDomain,
"runeLen": runeLen,
}
func joinStrArr(arr []string, sep string) string {
return strings.Join(arr, sep)
}
func runeLen(str string) int {
return len([]rune(str))
}
// func formatTimeAgo(t time.Time) string {
// // return timeago.English.Format(t)
// return timeago.Chinese.Format(t)
// }
const (
pageMiddleNum = 5
pageEndNum = 5
)
func intRange(start, end, curr int) []int {
// fmt.Println("start: ", start, "end", end)
n := end - start + 1
all := make([]int, n)
for i := 0; i < n; i++ {
all[i] = start + i
}
halfMiddleNum := (pageMiddleNum / 2)
currIndex := curr - 1
var result []int
if n > pageMiddleNum+pageEndNum*2 {
middleLeft := currIndex - halfMiddleNum
middleRight := currIndex + halfMiddleNum + 1
leftOverley := pageEndNum + halfMiddleNum
rightOverley := end - 1 - pageEndNum - halfMiddleNum
if currIndex < leftOverley || currIndex > rightOverley {
result = append(result, all[:leftOverley]...)
result = append(result, 0)
result = append(result, all[rightOverley:]...)
} else {
result = append(result, all[:pageEndNum]...)
result = append(result, 0)
result = append(result, all[middleLeft:middleRight]...)
result = append(result, 0)
result = append(result, all[n-pageEndNum:]...)
}
} else {
result = all
}
return result
}
func placehold(data any, placeholcer string) string {
if data == nil || data == false {
return placeholcer
}
val := reflect.ValueOf(data)
switch val.Kind() {
case reflect.Array, reflect.Slice, reflect.String, reflect.Map, reflect.Chan:
if val.Len() == 0 {
return placeholcer
}
fallthrough
default:
return ""
}
}
func upCaseHead(runeNum int, str string) string {
runeStr := []rune(str)
return strings.ToUpper(string(runeStr[:runeNum])) + string(runeStr[runeNum:])
}
func downCaseHead(runeNum int, str string) string {
runeStr := []rune(str)
return strings.ToLower(string(runeStr[:runeNum])) + string(runeStr[runeNum:])
}
func pageStr(path string, page int, query url.Values) string {
if query == nil {
return fmt.Sprintf("%s?page=%d", path, page)
}
var pageStr string
if page < 1 {
pageStr = "1"
} else {
pageStr = strconv.Itoa(page)
}
if query.Has("page") {
query.Set("page", pageStr)
} else {
query.Add("page", pageStr)
}
return path + "?" + query.Encode()
}
func calcDuration(start time.Time) string {
return fmt.Sprintf("%dms", time.Since(start).Milliseconds())
}
var urlDomainRegex = regexp.MustCompile(`[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{2,4}`)
func getDomain(url string) string {
return urlDomainRegex.FindString(url)
}