-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(): sanitize now preserves dashes
- Loading branch information
1 parent
4e859cc
commit 0a1ada4
Showing
4 changed files
with
111 additions
and
35 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
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,39 @@ | ||
package workflows | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// Sanitize converts a string to snake_case, preserving hyphens and handling various edge cases. It is intended for use | ||
// in generating unique identifiers for elements, modifiers, and blocks. | ||
func Sanitize(input string) string { | ||
// Normalize whitespace and special characters (excluding hyphens) to underscores. | ||
re := regexp.MustCompile(`[^a-zA-Z0-9-]+`) | ||
output := re.ReplaceAllString(input, "_") | ||
|
||
// Convert to lower case. | ||
output = strings.ToLower(output) | ||
|
||
// Normalize multiple underscores to a single underscore. | ||
output = regexp.MustCompile(`_+`).ReplaceAllString(output, "_") | ||
|
||
// Normalize multiple dashes to a single dash. (Crucial order) | ||
output = regexp.MustCompile(`-+`).ReplaceAllString(output, "-") | ||
|
||
// Handle empty input (crucially, trim whitespace first). | ||
output = strings.TrimSpace(output) | ||
if output == "" { | ||
return "" | ||
} | ||
|
||
if len(output) > 1 { | ||
// Remove leading/trailing underscores. | ||
output = strings.Trim(output, "_") | ||
|
||
// Remove leading/trailing dashes. | ||
output = strings.Trim(output, "-") | ||
} | ||
|
||
return output | ||
} |
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,61 @@ | ||
package workflows_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
"go.breu.io/durex/workflows" | ||
) | ||
|
||
type ( | ||
SanitizeTestSuite struct { | ||
suite.Suite | ||
} | ||
) | ||
|
||
func (suite *SanitizeTestSuite) TestSanitize() { | ||
cases := []struct { | ||
input string | ||
output string | ||
}{ | ||
{"Hello World", "hello_world"}, | ||
{"Hello-World", "hello-world"}, | ||
{"hello-world", "hello-world"}, | ||
{" hello-world ", "hello-world"}, | ||
{"hello world!", "hello_world"}, | ||
{"hello.world", "hello_world"}, | ||
{"-----hello---world----", "hello-world"}, | ||
{"hello--world---", "hello-world"}, | ||
{" hello--world ", "hello-world"}, | ||
{"!!!", "_"}, | ||
{"----", "-"}, | ||
{"", ""}, | ||
{" ", "_"}, | ||
{" ---hello--- ", "hello"}, | ||
{" hello--world-- ", "hello-world"}, | ||
{"-hello-", "hello"}, | ||
{"hello---", "hello"}, | ||
{"____hello____", "hello"}, | ||
{"_hello_", "hello"}, | ||
{"hello---world---", "hello-world"}, | ||
{"-hello-world-", "hello-world"}, | ||
{"hello-world-", "hello-world"}, | ||
{"-hello-world", "hello-world"}, | ||
{"hello-world_", "hello-world"}, | ||
{"_-hello-world_", "hello-world"}, | ||
{"_hello", "hello"}, | ||
{"hello_", "hello"}, | ||
} | ||
|
||
for _, testCase := range cases { | ||
suite.Run(testCase.input, func() { | ||
actual := workflows.Sanitize(testCase.input) | ||
suite.Equal(testCase.output, actual, "Sanitize(%q) should be %q, but got %q", testCase.input, testCase.output, actual) | ||
}) | ||
} | ||
} | ||
|
||
func TestSanitizeSuite(t *testing.T) { | ||
suite.Run(t, new(SanitizeTestSuite)) | ||
} |
This file was deleted.
Oops, something went wrong.