Skip to content

Commit

Permalink
change: always apply Substitute map in alphabetic order
Browse files Browse the repository at this point in the history
  • Loading branch information
matrixik committed Jul 6, 2016
1 parent ea39c58 commit 37b1461
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
16 changes: 12 additions & 4 deletions slug.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package slug
import (
"bytes"
"regexp"
"sort"
"strings"

"github.com/rainycape/unidecode"
Expand Down Expand Up @@ -80,17 +81,24 @@ func MakeLang(s string, lang string) (slug string) {
}

// Substitute returns string with superseded all substrings from
// provided substitution map.
// provided substitution map. Substitution map will be applied in alphabetic
// order. Many passes, on one substitution another one could apply.
func Substitute(s string, sub map[string]string) (buf string) {
buf = s
for key, val := range sub {
buf = strings.Replace(buf, key, val, -1)
var keys []string
for k := range sub {
keys = append(keys, k)
}
sort.Strings(keys)

for _, key := range keys {
buf = strings.Replace(buf, key, sub[key], -1)
}
return
}

// SubstituteRune substitutes string chars with provided rune
// substitution map.
// substitution map. One pass.
func SubstituteRune(s string, sub map[rune]string) string {
var buf bytes.Buffer
for _, c := range s {
Expand Down
8 changes: 5 additions & 3 deletions slug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ func TestSlugMakeUserSubstituteLang(t *testing.T) {
want string
}{
{map[string]string{"'": " "}, "en", "That's great", "that-s-great"},
{map[string]string{"&": "or"}, "en", "This & that", "this-or-that"}, // by default "&" => "and"
{map[string]string{"&": "or"}, "de", "This & that", "this-or-that"}, // by default "&" => "und"
{map[string]string{"&": "or"}, "en", "This & that", "this-or-that"}, // by default "&" => "and"
{map[string]string{"&": "or"}, "de", "This & that", "this-or-that"}, // by default "&" => "und"
{map[string]string{"&": "or", "@": "the"}, "de", "@ This & that", "the-this-or-that"}, // by default "&" => "und", "@" => "an"
}

Expand Down Expand Up @@ -143,7 +143,8 @@ func TestSubstituteLang(t *testing.T) {
want string
}{
{map[string]string{"o": "no"}, "o o o", "no no no"},
{map[string]string{"o": "no", "a": "or"}, "o a o", "no or no"},
{map[string]string{"o": "no", "a": "or"}, "o a o", "no nor no"},
{map[string]string{"a": "or", "o": "no"}, "o a o", "no nor no"},
{map[string]string{"'": " "}, "That's great", "That s great"},
}

Expand All @@ -165,6 +166,7 @@ func TestSubstituteRuneLang(t *testing.T) {
}{
{map[rune]string{'o': "no"}, "o o o", "no no no"},
{map[rune]string{'o': "no", 'a': "or"}, "o a o", "no or no"},
{map[rune]string{'a': "or", 'o': "no"}, "o a o", "no or no"},
{map[rune]string{'\'': " "}, "That's great", "That s great"},
}

Expand Down

0 comments on commit 37b1461

Please sign in to comment.