-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
74 lines (65 loc) · 1.36 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
package jin
import (
"math"
"os"
"path"
"reflect"
"runtime"
"strconv"
)
func resolveAddress(addr []string) string {
switch len(addr) {
case 0:
if port := os.Getenv("PORT"); port != "" {
debugPrint("Environment variable PORT=\"%s\"", port)
return ":" + port
}
debugPrint("Environment variable PORT is undefined. Using port :8080 by default")
return ":8080"
case 1:
return addr[0]
default:
panic("too many parameters")
}
}
func nameOfFunction(f any) string {
if f == nil {
return "nil"
}
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}
func lastChar(str string) uint8 {
if str == "" {
panic("The length of the string can't be 0")
}
return str[len(str)-1]
}
func joinPaths(absolutePath, relativePath string) string {
if relativePath == "" {
return absolutePath
}
finalPath := path.Join(absolutePath, relativePath)
if lastChar(relativePath) == '/' && lastChar(finalPath) != '/' {
return finalPath + "/"
}
return finalPath
}
// ordinalize ordinalizes the number by adding the ordinal to the number.
func ordinalize(number int) string {
abs := int(math.Abs(float64(number)))
nstr := strconv.Itoa(number)
i := abs % 100
if i == 11 || i == 12 || i == 13 {
return nstr + "th"
}
switch abs % 10 {
case 1:
return nstr + "st"
case 2:
return nstr + "nd"
case 3:
return nstr + "rd"
default:
return nstr + "th"
}
}