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

Support negative values in trunc #196

Merged
merged 2 commits into from
Oct 2, 2019
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
18 changes: 12 additions & 6 deletions docs/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,17 @@ trunc 5 "hello world"

The above produces `hello`.

```
trunc -5 "hello world"
```

The above produces `world`.

## abbrev

Truncate a string with ellipses (`...`)

Parameters:
Parameters:
- max length
- the string

Expand Down Expand Up @@ -284,7 +290,7 @@ len $fish | plural "one anchovy" "many anchovies"
```

In the above, if the length of the string is 1, the first argument will be
printed (`one anchovy`). Otherwise, the second argument will be printed
printed (`one anchovy`). Otherwise, the second argument will be printed
(`many anchovies`).

The arguments are:
Expand Down Expand Up @@ -338,10 +344,10 @@ Swap the case of a string using a word based algorithm.

Conversion algorithm:

- Upper case character converts to Lower case
- Title case character converts to Lower case
- Lower case character after Whitespace or at start converts to Title case
- Other Lower case character converts to Upper case
- Upper case character converts to Lower case
- Title case character converts to Lower case
- Lower case character after Whitespace or at start converts to Title case
- Other Lower case character converts to Upper case
- Whitespace is defined by unicode.IsSpace(char).

```
Expand Down
9 changes: 6 additions & 3 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,13 @@ func strval(v interface{}) string {
}

func trunc(c int, s string) string {
if len(s) <= c {
return s
if c < 0 && len(s)+c > 0 {
return s[len(s)+c:]
}
if c >= 0 && len(s) > c {
return s[:c]
}
return s[0:c]
return s
}

func join(sep string, v interface{}) string {
Expand Down
12 changes: 12 additions & 0 deletions strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ func TestTrunc(t *testing.T) {
if err := runt(tpl, "foo"); err != nil {
t.Error(err)
}
tpl = `{{ "baaaaaar" | trunc -3 }}`
Dean-Coakley marked this conversation as resolved.
Show resolved Hide resolved
if err := runt(tpl, "aar"); err != nil {
t.Error(err)
}
tpl = `{{ "baaaaaar" | trunc -999 }}`
if err := runt(tpl, "baaaaaar"); err != nil {
t.Error(err)
}
tpl = `{{ "baaaaaz" | trunc 0 }}`
if err := runt(tpl, ""); err != nil {
t.Error(err)
}
}

func TestQuote(t *testing.T) {
Expand Down