Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ These functions modify the data in-place.
| strip_num | Strips all ascii numeric characters from the data. |
| strip_alpha_unicode | Strips all unicode characters from the data. |
| strip_num_unicode | Strips all unicode numeric characters from the data. |
| strip_punctuation | Strips all ascii punctuation from the data. |



Expand Down
1 change: 1 addition & 0 deletions modifiers/modifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func New() *mold.Transformer {
mod.Register("strip_num", stripNumCase)
mod.Register("strip_num_unicode", stripNumUnicodeCase)
mod.Register("strip_alpha_unicode", stripAlphaUnicodeCase)
mod.Register("strip_punctuation", stripPunctuation)
mod.Register("camel", camelCase)
mod.Register("default", defaultValue)
return mod
Expand Down
11 changes: 11 additions & 0 deletions modifiers/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ func stripAlphaUnicodeCase(ctx context.Context, fl mold.FieldLevel) error {
return nil
}

var stripPunctuationRegex = regexp.MustCompile(`[[:punct:]]`)

// stripPunctuation removes punctuation. Example: "# M5W-1E6!!!" -> " M5W1E6"
func stripPunctuation(ctx context.Context, fl mold.FieldLevel) error {
switch fl.Field().Kind() {
case reflect.String:
fl.Field().SetString(stripPunctuationRegex.ReplaceAllLiteralString(fl.Field().String(), ""))
}
return nil
}

// camelCase converts string to camel case
func camelCase(ctx context.Context, fl mold.FieldLevel) error {
switch fl.Field().Kind() {
Expand Down
46 changes: 46 additions & 0 deletions modifiers/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,52 @@ func TestNotAlphaCase(t *testing.T) {
}
}

func TestPunctuation(t *testing.T) {
conform := New()

s := "# M5W-1E6!!!"
expected := " M5W1E6"

type Test struct {
String string `mod:"strip_punctuation"`
}

tt := Test{String: s}
err := conform.Struct(context.Background(), &tt)
if err != nil {
log.Fatal(err)
}
if tt.String != expected {
t.Fatalf("Unexpected value '%s'\n", tt.String)
}

err = conform.Field(context.Background(), &s, "strip_punctuation")
if err != nil {
log.Fatal(err)
}
if s != expected {
t.Fatalf("Unexpected value '%s'\n", s)
}

var iface interface{}
err = conform.Field(context.Background(), &iface, "strip_punctuation")
if err != nil {
log.Fatal(err)
}
if iface != nil {
t.Fatalf("Unexpected value '%v'\n", nil)
}

iface = s
err = conform.Field(context.Background(), &iface, "strip_punctuation")
if err != nil {
log.Fatal(err)
}
if iface != expected {
t.Fatalf("Unexpected value '%v'\n", iface)
}
}

func TestCamelCase(t *testing.T) {
conform := New()

Expand Down