diff options
| author | Javier Solana <javiersolanahuertas@gmail.com> | 2024-07-06 15:32:01 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-06 09:32:01 -0400 |
| commit | bdf3a1eb92020a0d1ce202df14b49f2f13445476 (patch) | |
| tree | 98cb40a4a366863cab8e710949851bd4c2da1d5b | |
| parent | 80b7c5a1aba49239b336d7eeed2525acc2d361be (diff) | |
feat(strings): New functions TrimRight and TrimLeft (#2148)
Signed-off-by: Javier Solana javier.solana@cabify.com
Signed-off-by: Javier Solana javier.solana@cabify.com
Co-authored-by: Javier Solana <javier.solana@cabify.com>
| -rw-r--r-- | docs-src/content/functions/strings.yml | 34 | ||||
| -rw-r--r-- | docs/content/functions/strings.md | 58 | ||||
| -rw-r--r-- | internal/funcs/strings.go | 10 | ||||
| -rw-r--r-- | internal/funcs/strings_test.go | 48 |
4 files changed, 150 insertions, 0 deletions
diff --git a/docs-src/content/functions/strings.yml b/docs-src/content/functions/strings.yml index 07378fb0..8c10a8f5 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 fe37830f..9ec1589a 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 cb440868..c07e0924 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 e1561a0c..032e45e3 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) + } +} |
