From eda3ed4ff659104106427efba742b8b21c8583d0 Mon Sep 17 00:00:00 2001 From: Martin Czygan Date: Tue, 13 Jul 2021 01:23:59 +0200 Subject: separate slugify functions --- skate/slugify.go | 18 ++++++++++++++++++ skate/slugify_test.go | 21 +++++++++++++++++++++ skate/verify.go | 11 ----------- skate/verify_test.go | 17 ----------------- 4 files changed, 39 insertions(+), 28 deletions(-) create mode 100644 skate/slugify.go create mode 100644 skate/slugify_test.go (limited to 'skate') 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 -- cgit v1.2.3