Skip to content

Commit

Permalink
Merge pull request #1 from mozillazg/develop
Browse files Browse the repository at this point in the history
v0.2.0
  • Loading branch information
mozillazg authored Aug 13, 2016
2 parents 35a4f89 + 5646157 commit 591116d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# Changelog


## 0.2.0 (2016-08-14)

**New**

* export `SeparatorForRe`
* export `ReInValidChar`
* export `ReDupSeparatorChar`


## 0.1.0 (2016-07-16)

* Initial Release
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ lint:
gofmt -s -w . slugify
golint .
golint slugify
go vet
20 changes: 12 additions & 8 deletions slugify.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ import (

// Separator separator between words
var Separator = "-"
var separatorForRe = regexp.QuoteMeta(Separator)

// ReInValidChar match invalid slug character
var ReInValidChar = regexp.MustCompile(fmt.Sprintf("[^%sa-zA-Z0-9]", separatorForRe))
var reDupSeparatorChar = regexp.MustCompile(fmt.Sprintf("%s{2,}", separatorForRe))
// SeparatorForRe for regexp
var SeparatorForRe = regexp.QuoteMeta(Separator)

// ReInValidChar match invalid slug string
var ReInValidChar = regexp.MustCompile(fmt.Sprintf("[^%sa-zA-Z0-9]", SeparatorForRe))

// ReDupSeparatorChar match duplicate separator string
var ReDupSeparatorChar = regexp.MustCompile(fmt.Sprintf("%s{2,}", SeparatorForRe))

// Version return version
func Version() string {
return "0.1.0"
return "0.2.0"
}

// Slugify implements make a pretty slug from the given text.
Expand All @@ -30,7 +34,7 @@ func Slugify(s string) string {
func slugify(s string) string {
s = unidecode.Unidecode(s)
s = replaceInValidCharacter(s, Separator)
s = removeDumpSeparator(s)
s = removeDupSeparator(s)
s = strings.Trim(s, Separator)
s = strings.ToLower(s)
return s
Expand All @@ -41,7 +45,7 @@ func replaceInValidCharacter(s, repl string) string {
return s
}

func removeDumpSeparator(s string) string {
s = reDupSeparatorChar.ReplaceAllString(s, Separator)
func removeDupSeparator(s string) string {
s = ReDupSeparatorChar.ReplaceAllString(s, Separator)
return s
}
6 changes: 4 additions & 2 deletions slugify_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package slugify

import "testing"
import "log"

type testCase struct {
input, expect string
Expand All @@ -13,13 +14,13 @@ func testSlugify(t *testing.T, input, expect string) {

func check(t *testing.T, ret, expect string) {
if ret != expect {
t.Errorf("Expected %s, got %s", expect, ret)
t.Errorf("Expected %v, got %v", expect, ret)
}
}

func TestVersion(t *testing.T) {
ret := Version()
expect := "0.1.0"
expect := "0.2.0"
check(t, ret, expect)
}

Expand All @@ -36,6 +37,7 @@ func TestSlugify(t *testing.T) {
{`C\'est déjà l\'été.`, "c-est-deja-l-ete"},
}
for _, c := range cases {
log.Printf("input %v, expect %v", c.input, c.expect)
testSlugify(t, c.input, c.expect)
}
}

0 comments on commit 591116d

Please sign in to comment.