Skip to content

Commit

Permalink
improve str normalize func
Browse files Browse the repository at this point in the history
  • Loading branch information
dogancanbakir committed Aug 14, 2024
1 parent 7b58775 commit 7265c5b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
21 changes: 17 additions & 4 deletions strings/strings_normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package stringsutil

import (
"strings"
"unicode"

"github.com/microcosm-cc/bluemonday"
)

type NormalizeOptions struct {
TrimSpaces bool
StripHTML bool
Lowercase bool
Uppercase bool
TrimSpaces bool
TrimCutset string
StripHTML bool
Lowercase bool
Uppercase bool
StripComments bool
}

var DefaultNormalizeOptions NormalizeOptions = NormalizeOptions{
Expand All @@ -25,6 +28,10 @@ func NormalizeWithOptions(data string, options NormalizeOptions) string {
data = strings.TrimSpace(data)
}

if options.TrimCutset != "" {
data = strings.Trim(data, options.TrimCutset)
}

if options.Lowercase {
data = strings.ToLower(data)
}
Expand All @@ -37,6 +44,12 @@ func NormalizeWithOptions(data string, options NormalizeOptions) string {
data = HTMLPolicy.Sanitize(data)
}

if options.StripComments {
if cut := strings.IndexAny(data, "#"); cut >= 0 {
data = strings.TrimRightFunc(data[:cut], unicode.IsSpace)
}
}

return data
}

Expand Down
43 changes: 43 additions & 0 deletions strings/stringsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,46 @@ func TestContainsAllI(t *testing.T) {
require.Equal(t, test.result, res)
}
}

func TestNormalizeWithOptions(t *testing.T) {
tests := []struct {
data string
options NormalizeOptions
result string
}{
{
data: " Hello World! ",
options: NormalizeOptions{TrimSpaces: true},
result: "Hello World!",
},
{
data: "\n\t\"'` Hello World! \n\t\"'` ",
options: NormalizeOptions{TrimCutset: "\n\t\"'` "},
result: "Hello World!",
},
{
data: " Hello World! ",
options: NormalizeOptions{Lowercase: true},
result: " hello world! ",
},
{
data: " Hello World! ",
options: NormalizeOptions{Uppercase: true},
result: " HELLO WORLD! ",
},
{
data: "<b>Hello World!</b>",
options: NormalizeOptions{StripHTML: true},
result: "Hello World!",
},
{
data: "Hello World! # Comment",
options: NormalizeOptions{StripComments: true},
result: "Hello World!",
},
}
for _, test := range tests {
res := NormalizeWithOptions(test.data, test.options)
require.Equal(t, test.result, res)
}
}

0 comments on commit 7265c5b

Please sign in to comment.