From 9f57bbd8b5577eba5ff731955f6f71db4bbd2979 Mon Sep 17 00:00:00 2001 From: Gram Date: Mon, 5 Oct 2020 10:26:45 +0200 Subject: [PATCH 1/3] improve ContainsAny --- templatehelper/templatehelper.go | 12 +++++++++++- templatehelper/templatehelper_test.go | 5 +++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/templatehelper/templatehelper.go b/templatehelper/templatehelper.go index 26993fbe..3516830e 100644 --- a/templatehelper/templatehelper.go +++ b/templatehelper/templatehelper.go @@ -74,10 +74,20 @@ var ( } return items[len(items)-1], nil }, + "ContainsAny": func(target, subs string, other ...string) bool { + if len(other) == 0 { + return strings.ContainsAny(target, subs) + } + for _, another := range other { + if strings.Contains(target, another) { + return true + } + } + return strings.Contains(target, subs) + }, // 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..2ec08b31 100644 --- a/templatehelper/templatehelper_test.go +++ b/templatehelper/templatehelper_test.go @@ -34,8 +34,13 @@ 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 EqualFold "HellO" "hello"}}ok{{end}}`, "ok"}, {`{{if EqualFold "ПривеТ" "привет"}}ok{{end}}`, "ok"}, From 383f9d925918f4b72309d09e2d4b681611eff500 Mon Sep 17 00:00:00 2001 From: Gram Date: Mon, 5 Oct 2020 10:46:16 +0200 Subject: [PATCH 2/3] +ContainsAll and more tests --- templatehelper/templatehelper.go | 13 +++++++++++ templatehelper/templatehelper_test.go | 33 +++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/templatehelper/templatehelper.go b/templatehelper/templatehelper.go index 3516830e..86bf76cb 100644 --- a/templatehelper/templatehelper.go +++ b/templatehelper/templatehelper.go @@ -75,9 +75,11 @@ 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 @@ -85,6 +87,17 @@ var ( } 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, diff --git a/templatehelper/templatehelper_test.go b/templatehelper/templatehelper_test.go index 2ec08b31..435a3ea5 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}}`, ""}, @@ -42,6 +42,10 @@ func Test_FuncMap_Positive(t *testing.T) { {`{{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"}, {`{{if EqualFold "good" "goed"}}ok{{end}}`, ""}, @@ -52,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]"}, @@ -78,7 +82,28 @@ 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"}, } for _, tcase := range cases { From e7c49548bdc59e34c7c7515bf224474d2ade6512 Mon Sep 17 00:00:00 2001 From: Gram Date: Mon, 5 Oct 2020 10:52:16 +0200 Subject: [PATCH 3/3] +Array filter --- templatehelper/templatehelper.go | 3 +++ templatehelper/templatehelper_test.go | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/templatehelper/templatehelper.go b/templatehelper/templatehelper.go index 86bf76cb..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) diff --git a/templatehelper/templatehelper_test.go b/templatehelper/templatehelper_test.go index 435a3ea5..eed304db 100644 --- a/templatehelper/templatehelper_test.go +++ b/templatehelper/templatehelper_test.go @@ -104,6 +104,11 @@ func Test_FuncMap_Positive(t *testing.T) { {`{{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"}, + // ... } for _, tcase := range cases {