Merge pull request #10 from donny-dont/negative-trunkate
Added negative truncation to helper function
This commit is contained in:
commit
c67388feab
|
@ -17,6 +17,7 @@ package template
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"math"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -100,12 +101,17 @@ func isFailure(conditional bool, options *raymond.Options) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func truncate(s string, len int) string {
|
func truncate(s string, len int) string {
|
||||||
if utf8.RuneCountInString(s) <= len {
|
if utf8.RuneCountInString(s) <= int(math.Abs(float64(len))) {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
runes := []rune(s)
|
runes := []rune(s)
|
||||||
|
|
||||||
|
if len < 0 {
|
||||||
|
len = -len
|
||||||
|
return string(runes[len:])
|
||||||
|
}
|
||||||
|
|
||||||
return string(runes[:len])
|
return string(runes[:len])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -53,6 +53,22 @@ func TestTruncate(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNegativeTruncate(t *testing.T) {
|
||||||
|
vals := map[string]string{
|
||||||
|
"foobarz": "rz",
|
||||||
|
"foöäüüu": "üu",
|
||||||
|
"üpsßßßk": "ßk",
|
||||||
|
"1234567": "67",
|
||||||
|
"!'§$%&/": "&/",
|
||||||
|
}
|
||||||
|
|
||||||
|
for input, want := range vals {
|
||||||
|
if got := truncate(input, -5); got != want {
|
||||||
|
t.Errorf("Want transform %s to %s, got %s", input, want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSince(t *testing.T) {
|
func TestSince(t *testing.T) {
|
||||||
t.Skip()
|
t.Skip()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue