mirror of
https://github.com/gohugoio/hugo.git
synced 2025-03-19 15:22:47 +00:00
parent
661d64c46a
commit
29caaddce6
2 changed files with 35 additions and 1 deletions
|
@ -1457,6 +1457,17 @@ func lower(s interface{}) (string, error) {
|
|||
return strings.ToLower(ss), nil
|
||||
}
|
||||
|
||||
// title returns a copy of the input s with all Unicode letters that begin words
|
||||
// mapped to their title case.
|
||||
func title(s interface{}) (string, error) {
|
||||
ss, err := cast.ToStringE(s)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return strings.Title(ss), nil
|
||||
}
|
||||
|
||||
// upper returns a copy of the input s with all Unicode letters mapped to their
|
||||
// upper case.
|
||||
func upper(s interface{}) (string, error) {
|
||||
|
@ -2158,7 +2169,7 @@ func initFuncMap() {
|
|||
"string": func(v interface{}) (string, error) { return cast.ToStringE(v) },
|
||||
"sub": func(a, b interface{}) (interface{}, error) { return helpers.DoArithmetic(a, b, '-') },
|
||||
"substr": substr,
|
||||
"title": func(a string) string { return strings.Title(a) },
|
||||
"title": title,
|
||||
"time": asTime,
|
||||
"trim": trim,
|
||||
"upper": upper,
|
||||
|
|
|
@ -2022,6 +2022,29 @@ func TestLower(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestTitle(t *testing.T) {
|
||||
cases := []struct {
|
||||
s interface{}
|
||||
want string
|
||||
isErr bool
|
||||
}{
|
||||
{"test", "Test", false},
|
||||
{template.HTML("hypertext"), "Hypertext", false},
|
||||
{[]byte("bytes"), "Bytes", false},
|
||||
}
|
||||
|
||||
for i, c := range cases {
|
||||
res, err := title(c.s)
|
||||
if (err != nil) != c.isErr {
|
||||
t.Fatalf("[%d] unexpected isErr state: want %v, got %v, err = %v", i, c.want, (err != nil), err)
|
||||
}
|
||||
|
||||
if res != c.want {
|
||||
t.Errorf("[%d] title failed: want %v, got %v", i, c.want, res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpper(t *testing.T) {
|
||||
cases := []struct {
|
||||
s interface{}
|
||||
|
|
Loading…
Reference in a new issue