1
0
mirror of https://github.com/beego/bee.git synced 2024-12-23 06:40:48 +00:00
bee/internal/app/module/beegopro/pongo2.go

60 lines
1.6 KiB
Go
Raw Normal View History

2020-07-04 14:58:03 +00:00
package beegopro
import (
"strings"
"unicode/utf8"
2020-10-09 15:33:58 +00:00
2020-12-16 05:20:41 +00:00
"github.com/beego/bee/v2/utils"
2020-10-09 15:33:58 +00:00
"github.com/flosch/pongo2"
2020-07-04 14:58:03 +00:00
)
func init() {
_ = pongo2.RegisterFilter("lowerFirst", pongo2LowerFirst)
_ = pongo2.RegisterFilter("upperFirst", pongo2UpperFirst)
_ = pongo2.RegisterFilter("snakeString", pongo2SnakeString)
_ = pongo2.RegisterFilter("camelString", pongo2CamelString)
}
func pongo2LowerFirst(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
if in.Len() <= 0 {
return pongo2.AsValue(""), nil
}
t := in.String()
r, size := utf8.DecodeRuneInString(t)
return pongo2.AsValue(strings.ToLower(string(r)) + t[size:]), nil
}
func pongo2UpperFirst(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
if in.Len() <= 0 {
return pongo2.AsValue(""), nil
}
t := in.String()
return pongo2.AsValue(strings.Replace(t, string(t[0]), strings.ToUpper(string(t[0])), 1)), nil
}
// snake string, XxYy to xx_yy
func pongo2SnakeString(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
if in.Len() <= 0 {
return pongo2.AsValue(""), nil
}
t := in.String()
return pongo2.AsValue(utils.SnakeString(t)), nil
}
// snake string, XxYy to xx_yy
func pongo2CamelString(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
if in.Len() <= 0 {
return pongo2.AsValue(""), nil
}
t := in.String()
return pongo2.AsValue(utils.CamelString(t)), nil
}
2020-07-21 14:24:42 +00:00
//func upperFirst(str string) string {
// return strings.Replace(str, string(str[0]), strings.ToUpper(string(str[0])), 1)
//}
2020-07-04 14:58:03 +00:00
func lowerFirst(str string) string {
return strings.Replace(str, string(str[0]), strings.ToLower(string(str[0])), 1)
}