diff --git a/template/helpers.go b/template/helpers.go index 3c7a371..b4085b4 100644 --- a/template/helpers.go +++ b/template/helpers.go @@ -17,6 +17,7 @@ package template import ( "fmt" "net/url" + "math" "regexp" "strings" "time" @@ -100,12 +101,17 @@ func isFailure(conditional bool, options *raymond.Options) string { } func truncate(s string, len int) string { - if utf8.RuneCountInString(s) <= len { + if utf8.RuneCountInString(s) <= int(math.Abs(float64(len))) { return s } runes := []rune(s) + if len < 0 { + len = -len + return string(runes[len:]) + } + return string(runes[:len]) } diff --git a/template/helpers_test.go b/template/helpers_test.go index 96d6e6e..b93339d 100644 --- a/template/helpers_test.go +++ b/template/helpers_test.go @@ -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) { t.Skip() }