Skip to content

Commit

Permalink
contains, hasPrefix, hasSuffix
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Ban committed Mar 7, 2022
1 parent 362a608 commit ff2a120
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
9 changes: 0 additions & 9 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,6 @@ func unique(e []interface{}) []interface{} {
return r
}

func contains(e []string, c string) bool {
for _, s := range e {
if s == c {
return true
}
}
return false
}

func item(s, sep string, num int) string {
i := strings.Split(s, sep)
if len(i) <= num {
Expand Down
12 changes: 12 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,15 @@ func notconditional(s1, s2 string) string {
}
return s2
}

func contains(substr string, str string) bool {
return strings.Contains(str, substr)
}

func hasPrefix(substr string, str string) bool {
return strings.HasPrefix(str, substr)
}

func hasSuffix(substr string, str string) bool {
return strings.HasSuffix(str, substr)
}
5 changes: 4 additions & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var flock = sync.Mutex{}

var fmap = template.FuncMap{
"add": add,
"concat": concat, // concat "a" "b" => "ab"
"concat": concat, // concat "a" "b" => "ab"
"contains": contains, // contains "a" "abc" => true
"createMap": createMap,
"date": dateFmt, // "2017-03-31 19:59:11" | date "06.01.02" => "17.03.31"
"dateFrom": dateFmtLayout,
Expand All @@ -28,6 +29,8 @@ var fmap = template.FuncMap{
"fixlenr": fixlenright,
"float": tofloat, // float "0123.234" => 123.234
"formatUKDate": formatUKDate,
"hasPrefix": hasPrefix, // hasPrefix "a" "ab" => true
"hasSuffix": hasSuffix, // hasSuffix "a" "ba" => true
"ifthen": conditional, // ifthen "a" "b" => a, ifthen "" "b" => b
"in_array": inArray,
"int": toint, // int "0123" => 123
Expand Down
6 changes: 3 additions & 3 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ func TestTemplate(t *testing.T) {
Result: "<items>\n <item>\n <p>a &amp; b</p>\n <z>1</z>\n </item>\n <item>\n <p>b</p>\n <z>2</z>\n </item>\n</items>",
},
"xml_encode array itemtag": {
Template: `{{xml_encode .A "product"}}`,
Template: `{{xml .A "product"}}`,
Values: map[string]interface{}{"A": []interface{}{map[string]interface{}{"z": 1, "p": "a & b"}, map[string]interface{}{"z": 2, "p": "b"}}},
Result: "<items>\n <product>\n <p>a &amp; b</p>\n <z>1</z>\n </product>\n <product>\n <p>b</p>\n <z>2</z>\n </product>\n</items>",
},
"xml_encode array itemtag roottags": {
Template: `{{xml_encode .A "product" "products"}}`,
Template: `{{xml .A "product" "products"}}`,
Values: map[string]interface{}{"A": []interface{}{map[string]interface{}{"z": 1, "p": "a & b"}, map[string]interface{}{"z": 2, "p": "b"}}},
Result: "<products>\n <product>\n <p>a &amp; b</p>\n <z>1</z>\n </product>\n <product>\n <p>b</p>\n <z>2</z>\n </product>\n</products>",
},
"xml_encode string": {
Template: `{{xml_encode .A}}`,
Template: `{{xml .A}}`,
Values: map[string]interface{}{"A": "something"},
Result: "<![CDATA[something]]>",
},
Expand Down

0 comments on commit ff2a120

Please sign in to comment.