Skip to content
This repository has been archived by the owner on Nov 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #95 from opennota/master
Browse files Browse the repository at this point in the history
Fix GetLead
  • Loading branch information
recoilme committed Aug 29, 2018
2 parents ef2a136 + 6892067 commit cb4f87b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
14 changes: 11 additions & 3 deletions routers/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"time"
"unicode/utf8"

jwt "github.com/dgrijalva/jwt-go"
humanize "github.com/dustin/go-humanize"
Expand Down Expand Up @@ -167,27 +168,34 @@ func ToStr(value interface{}) string {
if value == nil {
return ""
}
return fmt.Sprintf("%s", value)
return fmt.Sprint(value)
}

// ToDate convert time 2 date
func ToDate(t time.Time) string {
return fmt.Sprintf("%s", humanize.Time(t))
return fmt.Sprint(humanize.Time(t))
}

// GetLead return first paragraph
func GetLead(s string) string {
if len(s) < 300 {
return s + ".."
}
delim := strings.IndexRune(s, '\n')
delim := strings.IndexByte(s, '\n')
if delim > 300 || delim < 0 {
l := len([]rune(s))
if l > 300 {
l = 300
}
delim = strings.LastIndexByte(s[:l], ' ')
if delim < 0 {
for l > 0 {
r, size := utf8.DecodeLastRuneInString(s[:l])
if r != utf8.RuneError {
break
}
l -= size
}
delim = l
}
}
Expand Down
16 changes: 16 additions & 0 deletions routers/routers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package routers

import (
"strings"
"testing"
"unicode/utf8"
)

func TestGetLeadDoesNotSplitRunes(t *testing.T) {
lead := GetLead("@" + strings.Repeat("ф", 300))
for _, r := range lead {
if r == utf8.RuneError {
t.Fatalf("want valid UTF-8, got %q", lead)
}
}
}

0 comments on commit cb4f87b

Please sign in to comment.