-
Notifications
You must be signed in to change notification settings - Fork 0
/
slug.go
54 lines (42 loc) · 1.03 KB
/
slug.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package slug
import (
"regexp"
"strings"
"github.com/goloop/slug/lang"
"github.com/goloop/t13n"
)
var (
// Characters that are not allowed in the slug.
wrongRegx = regexp.MustCompile(`([^A-Za-z0-9\-]|-$|^-)`)
// Duplicate separator.
dusepRegx = regexp.MustCompile(`-[-]+`)
)
// New retursn pointer to Slug.
func New() *Slug {
return &Slug{lang: lang.None}
}
// Slug is the slug constructor.
type Slug struct {
lang string
}
// Lang sets the type of language features to use during slugify.
func (s *Slug) Lang(l string) *Slug {
s.lang = l
return s
}
// Make returns slug from string.
func (s *Slug) Make(t string) string {
var result string
result = t13n.Render(s.lang, t, slugRules)
result = dusepRegx.ReplaceAllString(result, "-")
result = wrongRegx.ReplaceAllString(result, "")
return result
}
// Lower returns slug in lowercase.
func (s *Slug) Lower(t string) string {
return strings.ToLower(s.Make(t))
}
// Upper returns slug in uppercase.
func (s *Slug) Upper(t string) string {
return strings.ToUpper(s.Make(t))
}