Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a few helpful template filters #336

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion templatehelper/templatehelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ import (
// FuncMap contains a few convenient template helpers
var (
FuncMap = template.FuncMap{
"Array": func(values ...interface{}) []interface{} {
return values
},
"JSON": func(values ...interface{}) htmlTemplate.JS {
json, _ := json.Marshal(values)
return htmlTemplate.JS(json)
Expand Down Expand Up @@ -74,10 +77,33 @@ var (
}
return items[len(items)-1], nil
},
"ContainsAny": func(target, subs string, other ...string) bool {
// contains any symbol from the given string
if len(other) == 0 {
return strings.ContainsAny(target, subs)
}
// contains any of the given strings
for _, another := range other {
if strings.Contains(target, another) {
return true
}
}
return strings.Contains(target, subs)
},
"ContainsAll": func(target string, other ...string) (bool, error) {
if len(other) == 0 {
return false, errors.New("to find substring in a string, use Contains")
}
for _, another := range other {
if !strings.Contains(target, another) {
return false, nil
}
}
return true, nil
},
// strings functions
"Compare": strings.Compare, // 1.5+ only
"Contains": strings.Contains,
"ContainsAny": strings.ContainsAny,
"Count": strings.Count,
"EqualFold": strings.EqualFold,
"HasPrefix": strings.HasPrefix,
Expand Down
41 changes: 38 additions & 3 deletions templatehelper/templatehelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,30 @@ func Test_FuncMap_Positive(t *testing.T) {
text string
expected string
}{
// sanity checks
// SANITY CHECKS //

{`{{if true}}ok{{end}}`, "ok"},
{`{{if false}}ok{{end}}`, ""},

// boolean filters
// BOOLEAN FILTERS //

{`{{if Matches "123" "\\d+"}}ok{{end}}`, "ok"},
{`{{if Matches "hello" "\\d+"}}ok{{end}}`, ""},

{`{{if Contains "123" "2"}}ok{{end}}`, "ok"},
{`{{if Contains "123" "4"}}ok{{end}}`, ""},

// contains any of symbols
{`{{if ContainsAny "123" "24"}}ok{{end}}`, "ok"},
{`{{if ContainsAny "123" "45"}}ok{{end}}`, ""},
// contains any of strings
{`{{if ContainsAny "123456" "23" "78"}}ok{{end}}`, "ok"},
{`{{if ContainsAny "123456" "78" "23"}}ok{{end}}`, "ok"},
{`{{if ContainsAny "123456" "46" "24"}}ok{{end}}`, ""},

{`{{if ContainsAll "123456" "23" "56"}}ok{{end}}`, "ok"},
{`{{if ContainsAll "123456" "23" "78"}}ok{{end}}`, ""},
{`{{if ContainsAll "123456" "24" "35"}}ok{{end}}`, ""},

{`{{if EqualFold "HellO" "hello"}}ok{{end}}`, "ok"},
{`{{if EqualFold "ПривеТ" "привет"}}ok{{end}}`, "ok"},
Expand All @@ -47,7 +56,7 @@ func Test_FuncMap_Positive(t *testing.T) {
{`{{if HasSuffix "hello" "lo"}}ok{{end}}`, "ok"},
{`{{if HasSuffix "hello" "he"}}ok{{end}}`, ""},

// filters returning a string
// FILTERS RETURNING A STRING //

{`{{JSON 123}}`, "[123]"},

Expand All @@ -73,6 +82,32 @@ func Test_FuncMap_Positive(t *testing.T) {

{`{{Replace "1234" "23" "56" -1}}`, "1564"},
{`{{Replace "12223" "2" "5" 1}}`, "15223"},

{`{{Title "aragorn son of arathorn"}}`, "Aragorn Son Of Arathorn"},
{`{{Title "dz"}}`, "Dz"},

{`{{ToTitle "aragorn son of arathorn"}}`, "ARAGORN SON OF ARATHORN"},
{`{{ToTitle "dz"}}`, "Dz"},

{`{{ToLower "Aragorn Son Of Arathorn"}}`, "aragorn son of arathorn"},
{`{{ToLower "dz"}}`, "dz"},

{`{{ToUpper "Aragorn Son Of Arathorn"}}`, "ARAGORN SON OF ARATHORN"},
{`{{ToUpper "dz"}}`, "DZ"},

{`{{Trim "-_=oh=hi-mark---=" "=_-"}}`, "oh=hi-mark"},
{`{{TrimLeft "-_=oh=hi-mark---=" "=_-"}}`, "oh=hi-mark---="},
{`{{TrimRight "-_=oh=hi-mark---=" "=_-"}}`, "-_=oh=hi-mark"},
{`{{TrimSpace " oh hi mark \\n\\t "}}`, "oh hi mark \\n\\t"},

{`{{TrimPrefix "123hello123" "123"}}`, "hello123"},
{`{{TrimPrefix "123hello123" "231"}}`, "123hello123"},
{`{{TrimSuffix "123hello123" "123"}}`, "123hello"},
{`{{TrimSuffix "123hello123" "231"}}`, "123hello123"},

// OTHER FILTERS //
{`{{Array "oh" "hi" "mark"}}`, "[oh hi mark]"},
{`{{Count "why do you cry willy why do you cry why willy why" "why"}}`, "4"},
// ...
}

Expand Down