Skip to content

Commit

Permalink
Support uppercase characters in slugs (gosimple#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kugelschieber authored and matrixik committed Aug 16, 2019
1 parent ec8fb67 commit ac1ea09
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 36 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func main() {

deText := slug.MakeLang("Diese & Dass", "de")
fmt.Println(deText) // Will print: "diese-und-dass"

slug.Lowercase = false // keep uppercase characters
deUppercaseText := slug.MakeLang("Diese & Dass", "de")
fmt.Println(deUppercaseText) // Will print: "Diese-und-Dass"

slug.CustomSub = map[string]string{
"water": "sand",
Expand Down
18 changes: 9 additions & 9 deletions languages_substitution.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ var deSub = map[rune]string{
'&': "und",
'@': "an",
'ä': "ae",
'Ä': "ae",
'Ä': "Ae",
'ö': "oe",
'Ö': "oe",
'Ö': "Oe",
'ü': "ue",
'Ü': "ue",
'Ü': "Ue",
}

var enSub = map[rune]string{
Expand Down Expand Up @@ -83,15 +83,15 @@ var trSub = map[rune]string{
'&': "ve",
'@': "et",
'ş': "s",
'Ş': "s",
'Ş': "S",
'ü': "u",
'Ü': "u",
'Ü': "U",
'ö': "o",
'Ö': "o",
'İ': "i",
'Ö': "O",
'İ': "I",
'ı': "i",
'ğ': "g",
'Ğ': "g",
'Ğ': "G",
'ç': "c",
'Ç': "c",
'Ç': "C",
}
10 changes: 8 additions & 2 deletions slug.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ var (
// after MaxLength.
MaxLength int

regexpNonAuthorizedChars = regexp.MustCompile("[^a-z0-9-_]")
// Lowercase defines if the resulting slug is transformed to lowercase.
// Default is true.
Lowercase = true

regexpNonAuthorizedChars = regexp.MustCompile("[^a-zA-Z0-9-_]")
regexpMultipleDashes = regexp.MustCompile("-+")
)

Expand Down Expand Up @@ -76,7 +80,9 @@ func MakeLang(s string, lang string) (slug string) {
// Process all non ASCII symbols
slug = unidecode.Unidecode(slug)

slug = strings.ToLower(slug)
if Lowercase {
slug = strings.ToLower(slug)
}

// Process all remaining symbols
slug = regexpNonAuthorizedChars.ReplaceAllString(slug, "-")
Expand Down
58 changes: 33 additions & 25 deletions slug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,38 +66,46 @@ func TestSlugMakeLang(t *testing.T) {
lang string
in string
want string
lowercase bool
}{
{"de", "Wir mögen Bücher & Käse", "wir-moegen-buecher-und-kaese"},
{"de", "Äpfel Über Österreich", "aepfel-ueber-oesterreich"},
{"en", "äÄäöÖöüÜü", "aaaooouuu"},
{"gr", "ϊχώΩϋ", "ixwwu"},
{"Ell", "ϊχώΩϋ", "ixwwu"},
{"tr", "şüöğıçŞÜÖİĞÇ", "suogicsuoigc"},
{"de", "Wir mögen Bücher & Käse", "wir-moegen-buecher-und-kaese", true},
{"de", "Wir mögen Bücher & Käse", "Wir-moegen-Buecher-und-Kaese", false},
{"de", "Äpfel Über Österreich", "aepfel-ueber-oesterreich", true},
{"de", "Äpfel Über Österreich", "Aepfel-Ueber-Oesterreich", false},
{"en", "äÄäöÖöüÜü", "aaaooouuu", true},
{"en", "äÄäöÖöüÜü", "aAaoOouUu", false},
{"gr", "ϊχώΩϋ", "ixwwu", true},
{"gr", "ϊχώΩϋ", "ixwwu", false},
{"Ell", "ϊχώΩϋ", "ixwwu", true},
{"Ell", "ϊχώΩϋ", "ixwwu", false},
{"tr", "şüöğıçŞÜÖİĞÇ", "suogicsuoigc", true},
{"tr", "şüöğıçŞÜÖİĞÇ", "suogicSUOIGC", false},
// & fun.
{"de", "This & that", "this-und-that"},
{"en", "This & that", "this-and-that"},
{"es", "This & that", "this-y-that"},
{"fi", "This & that", "this-ja-that"},
{"gr", "This & that", "this-kai-that"},
{"ell", "This & that", "this-kai-that"},
{"Ell", "This & that", "this-kai-that"},
{"nl", "This & that", "this-en-that"},
{"pl", "This & that", "this-i-that"},
{"pol", "This & that", "this-i-that"},
{"tr", "This & that", "this-ve-that"},
{"test", "This & that", "this-and-that"}, // unknown lang, fallback to "en"
{"de", "This & that", "this-und-that", true},
{"en", "This & that", "this-and-that", true},
{"es", "This & that", "this-y-that", true},
{"fi", "This & that", "this-ja-that", true},
{"gr", "This & that", "this-kai-that", true},
{"ell", "This & that", "this-kai-that", true},
{"Ell", "This & that", "this-kai-that", true},
{"nl", "This & that", "this-en-that", true},
{"pl", "This & that", "this-i-that", true},
{"pol", "This & that", "this-i-that", true},
{"tr", "This & that", "this-ve-that", true},
{"test", "This & that", "this-and-that", true}, // unknown lang, fallback to "en"
// Test defaultSub, when adding new lang copy/paste this line,
// it contain special characters.
{"de", "1\"2'3’4‒5–6—7―8", "1234-5-6-7-8"},
{"en", "1\"2'3’4‒5–6—7―8", "1234-5-6-7-8"},
{"es", "1\"2'3’4‒5–6—7―8", "1234-5-6-7-8"},
{"fi", "1\"2'3’4‒5–6—7―8", "1234-5-6-7-8"},
{"gr", "1\"2'3’4‒5–6—7―8", "1234-5-6-7-8"},
{"nl", "1\"2'3’4‒5–6—7―8", "1234-5-6-7-8"},
{"pl", "1\"2'3’4‒5–6—7―8", "1234-5-6-7-8"},
{"de", "1\"2'3’4‒5–6—7―8", "1234-5-6-7-8", true},
{"en", "1\"2'3’4‒5–6—7―8", "1234-5-6-7-8", true},
{"es", "1\"2'3’4‒5–6—7―8", "1234-5-6-7-8", true},
{"fi", "1\"2'3’4‒5–6—7―8", "1234-5-6-7-8", true},
{"gr", "1\"2'3’4‒5–6—7―8", "1234-5-6-7-8", true},
{"nl", "1\"2'3’4‒5–6—7―8", "1234-5-6-7-8", true},
{"pl", "1\"2'3’4‒5–6—7―8", "1234-5-6-7-8", true},
}

for index, smlt := range testCases {
Lowercase = smlt.lowercase
got := MakeLang(smlt.in, smlt.lang)
if got != smlt.want {
t.Errorf(
Expand Down

0 comments on commit ac1ea09

Please sign in to comment.