mirror of
https://github.com/thegeeklab/drone-template-lib.git
synced 2024-11-16 09:50:39 +00:00
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 (
|
||||
"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])
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user