Skip to content

Commit

Permalink
Add more functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
leekchan committed Jul 14, 2015
1 parent 8138d6c commit 3134947
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 33 deletions.
51 changes: 43 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,18 @@ If value is "The Go Programming Language", the output will be "the go programmin



### stringTruncateChars
### stringUpper

Converts the given string into all uppercase.

```
{{ value | stringUpper }}
```
If value is "The Go Programming Language", the output will be "THE GO PROGRAMMING LANGUAGE".



### stringTruncatechars

Truncates the given string if it is longer than the specified number of characters. Truncated strings will end with a translatable ellipsis sequence ("...")

Expand All @@ -104,17 +115,41 @@ Truncates the given string if it is longer than the specified number of characte
This function also supports unicode strings.

```
{{ value | stringTruncateChars 12 }}
{{ value | stringTruncatechars 12 }}
```

**Examples**

1. If input is {{ "The Go Programming Language" | stringTruncateChars 12 }}, the output will be "The Go Pr...". (basic string)
1. If input is {{ "안녕하세요. 반갑습니다." | stringTruncateChars 12 }}, the output will be "안녕하세요. 반갑...". (unicode)
1. If input is {{ "안녕하세요. The Go Programming Language" | stringTruncateChars 30 }}, the output will be "안녕하세요. The Go Programming L...". (unicode)
1. If input is {{ "The" | stringTruncateChars 30 }}, the output will be "The". (If the length of the given string is shorter than the argument, the output will be the original string.)
1. If input is {{ "The Go Programming Language" | stringTruncateChars 3 }}, the output will be "The". (If the argument is less than or equal to 3, the output will not contain "...".)
1. If input is {{ "The Go" | stringTruncateChars -1 }}, the output will be "The Go". (If the argument is less than 0, the argument will be ignored.)
1. If input is {{ "The Go Programming Language" | stringTruncatechars 12 }}, the output will be "The Go Pr...". (basic string)
1. If input is {{ "안녕하세요. 반갑습니다." | stringTruncatechars 12 }}, the output will be "안녕하세요. 반갑...". (unicode)
1. If input is {{ "안녕하세요. The Go Programming Language" | stringTruncatechars 30 }}, the output will be "안녕하세요. The Go Programming L...". (unicode)
1. If input is {{ "The" | stringTruncatechars 30 }}, the output will be "The". (If the length of the given string is shorter than the argument, the output will be the original string.)
1. If input is {{ "The Go Programming Language" | stringTruncatechars 3 }}, the output will be "The". (If the argument is less than or equal to 3, the output will not contain "...".)
1. If input is {{ "The Go" | stringTruncatechars -1 }}, the output will be "The Go". (If the argument is less than 0, the argument will be ignored.)



### stringUrlencode

Escapes the given string for use in a URL.

```
{{{ value | stringUrlencode }}}
```

If value is "http://www.example.org/foo?a=b&c=d", the output will be "http%3A%2F%2Fwww.example.org%2Ffoo%3Fa%3Db%26c%3Dd".



### stringWordcount

Returns the number of words.

```
{{{ value | stringWordcount }}}
```

If value is "The Go Programming Language", the output will be 4.



Expand Down
10 changes: 9 additions & 1 deletion gtf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gtf
import (
"html/template"
"strings"
"net/url"
)

var GtfFuncMap = template.FuncMap {
Expand All @@ -21,7 +22,10 @@ var GtfFuncMap = template.FuncMap {
"stringLower": func(s string) string {
return strings.ToLower(s)
},
"stringTruncateChars": func(n int, s string) string {
"stringUpper": func(s string) string {
return strings.ToUpper(s)
},
"stringTruncatechars": func(n int, s string) string {
if n < 0 {
return s
}
Expand All @@ -39,6 +43,10 @@ var GtfFuncMap = template.FuncMap {

return string(r[:n])
},
"stringUrlencode": url.QueryEscape,
"stringWordcount": func(s string) int {
return len(strings.Fields(s))
},
}

// gtf.New is a wrapper function of template.New(http://golang.org/pkg/text/template/#New).
Expand Down
60 changes: 36 additions & 24 deletions gtf_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package gtf

import (
"testing"
"bytes"
"testing"
)

func AssertEqual(t *testing.T, buffer *bytes.Buffer, testString string) {
Expand All @@ -20,46 +20,58 @@ func ParseTest(buffer *bytes.Buffer, body string) {

func TestGtfFuncMap(t *testing.T) {
var buffer bytes.Buffer

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

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

ParseTest(&buffer, "{{ \"\" | stringDefault \"default value\" }}")
AssertEqual(t, &buffer, "default value")

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

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

ParseTest(&buffer, "{{ \"안녕하세요. 반갑습니다.\" | stringTruncateChars 12 }}")
ParseTest(&buffer, "{{ \"The Go Programming Language\" | stringUpper }}")
AssertEqual(t, &buffer, "THE GO PROGRAMMING LANGUAGE")

ParseTest(&buffer, "{{ \"안녕하세요. 반갑습니다.\" | stringTruncatechars 12 }}")
AssertEqual(t, &buffer, "안녕하세요. 반갑...")
ParseTest(&buffer, "{{ \"The Go Programming Language\" | stringTruncateChars 12 }}")

ParseTest(&buffer, "{{ \"The Go Programming Language\" | stringTruncatechars 12 }}")
AssertEqual(t, &buffer, "The Go Pr...")
ParseTest(&buffer, "{{ \"안녕하세요. The Go Programming Language\" | stringTruncateChars 30 }}")

ParseTest(&buffer, "{{ \"안녕하세요. The Go Programming Language\" | stringTruncatechars 30 }}")
AssertEqual(t, &buffer, "안녕하세요. The Go Programming L...")
ParseTest(&buffer, "{{ \"The\" | stringTruncateChars 30 }}")

ParseTest(&buffer, "{{ \"The\" | stringTruncatechars 30 }}")
AssertEqual(t, &buffer, "The")
ParseTest(&buffer, "{{ \"The Go Programming Language\" | stringTruncateChars 3 }}")

ParseTest(&buffer, "{{ \"The Go Programming Language\" | stringTruncatechars 3 }}")
AssertEqual(t, &buffer, "The")
ParseTest(&buffer, "{{ \"The Go\" | stringTruncateChars 6 }}")

ParseTest(&buffer, "{{ \"The Go\" | stringTruncatechars 6 }}")
AssertEqual(t, &buffer, "The Go")
ParseTest(&buffer, "{{ \"The Go\" | stringTruncateChars 30 }}")

ParseTest(&buffer, "{{ \"The Go\" | stringTruncatechars 30 }}")
AssertEqual(t, &buffer, "The Go")
ParseTest(&buffer, "{{ \"The Go\" | stringTruncateChars 0 }}")

ParseTest(&buffer, "{{ \"The Go\" | stringTruncatechars 0 }}")
AssertEqual(t, &buffer, "")
ParseTest(&buffer, "{{ \"The Go\" | stringTruncateChars -1 }}")

ParseTest(&buffer, "{{ \"The Go\" | stringTruncatechars -1 }}")
AssertEqual(t, &buffer, "The Go")
}

ParseTest(&buffer, "{{ \"http://www.example.org/foo?a=b&c=d\" | stringUrlencode }}")
AssertEqual(t, &buffer, "http%3A%2F%2Fwww.example.org%2Ffoo%3Fa%3Db%26c%3Dd")

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

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

0 comments on commit 3134947

Please sign in to comment.