diff --git a/docs-src/content/functions/strings.yml b/docs-src/content/functions/strings.yml index 07378fb0b..8c10a8f56 100644 --- a/docs-src/content/functions/strings.yml +++ b/docs-src/content/functions/strings.yml @@ -421,6 +421,23 @@ funcs: - | $ gomplate -i '{{ "_-foo-_" | strings.Trim "_-" }}' foo + - name: strings.TrimLeft + # released: v4.1.0 + description: | + Trims a string by removing the given characters from the beginning of the string. + This wraps Go's [`strings.TrimLeft`](https://pkg.go.dev/strings#TrimLeft). + pipeline: true + arguments: + - name: cutset + required: true + description: the set of characters to cut + - name: input + required: true + description: the input + examples: + - | + $ gomplate -i '{{ " - hello, world!" | strings.TrimLeft " " }}' + - hello, world! - name: strings.TrimPrefix released: v2.5.0 description: | @@ -439,6 +456,23 @@ funcs: - | $ gomplate -i '{{ "hello, world" | strings.TrimPrefix "hello, " }}' world + - name: strings.TrimRight + # released: v4.1.0 + description: | + Trims a string by removing the given characters from the end of the string. + This wraps Go's [`strings.TrimRight`](https://pkg.go.dev/strings#TrimRight). + pipeline: true + arguments: + - name: cutset + required: true + description: the set of characters to cut + - name: input + required: true + description: the input + examples: + - | + $ gomplate -i '{{ "hello, world! " | strings.TrimRight " " }}' + hello, world! - name: strings.TrimSpace alias: trimSpace released: v1.9.0 diff --git a/docs/content/functions/strings.md b/docs/content/functions/strings.md index fe37830fa..9ec1589af 100644 --- a/docs/content/functions/strings.md +++ b/docs/content/functions/strings.md @@ -669,6 +669,35 @@ $ gomplate -i '{{ "_-foo-_" | strings.Trim "_-" }}' foo ``` +## `strings.TrimLeft`_(unreleased)_ +**Unreleased:** _This function is in development, and not yet available in released builds of gomplate._ + +Trims a string by removing the given characters from the beginning of the string. +This wraps Go's [`strings.TrimLeft`](https://pkg.go.dev/strings#TrimLeft). + +### Usage + +``` +strings.TrimLeft cutset input +``` +``` +input | strings.TrimLeft cutset +``` + +### Arguments + +| name | description | +|------|-------------| +| `cutset` | _(required)_ the set of characters to cut | +| `input` | _(required)_ the input | + +### Examples + +```console +$ gomplate -i '{{ " - hello, world!" | strings.TrimLeft " " }}' +- hello, world! +``` + ## `strings.TrimPrefix` Returns a string without the provided leading prefix string, if the prefix is present. @@ -699,6 +728,35 @@ $ gomplate -i '{{ "hello, world" | strings.TrimPrefix "hello, " }}' world ``` +## `strings.TrimRight`_(unreleased)_ +**Unreleased:** _This function is in development, and not yet available in released builds of gomplate._ + +Trims a string by removing the given characters from the end of the string. +This wraps Go's [`strings.TrimRight`](https://pkg.go.dev/strings#TrimRight). + +### Usage + +``` +strings.TrimRight cutset input +``` +``` +input | strings.TrimRight cutset +``` + +### Arguments + +| name | description | +|------|-------------| +| `cutset` | _(required)_ the set of characters to cut | +| `input` | _(required)_ the input | + +### Examples + +```console +$ gomplate -i '{{ "hello, world! " | strings.TrimRight " " }}' +hello, world! +``` + ## `strings.TrimSpace` **Alias:** `trimSpace` diff --git a/internal/funcs/strings.go b/internal/funcs/strings.go index cb440868e..c07e09249 100644 --- a/internal/funcs/strings.go +++ b/internal/funcs/strings.go @@ -223,11 +223,21 @@ func (StringFuncs) Trim(cutset string, s interface{}) string { return strings.Trim(conv.ToString(s), cutset) } +// TrimLeft - +func (StringFuncs) TrimLeft(cutset string, s interface{}) string { + return strings.TrimLeft(conv.ToString(s), cutset) +} + // TrimPrefix - func (StringFuncs) TrimPrefix(cutset string, s interface{}) string { return strings.TrimPrefix(conv.ToString(s), cutset) } +// TrimRight - +func (StringFuncs) TrimRight(cutset string, s interface{}) string { + return strings.TrimRight(conv.ToString(s), cutset) +} + // TrimSuffix - func (StringFuncs) TrimSuffix(cutset string, s interface{}) string { return strings.TrimSuffix(conv.ToString(s), cutset) diff --git a/internal/funcs/strings_test.go b/internal/funcs/strings_test.go index e1561a0c7..032e45e37 100644 --- a/internal/funcs/strings_test.go +++ b/internal/funcs/strings_test.go @@ -256,3 +256,51 @@ func TestRuneCount(t *testing.T) { require.NoError(t, err) assert.Equal(t, 5, n) } + +func TestTrimLeft(t *testing.T) { + t.Parallel() + + sf := &StringFuncs{} + + testdata := []struct { + in interface{} + cutset string + out string + }{ + {``, ``, ``}, + {`foo`, ``, `foo`}, + {` foo`, ` `, `foo`}, + {` foo`, ` `, `foo`}, + {`fooBAR`, `foo`, `BAR`}, + {`-_fooBAR`, `-_`, `fooBAR`}, + } + + for _, d := range testdata { + trimmed := sf.TrimLeft(d.cutset, d.in) + assert.Equal(t, d.out, trimmed) + } +} + +func TestTrimRight(t *testing.T) { + t.Parallel() + + sf := &StringFuncs{} + + testdata := []struct { + in interface{} + cutset string + out string + }{ + {``, ``, ``}, + {`foo`, ``, `foo`}, + {`foo `, ` `, `foo`}, + {`foo `, ` `, `foo`}, + {`fooBAR`, `BAR`, `foo`}, + {`fooBAR-_`, `-_`, `fooBAR`}, + } + + for _, d := range testdata { + trimmed := sf.TrimRight(d.cutset, d.in) + assert.Equal(t, d.out, trimmed) + } +}