From d8bea84adcc164723510159145c4b547dadb8f34 Mon Sep 17 00:00:00 2001 From: Ghazanfar Date: Tue, 7 Mar 2023 14:08:57 +0530 Subject: [PATCH] Make TrimSpace available for use in the template (#3281) * Make TrimSpace available for use in the template Signed-off-by: Ghazanfar --- docs/notifications.md | 1 + template/template.go | 8 +++++--- template/template_test.go | 5 +++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/docs/notifications.md b/docs/notifications.md index 7a953f7c..2f60eb9c 100644 --- a/docs/notifications.md +++ b/docs/notifications.md @@ -89,6 +89,7 @@ templating. | title | string |[strings.Title](http://golang.org/pkg/strings/#Title), capitalises first character of each word. | | toUpper | string | [strings.ToUpper](http://golang.org/pkg/strings/#ToUpper), converts all characters to upper case. | | toLower | string | [strings.ToLower](http://golang.org/pkg/strings/#ToLower), converts all characters to lower case. | +| trimSpace | string | [strings.TrimSpace](https://pkg.go.dev/strings#TrimSpace), removes leading and trailing white spaces. | | match | pattern, string | [Regexp.MatchString](https://golang.org/pkg/regexp/#MatchString). Match a string using Regexp. | | reReplaceAll | pattern, replacement, text | [Regexp.ReplaceAllString](http://golang.org/pkg/regexp/#Regexp.ReplaceAllString) Regexp substitution, unanchored. | | join | sep string, s []string | [strings.Join](http://golang.org/pkg/strings/#Join), concatenates the elements of s to create a single string. The separator string sep is placed between elements in the resulting string. (note: argument order inverted for easier pipelining in templates.) | diff --git a/template/template.go b/template/template.go index a42cf03d..3295544a 100644 --- a/template/template.go +++ b/template/template.go @@ -168,9 +168,11 @@ func (t *Template) ExecuteHTMLString(html string, data interface{}) (string, err type FuncMap map[string]interface{} var DefaultFuncs = FuncMap{ - "toUpper": strings.ToUpper, - "toLower": strings.ToLower, - "title": cases.Title(language.AmericanEnglish).String, + "toUpper": strings.ToUpper, + "toLower": strings.ToLower, + "trimSpace": strings.TrimSpace, + + "title": cases.Title(language.AmericanEnglish).String, // join is equal to strings.Join but inverts the argument order // for easier pipelining in templates. "join": func(sep string, s []string) string { diff --git a/template/template_test.go b/template/template_test.go index 03cbc45b..c28fa592 100644 --- a/template/template_test.go +++ b/template/template_test.go @@ -328,6 +328,11 @@ func TestTemplateExpansion(t *testing.T) { in: `{{ "abc" | title }}`, exp: "Abc", }, + { + title: "Template using TrimSpace", + in: `{{ " a b c " | trimSpace }}`, + exp: "a b c", + }, { title: "Template using positive match", in: `{{ if match "^a" "abc"}}abc{{ end }}`,