Skip to content

Commit

Permalink
Merge pull request #339 from hairyhenderson/add-slug-336
Browse files Browse the repository at this point in the history
Add strings.Slug function
  • Loading branch information
hairyhenderson authored May 25, 2018
2 parents 25d820c + 66ef678 commit 8c1aa36
Show file tree
Hide file tree
Showing 14 changed files with 1,081 additions and 1 deletion.
14 changes: 13 additions & 1 deletion Gopkg.lock

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

21 changes: 21 additions & 0 deletions docs/content/functions/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,27 @@ $ gomplate -i '{{ "172.21.1.42" | strings.ReplaceAll "." "-" }}'
172-21-1-42
```

## `strings.Slug`

Creates a a "slug" from a given string - supports unicode correctly. This wraps the [github.com/gosimple/slug](https://github.com/gosimple/slug) package. See [the github.com/gosimple/slug docs](https://godoc.org/github.com/gosimple/slug) for more information.

### Usage
```go
strings.Slug input
```
```go
input | strings.Slug
```

### Examples
```console
$ gomplate -i '{{ "Hello, world!" | strings.Slug }}'
hello-world

$ echo 'Rock & Roll @ Cafe Wha?' | gomplate -d in=stdin: -i '{{ strings.Slug (include "in") }}'
rock-and-roll-at-cafe-wha
```

## `strings.Title`

**Alias:** `title`
Expand Down
6 changes: 6 additions & 0 deletions funcs/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"strings"

"github.com/gosimple/slug"
gompstrings "github.com/hairyhenderson/gomplate/strings"
)

Expand Down Expand Up @@ -185,3 +186,8 @@ func (f *StringFuncs) Indent(args ...interface{}) string {
}
return gompstrings.Indent(width, indent, input)
}

// Slug -
func (f *StringFuncs) Slug(in interface{}) string {
return slug.Make(conv.ToString(in))
}
27 changes: 27 additions & 0 deletions funcs/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,30 @@ func TestAbbrev(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "...baz...", s)
}

func TestSlug(t *testing.T) {
sf := &StringFuncs{}
s := sf.Slug(nil)
assert.Equal(t, "nil", s)

s = sf.Slug(0)
assert.Equal(t, "0", s)

s = sf.Slug(1.85e-5)
assert.Equal(t, "1-85e-05", s)

s = sf.Slug("Hello, World!")
assert.Equal(t, "hello-world", s)

s = sf.Slug("foo@example.com")
assert.Equal(t, "fooatexample-com", s)

s = sf.Slug("rock & roll!")
assert.Equal(t, "rock-and-roll", s)

s = sf.Slug("foo@example.com")
assert.Equal(t, "fooatexample-com", s)

s = sf.Slug(`100%`)
assert.Equal(t, "100", s)
}
6 changes: 6 additions & 0 deletions test/integration/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ func (s *StringsSuite) TestRepeat(c *C) {
`ba{{ strings.Repeat -1 "na" }}`)
result.Assert(c, icmd.Expected{ExitCode: 1, Out: `negative count`})
}

func (s *StringsSuite) TestSlug(c *C) {
result := icmd.RunCommand(GomplateBin, "-i",
`{{ strings.Slug "Hellö, Wôrld! Free @ last..." }}`)
result.Assert(c, icmd.Expected{ExitCode: 0, Out: `hello-world-free-at-last`})
}
Loading

0 comments on commit 8c1aa36

Please sign in to comment.