Skip to content

Commit

Permalink
feat(strings): New functions TrimRight and TrimLeft (#2148)
Browse files Browse the repository at this point in the history
Signed-off-by: Javier Solana [email protected]

Signed-off-by: Javier Solana [email protected]
Co-authored-by: Javier Solana <[email protected]>
  • Loading branch information
jsolana and Javier Solana authored Jul 6, 2024
1 parent 80b7c5a commit bdf3a1e
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs-src/content/functions/strings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand All @@ -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
Expand Down
58 changes: 58 additions & 0 deletions docs/content/functions/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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`
Expand Down
10 changes: 10 additions & 0 deletions internal/funcs/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 48 additions & 0 deletions internal/funcs/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit bdf3a1e

Please sign in to comment.