GoBlog/utils.go

50 lines
914 B
Go
Raw Normal View History

2020-08-05 17:14:10 +00:00
package main
2020-09-01 16:53:21 +00:00
import (
2020-10-06 17:07:48 +00:00
"math/rand"
2020-11-01 17:37:21 +00:00
"net/http"
2020-09-19 12:56:31 +00:00
"sort"
2020-09-01 16:53:21 +00:00
"strings"
2020-10-06 17:07:48 +00:00
"time"
2020-09-01 16:53:21 +00:00
)
func urlize(str string) string {
newStr := ""
for _, c := range strings.Split(strings.ToLower(str), "") {
if c >= "a" && c <= "z" || c >= "A" && c <= "Z" || c >= "0" && c <= "9" {
newStr += c
} else if c == " " {
newStr += "-"
}
}
return newStr
}
2020-09-19 12:56:31 +00:00
func sortedStrings(s []string) []string {
sort.Slice(s, func(i, j int) bool {
return strings.ToLower(s[i]) < strings.ToLower(s[j])
})
return s
}
2020-10-06 17:07:48 +00:00
func generateRandomString(chars int) string {
rand.Seed(time.Now().UnixNano())
2020-10-06 17:07:48 +00:00
letters := []rune("abcdefghijklmnopqrstuvwxyz")
b := make([]rune, chars)
2020-10-06 17:07:48 +00:00
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
2020-11-01 17:37:21 +00:00
func slashTrimmedPath(r *http.Request) string {
return trimSlash(r.URL.Path)
}
func trimSlash(s string) string {
if len(s) > 1 {
s = strings.TrimSuffix(s, "/")
}
return s
}