diff --git a/strings/strings_normalize.go b/strings/strings_normalize.go index e7d75a8..b33d834 100644 --- a/strings/strings_normalize.go +++ b/strings/strings_normalize.go @@ -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{ @@ -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) } @@ -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 } diff --git a/strings/stringsutil_test.go b/strings/stringsutil_test.go index 48a1756..3fff945 100644 --- a/strings/stringsutil_test.go +++ b/strings/stringsutil_test.go @@ -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: "Hello World!", + 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) + } +}