Skip to content

Commit

Permalink
fix: broken Substitute
Browse files Browse the repository at this point in the history
Substitution did not work if substitution map had more than one element.

fix #6
  • Loading branch information
matrixik committed Sep 21, 2015
1 parent fb135f0 commit ea39c58
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion slug.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func MakeLang(s string, lang string) (slug string) {
func Substitute(s string, sub map[string]string) (buf string) {
buf = s
for key, val := range sub {
buf = strings.Replace(s, key, val, -1)
buf = strings.Replace(buf, key, val, -1)
}
return
}
Expand Down
5 changes: 5 additions & 0 deletions slug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func TestSlugMakeUserSubstituteLang(t *testing.T) {
{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", "@": "the"}, "de", "@ This & that", "the-this-or-that"}, // by default "&" => "und", "@" => "an"
}

for index, smust := range testCases {
Expand All @@ -116,6 +117,8 @@ func TestSlugMakeSubstituteOrderLang(t *testing.T) {
want string
}{
{map[rune]string{'o': "left"}, map[string]string{"o": "right"}, "o o", "left-left"},
{map[rune]string{'o': "left", 'a': "r"}, map[string]string{"o": "right"}, "o a o", "left-r-left"},
{map[rune]string{'o': "left"}, map[string]string{"o": "right", "a": "r"}, "a o a o", "r-left-r-left"},
{map[rune]string{'&': "down"}, map[string]string{"&": "up"}, "&", "down"},
}

Expand All @@ -140,6 +143,7 @@ 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{"'": " "}, "That's great", "That s great"},
}

Expand All @@ -160,6 +164,7 @@ func TestSubstituteRuneLang(t *testing.T) {
want string
}{
{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{'\'': " "}, "That's great", "That s great"},
}

Expand Down

0 comments on commit ea39c58

Please sign in to comment.