Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding strings.Trunc and strings.Abbrev #323

Merged
merged 1 commit into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions docs/content/functions/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,37 @@ menu:
parent: functions
---

## `strings.Abbrev`

Abbreviates a string using `...` (ellipses). Takes an optional offset from the beginning of the string, and a maximum final width (including added ellipses).

_Also see [`strings.Trunc`](#strings-trunc)._

### Usage
```go
strings.Abbrev [offset] width input
```
```go
input | strings.Abbrev [offset] width
```

### Arguments

| name | description |
|--------|-------|
| `offset` | _(optional)_ offset from the start of the string. Must be `4` or greater for ellipses to be added. Defaults to `0` |
| `width` | _(required)_ the desired maximum final width of the string, including ellipses |
| `input` | _(required)_ the input string to abbreviate |

### Example

```console
$ gomplate -i '{{ "foobarbazquxquux" | strings.Abbrev 9 }}'
foobar...
$ gomplate -i '{{ "foobarbazquxquux" | strings.Abbrev 6 9 }}'
...baz...
```

## `strings.Contains`

Reports whether a substring is contained within a string.
Expand Down Expand Up @@ -358,6 +389,27 @@ $ gomplate -i '{{ "hello, world" | strings.TrimSuffix "world" }}jello'
hello, jello
```

## `strings.Trunc`

Returns a string truncated to the given length.

_Also see [`strings.Abbrev`](#strings-abbrev)._

### Usage
```go
strings.Trunc length input
```
```go
input | strings.Trunc length
```

#### Example

```console
$ gomplate -i '{{ "hello, world" | strings.Trunc 5 }}'
hello
```

## `contains`

**See [`strings.Contains](#strings-contains) for a pipeline-compatible version**
Expand Down
29 changes: 29 additions & 0 deletions funcs/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"sync"

"github.com/Masterminds/goutils"
"github.com/hairyhenderson/gomplate/conv"
"github.com/pkg/errors"

Expand Down Expand Up @@ -51,6 +52,29 @@ func AddStringFuncs(f map[string]interface{}) {
// StringFuncs -
type StringFuncs struct{}

// Abbrev -
func (f *StringFuncs) Abbrev(args ...interface{}) (string, error) {
str := ""
offset := 0
maxWidth := 0
if len(args) < 2 {
return "", errors.Errorf("abbrev requires a 'maxWidth' and 'input' argument")
}
if len(args) == 2 {
maxWidth = conv.ToInt(args[0])
str = conv.ToString(args[1])
}
if len(args) == 3 {
offset = conv.ToInt(args[0])
maxWidth = conv.ToInt(args[1])
str = conv.ToString(args[2])
}
if len(str) <= maxWidth {
return str, nil
}
return goutils.AbbreviateFull(str, offset, maxWidth)
}

// ReplaceAll -
func (f *StringFuncs) ReplaceAll(old, new string, s interface{}) string {
return strings.Replace(conv.ToString(s), old, new, -1)
Expand Down Expand Up @@ -128,6 +152,11 @@ func (f *StringFuncs) TrimSpace(s interface{}) string {
return strings.TrimSpace(conv.ToString(s))
}

// Trunc -
func (f *StringFuncs) Trunc(length int, s interface{}) string {
return gompstrings.Trunc(length, conv.ToString(s))
}

// Indent -
func (f *StringFuncs) Indent(args ...interface{}) string {
input := conv.ToString(args[len(args)-1])
Expand Down
29 changes: 29 additions & 0 deletions funcs/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,32 @@ func TestTrimPrefix(t *testing.T) {
assert.Equal(t, "Bar",
sf.TrimPrefix("Foo", "FooBar"))
}

func TestTrunc(t *testing.T) {
sf := &StringFuncs{}
assert.Equal(t, "", sf.Trunc(5, ""))
assert.Equal(t, "", sf.Trunc(0, nil))
assert.Equal(t, "123", sf.Trunc(3, 123456789))
assert.Equal(t, "hello, world", sf.Trunc(-1, "hello, world"))
}

func TestAbbrev(t *testing.T) {
sf := &StringFuncs{}
_, err := sf.Abbrev()
assert.Error(t, err)

_, err = sf.Abbrev("foo")
assert.Error(t, err)

s, err := sf.Abbrev(3, "foo")
assert.NoError(t, err)
assert.Equal(t, "foo", s)

s, err = sf.Abbrev(2, 6, "foobar")
assert.NoError(t, err)
assert.Equal(t, "foobar", s)

s, err = sf.Abbrev(6, 9, "foobarbazquxquux")
assert.NoError(t, err)
assert.Equal(t, "...baz...", s)
}
11 changes: 11 additions & 0 deletions strings/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ func Indent(width int, indent, s string) string {
}
return string(res)
}

// Trunc - truncate a string to the given length
func Trunc(length int, s string) string {
if length < 0 {
return s
}
if len(s) <= length {
return s
}
return s[0:length]
}
9 changes: 9 additions & 0 deletions strings/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ func TestIndent(t *testing.T) {
assert.Equal(t, " foo", Indent(1, " ", "foo"))
assert.Equal(t, " foo", Indent(3, " ", "foo"))
}

func TestTrunc(t *testing.T) {
assert.Equal(t, "", Trunc(5, ""))
assert.Equal(t, "", Trunc(0, "hello, world"))
assert.Equal(t, "hello", Trunc(5, "hello, world"))
assert.Equal(t, "hello, world", Trunc(12, "hello, world"))
assert.Equal(t, "hello, world", Trunc(42, "hello, world"))
assert.Equal(t, "hello, world", Trunc(-1, "hello, world"))
}
Loading