1
0
Fork 0

feat!: add slug to training

BREAKING CHANGE: update init migration
This commit is contained in:
Vojtěch Mareš 2024-06-26 22:24:36 +02:00
parent 556b4f4e79
commit 2d32c80182
Signed by: vojtech.mares
GPG key ID: C6827B976F17240D
12 changed files with 219 additions and 45 deletions

47
pkg/slug/slug.go Normal file
View 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)
}