Skip to content

Commit

Permalink
Merge pull request #4 from krancour/add-string-contains
Browse files Browse the repository at this point in the history
Added string contains function.
  • Loading branch information
mattfarina committed Dec 29, 2015
2 parents 180608a + 1ccd322 commit 809a261
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ HELLO!HELLO!HELLO!HELLO!HELLO!
- wrap: Wrap text at the given column count
- wrapWith: Wrap text at the given column count, and with the given
string for a line terminator: `wrap 50 "\n\t" $string`
- contains: strings.Contains, but with the arguments switched: `contains substr str`. (This simplifies common pipelines)

### String Slice Functions:

Expand Down
3 changes: 3 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ String Functions
- randNumeric: Given a length, generate a string of digits.
- wrap: Force a line wrap at the given width. `wrap 80 "imagine a longer string"`
- wrapWith: Wrap a line at the given length, but using 'sep' instead of a newline. `wrapWith 50, "<br>", $html`
- contains: strings.Contains, but with the arguments switched: `contains substr str`. (This simplifies common pipelines)
String Slice Functions:
Expand Down Expand Up @@ -176,6 +177,8 @@ var genericMap = map[string]interface{}{
"swapcase": util.SwapCase,
"wrap": func(l int, s string) string { return util.Wrap(s, l) },
"wrapWith": func(l int, sep, str string) string { return util.WrapCustom(str, l, sep, true) },
// Switch order so that "foobar" | contains "foo"
"contains": func(substr string, str string) bool { return strings.Contains(str, substr) },

// Wrap Atoi to stop errors.
"atoi": func(a string) int { i, _ := strconv.Atoi(a); return i },
Expand Down
7 changes: 7 additions & 0 deletions functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ func TestRandom(t *testing.T) {

}

func TestContains(t *testing.T) {
tpl := `{{"foobar" | contains "foo"}}`
if err := runt(tpl, "true"); err != nil {
t.Error(err)
}
}

func runt(tpl, expect string) error {
return runtv(tpl, expect, "")
}
Expand Down

0 comments on commit 809a261

Please sign in to comment.