You've already forked filebrowser
mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-11-26 14:25:26 +08:00
structure changes
This commit is contained in:
42
tools/variables/transform.go
Normal file
42
tools/variables/transform.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package variables
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
var splitCapitalizeExceptions = map[string]string{
|
||||
"youtube": "YouTube",
|
||||
"github": "GitHub",
|
||||
"googleplus": "Google Plus",
|
||||
"linkedin": "LinkedIn",
|
||||
}
|
||||
|
||||
// SplitCapitalize splits a string by its uppercase letters and capitalize the
|
||||
// first letter of the string
|
||||
func SplitCapitalize(name string) string {
|
||||
if val, ok := splitCapitalizeExceptions[strings.ToLower(name)]; ok {
|
||||
return val
|
||||
}
|
||||
|
||||
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:]
|
||||
|
||||
return name
|
||||
}
|
||||
31
tools/variables/transform_test.go
Normal file
31
tools/variables/transform_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package variables
|
||||
|
||||
import "testing"
|
||||
|
||||
type testSplitCapitalize struct {
|
||||
name string
|
||||
result string
|
||||
}
|
||||
|
||||
var testSplitCapitalizeCases = []testSplitCapitalize{
|
||||
{"loremIpsum", "Lorem ipsum"},
|
||||
{"LoremIpsum", "Lorem ipsum"},
|
||||
{"loremipsum", "Loremipsum"},
|
||||
{"YouTube", "YouTube"},
|
||||
{"GitHub", "GitHub"},
|
||||
{"GooglePlus", "Google Plus"},
|
||||
{"Facebook", "Facebook"},
|
||||
}
|
||||
|
||||
func TestSplitCapitalize(t *testing.T) {
|
||||
for _, pair := range testSplitCapitalizeCases {
|
||||
v := SplitCapitalize(pair.name)
|
||||
if v != pair.result {
|
||||
t.Error(
|
||||
"For", pair.name,
|
||||
"expected", pair.result,
|
||||
"got", v,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
13
tools/variables/types.go
Normal file
13
tools/variables/types.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package variables
|
||||
|
||||
import "reflect"
|
||||
|
||||
// IsMap checks if some variable is a map
|
||||
func IsMap(sth interface{}) bool {
|
||||
return reflect.ValueOf(sth).Kind() == reflect.Map
|
||||
}
|
||||
|
||||
// IsSlice checks if some variable is a slice
|
||||
func IsSlice(sth interface{}) bool {
|
||||
return reflect.ValueOf(sth).Kind() == reflect.Slice
|
||||
}
|
||||
37
tools/variables/variables.go
Normal file
37
tools/variables/variables.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package variables
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Defined checks if variable is defined in a struct
|
||||
func Defined(data interface{}, field string) bool {
|
||||
t := reflect.Indirect(reflect.ValueOf(data)).Type()
|
||||
|
||||
if t.Kind() != reflect.Struct {
|
||||
log.Print("Non-struct type not allowed.")
|
||||
return false
|
||||
}
|
||||
|
||||
_, b := t.FieldByName(field)
|
||||
return b
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
Reference in New Issue
Block a user