Merge pull request #10 from donny-dont/negative-trunkate

Added negative truncation to helper function
This commit is contained in:
Brad Rydzewski 2020-09-22 13:00:49 -04:00 committed by GitHub
commit c67388feab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -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])
}

View File

@ -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()
}