diff --git a/templatehelper/templatehelper.go b/templatehelper/templatehelper.go index 26993fbe..c56f6bab 100644 --- a/templatehelper/templatehelper.go +++ b/templatehelper/templatehelper.go @@ -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) @@ -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, diff --git a/templatehelper/templatehelper_test.go b/templatehelper/templatehelper_test.go index 1d5fe8df..eed304db 100644 --- a/templatehelper/templatehelper_test.go +++ b/templatehelper/templatehelper_test.go @@ -21,12 +21,12 @@ 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}}`, ""}, @@ -34,8 +34,17 @@ func Test_FuncMap_Positive(t *testing.T) { {`{{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"}, @@ -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]"}, @@ -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"}, // ... }