Skip to content

Commit

Permalink
Merge pull request #1558 from jsanant/add-trim-functions
Browse files Browse the repository at this point in the history
Added trim functions.
  • Loading branch information
eikenb authored Apr 12, 2022
2 parents 19156b3 + 03ed5d7 commit 0a8d8e2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/templating-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ provides the following functions:
- [mergeMap](#mergemap)
- [mergeMapWithOverride](#mergemapwithoverride)
- [trimSpace](#trimspace)
- [trim](#trim)
- [trimPrefix](#trimprefix)
- [trimSuffix](#trimsuffix)
- [parseBool](#parsebool)
- [parseFloat](#parsefloat)
- [parseInt](#parseint)
Expand Down Expand Up @@ -1276,6 +1279,30 @@ Takes the provided input and trims all whitespace, tabs and newlines:
{{ file "/etc/ec2_version" | trimSpace }}
```

### `trim`

Takes the provided input and trims all leading and trailing unicode:

```golang
{{ "hello world!!" | trim "!!" }}
```

### `trimPrefix`

Takes the provided input and trims leading prefix string:

```golang
{{ "hello world!!" | trimPrefix "hello" }}
```

### `trimSuffix`

Takes the provided input and trims trailing suffix string:

```golang
{{ "hello world!!" | trimSuffix "world!!" }}
```

### `parseBool`

Takes the given string and parses it as a boolean:
Expand Down
15 changes: 15 additions & 0 deletions template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,21 @@ func join(sep string, a []string) (string, error) {
return strings.Join(a, sep), nil
}

// trim is a version of strings.Trim that can be piped
func trim(cutset string, s string) (string, error) {
return strings.Trim(s, cutset), nil
}

// trimPrefix is a version of strings.TrimPrefix that can be piped
func trimPrefix(prefix string, s string) (string, error) {
return strings.TrimPrefix(s, prefix), nil
}

// trimSuffix is a version of strings.TrimSuffix that can be piped
func trimSuffix(suffix string, s string) (string, error) {
return strings.TrimSuffix(s, suffix), nil
}

// TrimSpace is a version of strings.TrimSpace that can be piped
func trimSpace(s string) (string, error) {
return strings.TrimSpace(s), nil
Expand Down
3 changes: 3 additions & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ func funcMap(i *funcMapInput) template.FuncMap {
"indent": indent,
"loop": loop,
"join": join,
"trim": trim,
"trimPrefix": trimPrefix,
"trimSuffix": trimSuffix,
"trimSpace": trimSpace,
"parseBool": parseBool,
"parseFloat": parseFloat,
Expand Down
33 changes: 33 additions & 0 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,39 @@ func TestTemplate_Execute(t *testing.T) {
"a;b;c",
false,
},
{
"helper_trim",
&NewTemplateInput{
Contents: `{{ "!!hello world!!" | trim "!!" }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"hello world",
false,
},
{
"helper_trimPrefix",
&NewTemplateInput{
Contents: `{{ "hello world!!" | trimPrefix "hello " }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"world!!",
false,
},
{
"helper_trimSuffix",
&NewTemplateInput{
Contents: `{{ "hello world!!" | trimSuffix " world!!" }}`,
},
&ExecuteInput{
Brain: NewBrain(),
},
"hello",
false,
},
{
"helper_parseBool",
&NewTemplateInput{
Expand Down

0 comments on commit 0a8d8e2

Please sign in to comment.