Skip to content

Commit

Permalink
dasherize
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Jul 28, 2018
1 parent 38140e2 commit 50035f4
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 27 deletions.
30 changes: 30 additions & 0 deletions dasherize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package flect

import (
"strings"
"unicode"
)

// Dasherize returns an alphanumeric, lowercased, dashed string
// Donald E. Knuth = donald-e-knuth
// Test with + sign = test-with-sign
// admin/WidgetID = admin-widget-id
func Dasherize(s string) string {
return New(s).Dasherize().String()
}

func (i Ident) Dasherize() Ident {
var parts []string

for _, part := range i.Parts {
var x string
for _, c := range part {
if unicode.IsLetter(c) || unicode.IsDigit(c) {
x += string(c)
}
}
parts = xappend(parts, x)
}

return New(strings.ToLower(strings.Join(parts, "-")))
}
29 changes: 29 additions & 0 deletions dasherize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package flect

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_Dasherize(t *testing.T) {
table := []tt{
{"", ""},
{"admin/WidgetID", "admin-widget-id"},
{"Donald E. Knuth", "donald-e-knuth"},
{"Random text with *(bad)* characters", "random-text-with-bad-characters"},
{"Trailing bad characters!@#", "trailing-bad-characters"},
{"!@#Leading bad characters", "leading-bad-characters"},
{"Squeeze separators", "squeeze-separators"},
{"Test with + sign", "test-with-sign"},
{"Test with malformed utf8 \251", "test-with-malformed-utf8"},
}

for _, tt := range table {
t.Run(tt.act, func(st *testing.T) {
r := require.New(st)
r.Equal(tt.exp, Dasherize(tt.act))
r.Equal(tt.exp, Dasherize(tt.exp))
})
}
}
32 changes: 32 additions & 0 deletions flect.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,41 @@
package flect

import (
"strings"
"unicode"
)

func isVowel(r rune) bool {
switch r {
case 'a', 'e', 'i', 'o', 'u':
return true
}
return false
}

var spaces = []rune{'_', ' ', ':', '-', '/'}

func isSpace(c rune) bool {
for _, r := range spaces {
if r == c {
return true
}
}
return unicode.IsSpace(c)
}

func xappend(a []string, ss ...string) []string {
for _, s := range ss {
s = strings.TrimSpace(s)
for _, x := range spaces {
s = strings.Trim(s, string(x))
}
if _, ok := baseAcronyms[strings.ToUpper(s)]; ok {
s = strings.ToUpper(s)
}
if s != "" {
a = append(a, s)
}
}
return a
}
27 changes: 0 additions & 27 deletions ident.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,3 @@ func toParts(s string) []string {

return parts
}

var spaces = []rune{'_', ' ', ':', '-', '/'}

func isSpace(c rune) bool {
for _, r := range spaces {
if r == c {
return true
}
}
return unicode.IsSpace(c)
}

func xappend(a []string, ss ...string) []string {
for _, s := range ss {
s = strings.TrimSpace(s)
for _, x := range spaces {
s = strings.Trim(s, string(x))
}
if _, ok := baseAcronyms[strings.ToUpper(s)]; ok {
s = strings.ToUpper(s)
}
if s != "" {
a = append(a, s)
}
}
return a
}

0 comments on commit 50035f4

Please sign in to comment.