mirror of
https://github.com/gohugoio/hugo.git
synced 2025-03-20 05:24:17 +00:00
parent
f25f837339
commit
6b8422bb35
2 changed files with 43 additions and 1 deletions
|
@ -239,6 +239,21 @@ func slicestr(a interface{}, startEnd ...interface{}) (string, error) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// hasPrefix tests whether the input s begins with prefix.
|
||||||
|
func hasPrefix(s, prefix interface{}) (bool, error) {
|
||||||
|
ss, err := cast.ToStringE(s)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sp, err := cast.ToStringE(prefix)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.HasPrefix(ss, sp), nil
|
||||||
|
}
|
||||||
|
|
||||||
// substr extracts parts of a string, beginning at the character at the specified
|
// substr extracts parts of a string, beginning at the character at the specified
|
||||||
// position, and returns the specified number of characters.
|
// position, and returns the specified number of characters.
|
||||||
//
|
//
|
||||||
|
@ -2060,7 +2075,7 @@ func initFuncMap() {
|
||||||
"getJSON": getJSON,
|
"getJSON": getJSON,
|
||||||
"getenv": func(varName string) string { return os.Getenv(varName) },
|
"getenv": func(varName string) string { return os.Getenv(varName) },
|
||||||
"gt": gt,
|
"gt": gt,
|
||||||
"hasPrefix": func(a, b string) bool { return strings.HasPrefix(a, b) },
|
"hasPrefix": hasPrefix,
|
||||||
"highlight": highlight,
|
"highlight": highlight,
|
||||||
"htmlEscape": htmlEscape,
|
"htmlEscape": htmlEscape,
|
||||||
"htmlUnescape": htmlUnescape,
|
"htmlUnescape": htmlUnescape,
|
||||||
|
|
|
@ -804,6 +804,33 @@ func TestSlicestr(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHasPrefix(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
s interface{}
|
||||||
|
prefix interface{}
|
||||||
|
want interface{}
|
||||||
|
isErr bool
|
||||||
|
}{
|
||||||
|
{"abcd", "ab", true, false},
|
||||||
|
{"abcd", "cd", false, false},
|
||||||
|
{template.HTML("abcd"), "ab", true, false},
|
||||||
|
{template.HTML("abcd"), "cd", false, false},
|
||||||
|
{template.HTML("1234"), 12, true, false},
|
||||||
|
{template.HTML("1234"), 34, false, false},
|
||||||
|
{[]byte("abcd"), "ab", true, false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, c := range cases {
|
||||||
|
res, err := hasPrefix(c.s, c.prefix)
|
||||||
|
if (err != nil) != c.isErr {
|
||||||
|
t.Fatalf("[%d] unexpected isErr state: want %v, got %v, err = %v", i, c.isErr, err != nil, err)
|
||||||
|
}
|
||||||
|
if res != c.want {
|
||||||
|
t.Errorf("[%d] want %v, got %v", i, c.want, res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSubstr(t *testing.T) {
|
func TestSubstr(t *testing.T) {
|
||||||
var err error
|
var err error
|
||||||
var n int
|
var n int
|
||||||
|
|
Loading…
Reference in a new issue