Skip to content

Commit

Permalink
feat: Extend replaceAll to all stringish types
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Mar 5, 2024
1 parent 9965c51 commit 994c83e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ func NewFuncMap() template.FuncMap {
"prefixLines": prefixLinesTemplateFunc,
"quote": eachString(strconv.Quote),
"regexpReplaceAll": regexpReplaceAllTemplateFunc,
"replaceAll": func(old, new, s string) string {
return strings.ReplaceAll(s, old, new)
"replaceAll": func(old, new_ string, v any) any {
return eachString(func(s string) any {
return strings.ReplaceAll(s, old, new_)
})(v)
},
"stat": eachString(statTemplateFunc),
"toJSON": toJSONTemplateFunc,
Expand Down
4 changes: 4 additions & 0 deletions templatefuncs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ func TestFuncMap(t *testing.T) {
template: `{{ "abcba" | replaceAll "b" "d" }}`,
expected: `adcda`,
},
{
template: `{{ list "abc" "cba" | replaceAll "b" "d" }}`,
expected: "[adc cda]",
},
{
template: `{{ quote "a" }}`,
expected: `"a"`,
Expand Down

0 comments on commit 994c83e

Please sign in to comment.