Add safe slug generation func in package id
This commit is contained in:
@@ -0,0 +1,12 @@
|
|||||||
|
package id
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/writeas/nerds/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GenSafeUniqueSlug generatees a reasonably unique random slug from the given
|
||||||
|
// original slug. It's "safe" because it uses 0-9 b-z excluding vowels.
|
||||||
|
func GenSafeUniqueSlug(slug string) string {
|
||||||
|
return fmt.Sprintf("%s-%s", slug, store.GenerateRandomString("0123456789bcdfghjklmnpqrstvwxyz", 4))
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package id
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestGenSafeUniqueSlug(t *testing.T) {
|
||||||
|
slug := "slug"
|
||||||
|
r := map[string]bool{}
|
||||||
|
|
||||||
|
for i := 0; i < 1000; i++ {
|
||||||
|
s := GenSafeUniqueSlug(slug)
|
||||||
|
if s == slug {
|
||||||
|
t.Errorf("Got same slug as inputted!")
|
||||||
|
}
|
||||||
|
if _, ok := r[s]; ok {
|
||||||
|
t.Errorf("#%d: slug %s was already generated in testing.", i, s)
|
||||||
|
}
|
||||||
|
r[s] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user