mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-06-22 13:18:29 -04:00
Improve template helper functions: string/slice (#24266)
Follow #23328 The improvements: 1. The `contains` functions are covered by tests 2. The inconsistent behavior of `containGeneric` is replaced by `StringUtils.Contains` and `SliceUtils.Contains` 3. In the future we can move more help functions into XxxUtils to simplify the `helper.go` and reduce unnecessary global functions. FAQ: 1. Why it's called `StringUtils.Contains` but not `strings.Contains` like Golang? Because our `StringUtils` is not Golang's `strings` package. There will be our own string functions. --------- Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
parent
c0d105609f
commit
8820191476
11 changed files with 105 additions and 40 deletions
|
@ -15,7 +15,6 @@ import (
|
|||
"mime"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -68,11 +67,15 @@ func NewFuncMap() []template.FuncMap {
|
|||
"PathEscape": url.PathEscape,
|
||||
"PathEscapeSegments": util.PathEscapeSegments,
|
||||
|
||||
// utils
|
||||
"StringUtils": NewStringUtils,
|
||||
"SliceUtils": NewSliceUtils,
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// string / json
|
||||
// TODO: move string helper functions to StringUtils
|
||||
"Join": strings.Join,
|
||||
"DotEscape": DotEscape,
|
||||
"HasPrefix": strings.HasPrefix,
|
||||
"EllipsisString": base.EllipsisString,
|
||||
"DumpVar": dumpVar,
|
||||
|
||||
|
@ -144,35 +147,6 @@ func NewFuncMap() []template.FuncMap {
|
|||
return fmt.Sprint(time.Since(startTime).Nanoseconds()/1e6) + "ms"
|
||||
},
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// slice
|
||||
"containGeneric": func(arr, v interface{}) bool {
|
||||
arrV := reflect.ValueOf(arr)
|
||||
if arrV.Kind() == reflect.String && reflect.ValueOf(v).Kind() == reflect.String {
|
||||
return strings.Contains(arr.(string), v.(string))
|
||||
}
|
||||
if arrV.Kind() == reflect.Slice {
|
||||
for i := 0; i < arrV.Len(); i++ {
|
||||
iV := arrV.Index(i)
|
||||
if !iV.CanInterface() {
|
||||
continue
|
||||
}
|
||||
if iV.Interface() == v {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
},
|
||||
"contain": func(s []int64, id int64) bool {
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] == id {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
},
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
// setting
|
||||
"AppName": func() string {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue