Skip to content

Commit

Permalink
fix(): sanitize now preserves dashes
Browse files Browse the repository at this point in the history
  • Loading branch information
debuggerpk committed Oct 6, 2024
1 parent 4e859cc commit 0a1ada4
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 35 deletions.
22 changes: 11 additions & 11 deletions workflows/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@ func (w *options) ParentWorkflowID() string {
return w.ParentID
}

// IDSuffix sanitizes the suffix and returns it.
// IDSuffix Sanitizes the suffix and returns it.
func (w *options) IDSuffix() string {
parts := []string{w.Block, w.BlockID, w.Element, w.ElementID, w.Mod, w.ModID}
for _, key := range w.Order {
parts = append(parts, key, w.Props[key])
}

sanitized := make([]string, 0)
Sanitized := make([]string, 0)

// removing empty strings and trimming spaces
for _, part := range parts {
if strings.TrimSpace(part) != "" {
sanitized = append(sanitized, part)
Sanitized = append(Sanitized, part)
}
}

return strings.Join(sanitized, ".")
return strings.Join(Sanitized, ".")
}

// MaxAttempts returns the max attempts for the workflow.
Expand Down Expand Up @@ -95,7 +95,7 @@ func WithBlock(val string) Option {
return NewDuplicateIDPropError("block", o.(*options).Block, val)
}

o.(*options).Block = sanitize(val)
o.(*options).Block = Sanitize(val)

return nil
}
Expand All @@ -108,7 +108,7 @@ func WithBlockID(val string) Option {
return NewDuplicateIDPropError("blockID", o.(*options).BlockID, val)
}

o.(*options).BlockID = sanitize(val)
o.(*options).BlockID = Sanitize(val)

return nil
}
Expand All @@ -121,7 +121,7 @@ func WithElement(val string) Option {
return NewDuplicateIDPropError("element", o.(*options).Element, val)
}

o.(*options).Element = sanitize(val)
o.(*options).Element = Sanitize(val)

return nil
}
Expand All @@ -134,7 +134,7 @@ func WithElementID(val string) Option {
return NewDuplicateIDPropError("element id", o.(*options).ElementID, val)
}

o.(*options).ElementID = sanitize(val)
o.(*options).ElementID = Sanitize(val)

return nil
}
Expand All @@ -147,7 +147,7 @@ func WithMod(val string) Option {
return NewDuplicateIDPropError("modifier", o.(*options).Mod, val)
}

o.(*options).Mod = sanitize(val)
o.(*options).Mod = Sanitize(val)

return nil
}
Expand All @@ -160,7 +160,7 @@ func WithModID(val string) Option {
return NewDuplicateIDPropError("modifier id", o.(*options).ModID, val)
}

o.(*options).ModID = sanitize(val)
o.(*options).ModID = Sanitize(val)

return nil
}
Expand All @@ -170,7 +170,7 @@ func WithModID(val string) Option {
func WithProp(key, val string) Option {
return func(o Options) error {
o.(*options).Order = append(o.(*options).Order, key)
o.(*options).Props[sanitize(key)] = sanitize(val)
o.(*options).Props[Sanitize(key)] = Sanitize(val)

return nil
}
Expand Down
39 changes: 39 additions & 0 deletions workflows/sanitize.go
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
}
61 changes: 61 additions & 0 deletions workflows/sanitize_test.go
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))
}
24 changes: 0 additions & 24 deletions workflows/utils.go

This file was deleted.

0 comments on commit 0a1ada4

Please sign in to comment.