-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move string utils to central location (#334)
## Issue None ## Description Move string utils package to common location for reuse Signed-off-by: Artur Shad Nik <artur@spectrocloud.com>
- Loading branch information
1 parent
de015d9
commit 44c09ab
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), "-") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |