tpl: Cleanup strings.TrimPrefix and TrimSuffix
authorCameron Moore <moorereason@gmail.com>
Mon, 14 Aug 2017 23:30:04 +0000 (18:30 -0500)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 18 Aug 2017 12:21:58 +0000 (14:21 +0200)
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.

tpl/strings/init.go
tpl/strings/strings.go
tpl/strings/strings_test.go

index b7491b81dc2559e2ba4c597a41e222329419597c..dc674c52207e8eb6e2b3aee3fa9397a6eb37d2dd 100644 (file)
@@ -119,6 +119,14 @@ func init() {
                        },
                )
 
+               ns.AddMethodMapping(ctx.TrimPrefix,
+                       nil,
+                       [][2]string{
+                               {`{{ "aabbaa" | strings.TrimPrefix "a" }}`, `abbaa`},
+                               {`{{ "aabbaa" | strings.TrimPrefix "aa" }}`, `bbaa`},
+                       },
+               )
+
                ns.AddMethodMapping(ctx.TrimRight,
                        nil,
                        [][2]string{
@@ -126,6 +134,14 @@ func init() {
                        },
                )
 
+               ns.AddMethodMapping(ctx.TrimSuffix,
+                       nil,
+                       [][2]string{
+                               {`{{ "aabbaa" | strings.TrimSuffix "a" }}`, `aabba`},
+                               {`{{ "aabbaa" | strings.TrimSuffix "aa" }}`, `aabb`},
+                       },
+               )
+
                ns.AddMethodMapping(ctx.Title,
                        []string{"title"},
                        [][2]string{
index 1e1bda493891c53e1e875ee4f1e08d428b764d98..f8acbfe2b22924b0d7fdac2e558c3a304db64139 100644 (file)
@@ -365,7 +365,7 @@ func (ns *Namespace) TrimLeft(cutset, s interface{}) (string, error) {
 
 // TrimPrefix returns s without the provided leading prefix string. If s doesn't
 // start with prefix, s is returned unchanged.
-func (ns *Namespace) TrimPrefix(s, prefix interface{}) (string, error) {
+func (ns *Namespace) TrimPrefix(prefix, s interface{}) (string, error) {
        ss, err := cast.ToStringE(s)
        if err != nil {
                return "", err
@@ -397,7 +397,7 @@ func (ns *Namespace) TrimRight(cutset, s interface{}) (string, error) {
 
 // TrimSuffix returns s without the provided trailing suffix string. If s
 // doesn't end with suffix, s is returned unchanged.
-func (ns *Namespace) TrimSuffix(s, suffix interface{}) (string, error) {
+func (ns *Namespace) TrimSuffix(suffix, s interface{}) (string, error) {
        ss, err := cast.ToStringE(s)
        if err != nil {
                return "", err
index 8c6b97602f0d654662591b763a1c44e77713d296..3ab73392c0fea582ecf697194acdb60037a2c65d 100644 (file)
@@ -627,7 +627,7 @@ func TestTrimPrefix(t *testing.T) {
        } {
                errMsg := fmt.Sprintf("[%d] %v", i, test)
 
-               result, err := ns.TrimPrefix(test.s, test.prefix)
+               result, err := ns.TrimPrefix(test.prefix, test.s)
 
                if b, ok := test.expect.(bool); ok && !b {
                        require.Error(t, err, errMsg)
@@ -692,7 +692,7 @@ func TestTrimSuffix(t *testing.T) {
        } {
                errMsg := fmt.Sprintf("[%d] %v", i, test)
 
-               result, err := ns.TrimSuffix(test.s, test.suffix)
+               result, err := ns.TrimSuffix(test.suffix, test.s)
 
                if b, ok := test.expect.(bool); ok && !b {
                        require.Error(t, err, errMsg)