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)
|
||||
}
|
||||
Reference in a new issue