summaryrefslogtreecommitdiffstats
path: root/tpl
diff options
context:
space:
mode:
authorDavid E. Wheeler <david@justatheory.com>2018-06-04 13:47:03 -0400
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2018-06-04 20:47:03 +0300
commit019bd5576be87c9f06b6a928ede1a5e78677f7b3 (patch)
tree2eba3a897b200a127ccd5e2e952551e03a9c5c89 /tpl
parentc3115292a7f2d2623cb45054a361e997ad9330c9 (diff)
tpl/strings: strings.RuneCount
Diffstat (limited to 'tpl')
-rw-r--r--tpl/strings/init.go5
-rw-r--r--tpl/strings/strings.go9
-rw-r--r--tpl/strings/strings_test.go27
3 files changed, 41 insertions, 0 deletions
diff --git a/tpl/strings/init.go b/tpl/strings/init.go
index 883ce76d1..8ff4cf98f 100644
--- a/tpl/strings/init.go
+++ b/tpl/strings/init.go
@@ -41,6 +41,11 @@ func init() {
[][2]string{},
)
+ ns.AddMethodMapping(ctx.RuneCount,
+ nil,
+ [][2]string{},
+ )
+
ns.AddMethodMapping(ctx.CountWords,
[]string{"countwords"},
[][2]string{},
diff --git a/tpl/strings/strings.go b/tpl/strings/strings.go
index d7d8f2d85..1864bb9e0 100644
--- a/tpl/strings/strings.go
+++ b/tpl/strings/strings.go
@@ -57,6 +57,15 @@ func (ns *Namespace) CountRunes(s interface{}) (int, error) {
return counter, nil
}
+// RuneCount returns the number of runes in s.
+func (ns *Namespace) RuneCount(s interface{}) (int, error) {
+ ss, err := cast.ToStringE(s)
+ if err != nil {
+ return 0, fmt.Errorf("Failed to convert content to string: %s", err)
+ }
+ return utf8.RuneCountInString(ss), nil
+}
+
// CountWords returns the approximate word count in s.
func (ns *Namespace) CountWords(s interface{}) (int, error) {
ss, err := cast.ToStringE(s)
diff --git a/tpl/strings/strings_test.go b/tpl/strings/strings_test.go
index 69863c30d..22695ba08 100644
--- a/tpl/strings/strings_test.go
+++ b/tpl/strings/strings_test.go
@@ -173,6 +173,33 @@ func TestCountRunes(t *testing.T) {
}
}
+func TestRuneCount(t *testing.T) {
+ t.Parallel()
+
+ for i, test := range []struct {
+ s interface{}
+ expect interface{}
+ }{
+ {"foo bar", 7},
+ {"旁边", 2},
+ {`<div class="test">旁边</div>`, 26},
+ // errors
+ {tstNoStringer{}, false},
+ } {
+ errMsg := fmt.Sprintf("[%d] %v", i, test.s)
+
+ result, err := ns.RuneCount(test.s)
+
+ if b, ok := test.expect.(bool); ok && !b {
+ require.Error(t, err, errMsg)
+ continue
+ }
+
+ require.NoError(t, err, errMsg)
+ assert.Equal(t, test.expect, result, errMsg)
+ }
+}
+
func TestCountWords(t *testing.T) {
t.Parallel()