diff options
author | Martin Czygan <martin.czygan@gmail.com> | 2021-07-13 01:23:59 +0200 |
---|---|---|
committer | Martin Czygan <martin.czygan@gmail.com> | 2021-07-13 01:23:59 +0200 |
commit | eda3ed4ff659104106427efba742b8b21c8583d0 (patch) | |
tree | d26f2a56d0a314c98ea25d945856df3300db3234 | |
parent | a70e5f91a29cea7689f023a19e58037869fccf1e (diff) | |
download | refcat-eda3ed4ff659104106427efba742b8b21c8583d0.tar.gz refcat-eda3ed4ff659104106427efba742b8b21c8583d0.zip |
separate slugify functions
-rw-r--r-- | skate/slugify.go | 18 | ||||
-rw-r--r-- | skate/slugify_test.go | 21 | ||||
-rw-r--r-- | skate/verify.go | 11 | ||||
-rw-r--r-- | skate/verify_test.go | 17 |
4 files changed, 39 insertions, 28 deletions
diff --git a/skate/slugify.go b/skate/slugify.go new file mode 100644 index 0000000..e8fa8bf --- /dev/null +++ b/skate/slugify.go @@ -0,0 +1,18 @@ +package skate + +import ( + "bytes" + "fmt" + "strings" +) + +// slugifyString is a basic string slugifier. +func slugifyString(s string) string { + var buf bytes.Buffer + for _, c := range strings.TrimSpace(strings.ToLower(s)) { + if (c > 96 && c < 123) || (c > 47 && c < 58) || (c == 32) || (c == 9) || (c == 10) { + fmt.Fprintf(&buf, "%c", c) + } + } + return strings.Join(strings.Fields(buf.String()), " ") +} diff --git a/skate/slugify_test.go b/skate/slugify_test.go new file mode 100644 index 0000000..adc5646 --- /dev/null +++ b/skate/slugify_test.go @@ -0,0 +1,21 @@ +package skate + +import "testing" + +func TestSlugifyString(t *testing.T) { + var cases = []struct { + s string + result string + }{ + {"", ""}, + {" ", ""}, + {" Optimize everything", "optimize everything"}, + {"ABCü~", "abc"}, + } + for _, c := range cases { + got := slugifyString(c.s) + if got != c.result { + t.Errorf("slugifyString: '%v', want '%v', got '%v'", c.s, c.result, got) + } + } +} diff --git a/skate/verify.go b/skate/verify.go index d6f3fd9..db3a925 100644 --- a/skate/verify.go +++ b/skate/verify.go @@ -671,17 +671,6 @@ func absInt(v int) int { return v } -// slugifyString is a basic string slugifier. -func slugifyString(s string) string { - var buf bytes.Buffer - for _, c := range strings.TrimSpace(strings.ToLower(s)) { - if (c > 96 && c < 123) || (c > 47 && c < 58) || (c == 32) || (c == 9) || (c == 10) { - fmt.Fprintf(&buf, "%c", c) - } - } - return strings.Join(strings.Fields(buf.String()), " ") -} - // looksLikeComponent returns true, if either a looks like a component of b, or vice versa. func looksLikeComponent(a, b string) bool { ac := strings.Split(a, ".") diff --git a/skate/verify_test.go b/skate/verify_test.go index b7aa429..0c6c88a 100644 --- a/skate/verify_test.go +++ b/skate/verify_test.go @@ -9,23 +9,6 @@ import ( "github.com/segmentio/encoding/json" ) -func TestSlugifyString(t *testing.T) { - var cases = []struct { - s string - result string - }{ - {"", ""}, - {" ", ""}, - {" Optimize everything", "optimize everything"}, - {"ABCü~", "abc"}, - } - for _, c := range cases { - got := slugifyString(c.s) - if got != c.result { - t.Errorf("slugifyString: '%v', want '%v', got '%v'", c.s, c.result, got) - } - } -} func TestLooksLikeComponent(t *testing.T) { var cases = []struct { a, b string |