feat!: add slug to training
BREAKING CHANGE: update init migration
This commit is contained in:
parent
556b4f4e79
commit
2d32c80182
12 changed files with 219 additions and 45 deletions
47
pkg/slug/slug.go
Normal file
47
pkg/slug/slug.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
package slug
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gosimple/unidecode"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Slug string
|
||||
|
||||
var (
|
||||
ErrInvalidSlug = errors.New("invalid slug")
|
||||
|
||||
regexpNonAuthorizedChars = regexp.MustCompile("[^a-zA-Z0-9-_]")
|
||||
regexpMultipleDashes = regexp.MustCompile("-+")
|
||||
)
|
||||
|
||||
func New(s string) Slug {
|
||||
s = unidecode.Unidecode(s)
|
||||
s = strings.ToLower(s)
|
||||
s = regexpNonAuthorizedChars.ReplaceAllString(s, "-")
|
||||
s = regexpMultipleDashes.ReplaceAllString(s, "-")
|
||||
s = strings.Trim(s, "-_")
|
||||
|
||||
return Slug(s)
|
||||
}
|
||||
|
||||
func NewString(s string) string {
|
||||
return New(s).String()
|
||||
}
|
||||
|
||||
func Validate(s string) error {
|
||||
if s == "" {
|
||||
return ErrInvalidSlug
|
||||
}
|
||||
|
||||
if s == NewString(s) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return ErrInvalidSlug
|
||||
}
|
||||
|
||||
func (s Slug) String() string {
|
||||
return string(s)
|
||||
}
|
||||
59
pkg/slug/slug_test.go
Normal file
59
pkg/slug/slug_test.go
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
package slug
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
t.Run("Test New", func(t *testing.T) {
|
||||
s := New("Hello World")
|
||||
assert.Equal(t, "hello-world", s.String())
|
||||
})
|
||||
|
||||
t.Run("Test New with !", func(t *testing.T) {
|
||||
s := New("Hello World!")
|
||||
assert.Equal(t, "hello-world", s.String())
|
||||
})
|
||||
|
||||
t.Run("Test New with unicode", func(t *testing.T) {
|
||||
s := New("Ahoj Světe!") // hello world in Czech
|
||||
assert.Equal(t, "ahoj-svete", s.String())
|
||||
})
|
||||
}
|
||||
|
||||
func TestValidate(t *testing.T) {
|
||||
t.Run("Test Validate", func(t *testing.T) {
|
||||
assert.NoError(t, Validate("hello-world"))
|
||||
})
|
||||
|
||||
t.Run("Test Validate with !", func(t *testing.T) {
|
||||
assert.ErrorIs(t, Validate("hello-world!"), ErrInvalidSlug)
|
||||
})
|
||||
|
||||
t.Run("Test Validate with unicode", func(t *testing.T) {
|
||||
assert.ErrorIs(t, Validate("ahoj-světe"), ErrInvalidSlug)
|
||||
})
|
||||
|
||||
t.Run("Test Validate with empty string", func(t *testing.T) {
|
||||
assert.ErrorIs(t, Validate(""), ErrInvalidSlug)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSlug_String(t *testing.T) {
|
||||
t.Run("Test String", func(t *testing.T) {
|
||||
s := Slug("hello-world")
|
||||
assert.Equal(t, "hello-world", s.String())
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewString(t *testing.T) {
|
||||
t.Run("Test NewString", func(t *testing.T) {
|
||||
input := "Hello World"
|
||||
expected := "hello-world"
|
||||
stringSlug := NewString(input)
|
||||
|
||||
assert.Equal(t, expected, stringSlug)
|
||||
assert.IsType(t, string(""), stringSlug)
|
||||
})
|
||||
}
|
||||
Reference in a new issue