Skip to content

Commit

Permalink
chore: move string utils to central location (#334)
Browse files Browse the repository at this point in the history
## Issue
None

## Description
Move string utils package to common location for reuse

Signed-off-by: Artur Shad Nik <artur@spectrocloud.com>
  • Loading branch information
arturshadnik committed Jul 19, 2024
1 parent de015d9 commit 44c09ab
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
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)
}
})
}
}

0 comments on commit 44c09ab

Please sign in to comment.