filebrowser/utils/utils.go

111 lines
2.8 KiB
Go
Raw Normal View History

2015-09-14 09:46:31 +00:00
package utils
import (
"errors"
"net/http"
2015-09-14 09:46:31 +00:00
"reflect"
"strings"
"unicode"
)
// Dict allows to send more than one variable into a template
func Dict(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
}
2015-09-18 08:47:02 +00:00
// IsMap checks if some variable is a map
2015-09-14 09:46:31 +00:00
func IsMap(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Map
}
2015-09-18 08:47:02 +00:00
// IsSlice checks if some variable is a slice
2015-09-15 10:59:48 +00:00
func IsSlice(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Slice
}
2015-09-18 08:47:02 +00:00
// IsArray checks if some variable is an array
2015-09-15 10:59:48 +00:00
func IsArray(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Array
}
2015-09-18 08:47:02 +00:00
// IsString checks if some variable is a string
2015-09-17 10:32:27 +00:00
func IsString(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.String
}
2015-09-18 08:47:02 +00:00
// IsInt checks if some variable is an integer
2015-09-17 10:32:27 +00:00
func IsInt(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Int
}
2015-09-18 08:47:02 +00:00
// IsBool checks if some variable is a boolean
2015-09-17 10:32:27 +00:00
func IsBool(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Bool
}
2015-09-18 08:47:02 +00:00
// IsInterface checks if some variable is an interface
2015-09-17 10:32:27 +00:00
func IsInterface(sth interface{}) bool {
return reflect.ValueOf(sth).Kind() == reflect.Interface
}
2015-09-18 08:47:02 +00:00
// IsMarkdownFile checks if a filename belongs to a markdown file
2015-09-17 20:26:06 +00:00
func IsMarkdownFile(filename string) bool {
return strings.HasSuffix(filename, ".markdown") || strings.HasSuffix(filename, ".md")
}
2015-09-18 08:47:02 +00:00
// SplitCapitalize splits a string by its uppercase letters and capitalize the
// first letter of the string
2015-09-14 09:46:31 +00:00
func SplitCapitalize(name string) string {
var words []string
l := 0
for s := name; s != ""; s = s[l:] {
l = strings.IndexFunc(s[1:], unicode.IsUpper) + 1
if l <= 0 {
l = len(s)
}
words = append(words, s[:l])
}
name = ""
for _, element := range words {
name += element + " "
}
name = strings.ToLower(name[:len(name)-1])
name = strings.ToUpper(string(name[0])) + name[1:len(name)]
return name
}
2015-09-18 08:47:02 +00:00
// ParseComponents parses the components of an URL creating an array
func ParseComponents(r *http.Request) []string {
//The URL that the user queried.
path := r.URL.Path
path = strings.TrimSpace(path)
//Cut off the leading and trailing forward slashes, if they exist.
//This cuts off the leading forward slash.
if strings.HasPrefix(path, "/") {
path = path[1:]
}
//This cuts off the trailing forward slash.
if strings.HasSuffix(path, "/") {
cutOffLastCharLen := len(path) - 1
path = path[:cutOffLastCharLen]
}
//We need to isolate the individual components of the path.
components := strings.Split(path, "/")
return components
}