Skip to content

Commit

Permalink
Add a new slug modifier (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkan committed May 21, 2023
1 parent 1d5da9d commit 46fc5ee
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ These functions modify the data in-place.
| ltrim | Trims spaces from the left of the data provided in the params. |
| rtrim | Trims spaces from the right of the data provided in the params. |
| set | Set the provided value. |
| slug | Converts the field to a [slug](https://github.com/gosimple/slug) |
| snake | Snake Cases the data. |
| strip_alpha | Strips all ascii characters from the data. |
| strip_alpha_unicode | Strips all unicode characters from the data. |
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ require (

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/gosimple/slug v1.13.1 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/gosimple/slug v1.13.1 h1:bQ+kpX9Qa6tHRaK+fZR0A0M2Kd7Pa5eHPPsb1JpHD+Q=
github.com/gosimple/slug v1.13.1/go.mod h1:UiRaFH+GEilHstLUmcBgWcI42viBN7mAb818JrYOeFQ=
github.com/gosimple/unidecode v1.0.1 h1:hZzFTMMqSswvf0LBJZCZgThIZrpDHFXux9KeGmn6T/o=
github.com/gosimple/unidecode v1.0.1/go.mod h1:CP0Cr1Y1kogOtx0bJblKzsVWrqYaqfNOnHzpgWw4Awc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/segmentio/go-camelcase v0.0.0-20160726192923-7085f1e3c734 h1:Cpx2WLIv6fuPvaJAHNhYOgYzk/8RcJXu/8+mOrxf2KM=
Expand Down
1 change: 1 addition & 0 deletions modifiers/modifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func New() *mold.Transformer {
mod.Register("rtrim", trimRight)
mod.Register("set", setValue)
mod.Register("snake", snakeCase)
mod.Register("slug", slugCase)
mod.Register("strip_alpha_unicode", stripAlphaUnicodeCase)
mod.Register("strip_alpha", stripAlphaCase)
mod.Register("strip_num_unicode", stripNumUnicodeCase)
Expand Down
10 changes: 10 additions & 0 deletions modifiers/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"golang.org/x/text/language"

"github.com/go-playground/mold/v4"
"github.com/gosimple/slug"
"github.com/segmentio/go-camelcase"
"github.com/segmentio/go-snakecase"
)
Expand Down Expand Up @@ -89,6 +90,15 @@ func snakeCase(ctx context.Context, fl mold.FieldLevel) error {
return nil
}

// slug converts string to a slug
func slugCase(ctx context.Context, fl mold.FieldLevel) error {
switch fl.Field().Kind() {
case reflect.String:
fl.Field().SetString(slug.Make(fl.Field().String()))
}
return nil
}

// titleCase converts string to title case, e.g. "this is a sentence" -> "This Is A Sentence"
func titleCase(ctx context.Context, fl mold.FieldLevel) error {
switch fl.Field().Kind() {
Expand Down
46 changes: 46 additions & 0 deletions modifiers/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,52 @@ func TestTitleCase(t *testing.T) {
}
}

func TestSlugCase(t *testing.T) {
conform := New()

s := "this-is +a SentencE9"
expected := "this-is-a-sentence9"

type Test struct {
String string `mod:"slug"`
}

tt := Test{String: s}
err := conform.Struct(context.Background(), &tt)
if err != nil {
log.Fatal(err)
}
if tt.String != expected {
t.Fatalf("Unexpected value '%s'\n", tt.String)
}

err = conform.Field(context.Background(), &s, "slug")
if err != nil {
log.Fatal(err)
}
if s != expected {
t.Fatalf("Unexpected value '%s'\n", s)
}

var iface interface{}
err = conform.Field(context.Background(), &iface, "slug")
if err != nil {
log.Fatal(err)
}
if iface != nil {
t.Fatalf("Unexpected value '%v'\n", nil)
}

iface = s
err = conform.Field(context.Background(), &iface, "slug")
if err != nil {
log.Fatal(err)
}
if iface != expected {
t.Fatalf("Unexpected value '%v'\n", iface)
}
}

func TestNameCase(t *testing.T) {
conform := New()

Expand Down

0 comments on commit 46fc5ee

Please sign in to comment.