Skip to content

Commit

Permalink
Merge 65c643a into 79e3a68
Browse files Browse the repository at this point in the history
  • Loading branch information
ncatelli committed Jan 24, 2019
2 parents 79e3a68 + 65c643a commit f20315c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -18,4 +18,4 @@ before_install:
- go get github.com/mattn/goveralls
- if ! go get code.google.com/p/go.tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi
script:
- $HOME/gopath/bin/goveralls -service=travis-ci
- $HOME/gopath/bin/goveralls -service=travis-ci
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -696,6 +696,23 @@ This function also supports unicode strings.



#### randomintrange

Returns a random integer value between the first and second argument

* supported value types : int

```
{{ value | randomintrange 0 5}}
```

**Examples**

1. If input is {{ randomintrange 0 5 }}, the output could be "4".




#### striptags

Makes all possible efforts to strip all [X]HTML tags from given value.
Expand Down
6 changes: 6 additions & 0 deletions gtf.go
Expand Up @@ -427,6 +427,12 @@ var GtfTextFuncMap = textTemplate.FuncMap{

return ""
},
"randomintrange": func(min, max int, value interface{}) int {
defer recovery()

rand.Seed(time.Now().UTC().UnixNano())
return rand.Intn(max-min) + min
},
"striptags": func(s string) string {
return strings.TrimSpace(striptagsRegexp.ReplaceAllString(s, ""))
},
Expand Down
17 changes: 16 additions & 1 deletion gtf_test.go
Expand Up @@ -3,6 +3,7 @@ package gtf
import (
"bytes"
"html/template"
"strconv"
"testing"
)

Expand All @@ -13,6 +14,17 @@ func AssertEqual(t *testing.T, buffer *bytes.Buffer, testString string) {
buffer.Reset()
}

func AssertIntInRange(t *testing.T, buffer *bytes.Buffer, min, max int) {
parsedInt, err := strconv.Atoi(buffer.String())
if err != nil {
t.Error(err)
}
if parsedInt < min || parsedInt > max {
t.Errorf("Expected to be within the range of %d and %d, got %d", min, max, parsedInt)
}
buffer.Reset()
}

func ParseTest(buffer *bytes.Buffer, body string, data interface{}) {
tpl := New("test")
tpl.Parse(body)
Expand All @@ -33,7 +45,7 @@ func TestGtfFuncMap(t *testing.T) {

ParseTest(&buffer, "{{ \"the go programming language\" | title }}", "")
AssertEqual(t, &buffer, "The Go Programming Language")

ParseTest(&buffer, "{{ \"The Go Programming Language\" | default \"default value\" }}", "")
AssertEqual(t, &buffer, "The Go Programming Language")

Expand Down Expand Up @@ -358,6 +370,9 @@ func TestGtfFuncMap(t *testing.T) {
ParseTest(&buffer, "{{ . | random }}", [1]string{"go"})
AssertEqual(t, &buffer, "go")

ParseTest(&buffer, "{{ . | randomintrange 1 5 }}", false)
AssertIntInRange(t, &buffer, 1, 5)

ParseTest(&buffer, "{{ . | random }}", false)
AssertEqual(t, &buffer, "")

Expand Down

0 comments on commit f20315c

Please sign in to comment.