Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move string utils to central location #334

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions internal/utils/strings/strings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Package strings contains utility functions for working with strings.
package strings

import "strings"

// DeDupeStrSlice deduplicates a slices of strings
func DeDupeStrSlice(ss []string) []string {
found := make(map[string]bool)
l := []string{}
for _, s := range ss {
if _, ok := found[s]; !ok {
found[s] = true
l = append(l, s)
}
}
return l
}

// Sanitize sends a string to lowercase, trims whitespace, replaces all non alphanumeric characters with a dash,
// removes consecutive dashes, and trims any leading or trailing dashes.
func Sanitize(s string) string {
s = strings.ToLower(s)
s = strings.TrimSpace(s)
s = strings.Map(func(r rune) rune {
if r >= 'a' && r <= 'z' || r >= '0' && r <= '9' {
return r
}
return '-'
}, s)

// Remove consecutive dashes
var b strings.Builder
prevDash := false
for _, r := range s {
if r == '-' {
if !prevDash {
b.WriteRune(r)
prevDash = true
}
} else {
b.WriteRune(r)
prevDash = false
}
}

return strings.Trim(b.String(), "-")
}
90 changes: 90 additions & 0 deletions internal/utils/strings/strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package strings

import (
"slices"
"testing"
)

func TestDeDupeStrSlice(t *testing.T) {
tests := []struct {
name string
input []string
expected []string
}{
{
name: "Empty slice",
input: []string{},
expected: []string{},
},
{
name: "Single element",
input: []string{"foo"},
expected: []string{"foo"},
},
{
name: "Duplicate elements",
input: []string{"foo", "foo"},
expected: []string{"foo"},
},
{
name: "Multiple elements",
input: []string{"foo", "bar", "foo", "baz", "bar"},
expected: []string{"foo", "bar", "baz"},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := DeDupeStrSlice(tt.input)
if len(actual) != len(tt.expected) {
t.Fatalf("expected %v, got %v", tt.expected, actual)
}
if !slices.Equal(actual, tt.expected) {
t.Fatalf("expected %v, got %v", tt.expected, actual)
}
})
}
}

func TestSanitize(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "Empty string",
input: "",
expected: "",
},
{
name: "Lowercase",
input: "FOO",
expected: "foo",
},
{
name: "Trim whitespace",
input: " foo ",
expected: "foo",
},
{
name: "Replace non-alphanumeric characters",
input: "foo-bar_baz",
expected: "foo-bar-baz",
},
{
name: "Remove consecutive & leading/trailing dashes",
input: "! Example---string with **multiple** non-alphanumeric---characters---! ",
expected: "example-string-with-multiple-non-alphanumeric-characters",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := Sanitize(tt.input)
if actual != tt.expected {
t.Fatalf("expected %v, got %v", tt.expected, actual)
}
})
}
}
Loading