Skip to content

Commit 29a2da0

Browse files
moorereasonbep
authored andcommitted
tpl: Cleanup strings.TrimPrefix and TrimSuffix
These funcs were added during the move to namespaces but were undocumented. This commit fixes the order of the arguments and adds the funcs to the method mapping.
1 parent 7674ad7 commit 29a2da0

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

‎tpl/strings/init.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,29 @@ func init() {
119119
},
120120
)
121121

122+
ns.AddMethodMapping(ctx.TrimPrefix,
123+
nil,
124+
[][2]string{
125+
{`{{ "aabbaa" | strings.TrimPrefix "a" }}`, `abbaa`},
126+
{`{{ "aabbaa" | strings.TrimPrefix "aa" }}`, `bbaa`},
127+
},
128+
)
129+
122130
ns.AddMethodMapping(ctx.TrimRight,
123131
nil,
124132
[][2]string{
125133
{`{{ "aabbaa" | strings.TrimRight "a" }}`, `aabb`},
126134
},
127135
)
128136

137+
ns.AddMethodMapping(ctx.TrimSuffix,
138+
nil,
139+
[][2]string{
140+
{`{{ "aabbaa" | strings.TrimSuffix "a" }}`, `aabba`},
141+
{`{{ "aabbaa" | strings.TrimSuffix "aa" }}`, `aabb`},
142+
},
143+
)
144+
129145
ns.AddMethodMapping(ctx.Title,
130146
[]string{"title"},
131147
[][2]string{

‎tpl/strings/strings.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ func (ns *Namespace) TrimLeft(cutset, s interface{}) (string, error) {
365365

366366
// TrimPrefix returns s without the provided leading prefix string. If s doesn't
367367
// start with prefix, s is returned unchanged.
368-
func (ns *Namespace) TrimPrefix(s, prefix interface{}) (string, error) {
368+
func (ns *Namespace) TrimPrefix(prefix, s interface{}) (string, error) {
369369
ss, err := cast.ToStringE(s)
370370
if err != nil {
371371
return "", err
@@ -397,7 +397,7 @@ func (ns *Namespace) TrimRight(cutset, s interface{}) (string, error) {
397397

398398
// TrimSuffix returns s without the provided trailing suffix string. If s
399399
// doesn't end with suffix, s is returned unchanged.
400-
func (ns *Namespace) TrimSuffix(s, suffix interface{}) (string, error) {
400+
func (ns *Namespace) TrimSuffix(suffix, s interface{}) (string, error) {
401401
ss, err := cast.ToStringE(s)
402402
if err != nil {
403403
return "", err

‎tpl/strings/strings_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ func TestTrimPrefix(t *testing.T) {
627627
} {
628628
errMsg := fmt.Sprintf("[%d] %v", i, test)
629629

630-
result, err := ns.TrimPrefix(test.s, test.prefix)
630+
result, err := ns.TrimPrefix(test.prefix, test.s)
631631

632632
if b, ok := test.expect.(bool); ok && !b {
633633
require.Error(t, err, errMsg)
@@ -692,7 +692,7 @@ func TestTrimSuffix(t *testing.T) {
692692
} {
693693
errMsg := fmt.Sprintf("[%d] %v", i, test)
694694

695-
result, err := ns.TrimSuffix(test.s, test.suffix)
695+
result, err := ns.TrimSuffix(test.suffix, test.s)
696696

697697
if b, ok := test.expect.(bool); ok && !b {
698698
require.Error(t, err, errMsg)

0 commit comments

Comments
 (0)