mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-20 07:13:06 +00:00
tpl: Add strings.TrimLeft and TrimRight
This commit is contained in:
parent
08f48b91d6
commit
7674ad7382
3 changed files with 116 additions and 0 deletions
|
@ -112,6 +112,20 @@ func init() {
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
ns.AddMethodMapping(ctx.TrimLeft,
|
||||||
|
nil,
|
||||||
|
[][2]string{
|
||||||
|
{`{{ "aabbaa" | strings.TrimLeft "a" }}`, `bbaa`},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
ns.AddMethodMapping(ctx.TrimRight,
|
||||||
|
nil,
|
||||||
|
[][2]string{
|
||||||
|
{`{{ "aabbaa" | strings.TrimRight "a" }}`, `aabb`},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
ns.AddMethodMapping(ctx.Title,
|
ns.AddMethodMapping(ctx.Title,
|
||||||
[]string{"title"},
|
[]string{"title"},
|
||||||
[][2]string{
|
[][2]string{
|
||||||
|
|
|
@ -347,6 +347,22 @@ func (ns *Namespace) Trim(s, cutset interface{}) (string, error) {
|
||||||
return _strings.Trim(ss, sc), nil
|
return _strings.Trim(ss, sc), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TrimLeft returns a slice of the string s with all leading characters
|
||||||
|
// contained in cutset removed.
|
||||||
|
func (ns *Namespace) TrimLeft(cutset, s interface{}) (string, error) {
|
||||||
|
ss, err := cast.ToStringE(s)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
sc, err := cast.ToStringE(cutset)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return _strings.TrimLeft(ss, sc), nil
|
||||||
|
}
|
||||||
|
|
||||||
// TrimPrefix returns s without the provided leading prefix string. If s doesn't
|
// TrimPrefix returns s without the provided leading prefix string. If s doesn't
|
||||||
// start with prefix, s is returned unchanged.
|
// start with prefix, s is returned unchanged.
|
||||||
func (ns *Namespace) TrimPrefix(s, prefix interface{}) (string, error) {
|
func (ns *Namespace) TrimPrefix(s, prefix interface{}) (string, error) {
|
||||||
|
@ -363,6 +379,22 @@ func (ns *Namespace) TrimPrefix(s, prefix interface{}) (string, error) {
|
||||||
return _strings.TrimPrefix(ss, sx), nil
|
return _strings.TrimPrefix(ss, sx), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TrimRight returns a slice of the string s with all trailing characters
|
||||||
|
// contained in cutset removed.
|
||||||
|
func (ns *Namespace) TrimRight(cutset, s interface{}) (string, error) {
|
||||||
|
ss, err := cast.ToStringE(s)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
sc, err := cast.ToStringE(cutset)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return _strings.TrimRight(ss, sc), nil
|
||||||
|
}
|
||||||
|
|
||||||
// TrimSuffix returns s without the provided trailing suffix string. If s
|
// TrimSuffix returns s without the provided trailing suffix string. If s
|
||||||
// doesn't end with suffix, s is returned unchanged.
|
// doesn't end with suffix, s is returned unchanged.
|
||||||
func (ns *Namespace) TrimSuffix(s, suffix interface{}) (string, error) {
|
func (ns *Namespace) TrimSuffix(s, suffix interface{}) (string, error) {
|
||||||
|
|
|
@ -574,6 +574,41 @@ func TestTrim(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTrimLeft(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
for i, test := range []struct {
|
||||||
|
s interface{}
|
||||||
|
cutset interface{}
|
||||||
|
expect interface{}
|
||||||
|
}{
|
||||||
|
{"abba", "a", "bba"},
|
||||||
|
{"abba", "ab", ""},
|
||||||
|
{"<tag>", "<>", "tag>"},
|
||||||
|
{`"quote"`, `"`, `quote"`},
|
||||||
|
{1221, "1", "221"},
|
||||||
|
{1221, "12", ""},
|
||||||
|
{"007", "0", "7"},
|
||||||
|
{template.HTML("<tag>"), "<>", "tag>"},
|
||||||
|
{[]byte("<tag>"), "<>", "tag>"},
|
||||||
|
// errors
|
||||||
|
{"", tstNoStringer{}, false},
|
||||||
|
{tstNoStringer{}, "", false},
|
||||||
|
} {
|
||||||
|
errMsg := fmt.Sprintf("[%d] %v", i, test)
|
||||||
|
|
||||||
|
result, err := ns.TrimLeft(test.cutset, test.s)
|
||||||
|
|
||||||
|
if b, ok := test.expect.(bool); ok && !b {
|
||||||
|
require.Error(t, err, errMsg)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
require.NoError(t, err, errMsg)
|
||||||
|
assert.Equal(t, test.expect, result, errMsg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTrimPrefix(t *testing.T) {
|
func TestTrimPrefix(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
@ -604,6 +639,41 @@ func TestTrimPrefix(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTrimRight(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
for i, test := range []struct {
|
||||||
|
s interface{}
|
||||||
|
cutset interface{}
|
||||||
|
expect interface{}
|
||||||
|
}{
|
||||||
|
{"abba", "a", "abb"},
|
||||||
|
{"abba", "ab", ""},
|
||||||
|
{"<tag>", "<>", "<tag"},
|
||||||
|
{`"quote"`, `"`, `"quote`},
|
||||||
|
{1221, "1", "122"},
|
||||||
|
{1221, "12", ""},
|
||||||
|
{"007", "0", "007"},
|
||||||
|
{template.HTML("<tag>"), "<>", "<tag"},
|
||||||
|
{[]byte("<tag>"), "<>", "<tag"},
|
||||||
|
// errors
|
||||||
|
{"", tstNoStringer{}, false},
|
||||||
|
{tstNoStringer{}, "", false},
|
||||||
|
} {
|
||||||
|
errMsg := fmt.Sprintf("[%d] %v", i, test)
|
||||||
|
|
||||||
|
result, err := ns.TrimRight(test.cutset, test.s)
|
||||||
|
|
||||||
|
if b, ok := test.expect.(bool); ok && !b {
|
||||||
|
require.Error(t, err, errMsg)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
require.NoError(t, err, errMsg)
|
||||||
|
assert.Equal(t, test.expect, result, errMsg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTrimSuffix(t *testing.T) {
|
func TestTrimSuffix(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue