Skip to content

Commit ae7856a

Browse files
authored
Add strip_punctuation modifier (#35)
1 parent f0a6470 commit ae7856a

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ These functions modify the data in-place.
4949
| strip_num | Strips all ascii numeric characters from the data. |
5050
| strip_alpha_unicode | Strips all unicode characters from the data. |
5151
| strip_num_unicode | Strips all unicode numeric characters from the data. |
52+
| strip_punctuation | Strips all ascii punctuation from the data. |
5253

5354

5455

modifiers/modifiers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func New() *mold.Transformer {
2323
mod.Register("strip_num", stripNumCase)
2424
mod.Register("strip_num_unicode", stripNumUnicodeCase)
2525
mod.Register("strip_alpha_unicode", stripAlphaUnicodeCase)
26+
mod.Register("strip_punctuation", stripPunctuation)
2627
mod.Register("camel", camelCase)
2728
mod.Register("default", defaultValue)
2829
return mod

modifiers/string.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,17 @@ func stripAlphaUnicodeCase(ctx context.Context, fl mold.FieldLevel) error {
191191
return nil
192192
}
193193

194+
var stripPunctuationRegex = regexp.MustCompile(`[[:punct:]]`)
195+
196+
// stripPunctuation removes punctuation. Example: "# M5W-1E6!!!" -> " M5W1E6"
197+
func stripPunctuation(ctx context.Context, fl mold.FieldLevel) error {
198+
switch fl.Field().Kind() {
199+
case reflect.String:
200+
fl.Field().SetString(stripPunctuationRegex.ReplaceAllLiteralString(fl.Field().String(), ""))
201+
}
202+
return nil
203+
}
204+
194205
// camelCase converts string to camel case
195206
func camelCase(ctx context.Context, fl mold.FieldLevel) error {
196207
switch fl.Field().Kind() {

modifiers/string_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,52 @@ func TestNotAlphaCase(t *testing.T) {
765765
}
766766
}
767767

768+
func TestPunctuation(t *testing.T) {
769+
conform := New()
770+
771+
s := "# M5W-1E6!!!"
772+
expected := " M5W1E6"
773+
774+
type Test struct {
775+
String string `mod:"strip_punctuation"`
776+
}
777+
778+
tt := Test{String: s}
779+
err := conform.Struct(context.Background(), &tt)
780+
if err != nil {
781+
log.Fatal(err)
782+
}
783+
if tt.String != expected {
784+
t.Fatalf("Unexpected value '%s'\n", tt.String)
785+
}
786+
787+
err = conform.Field(context.Background(), &s, "strip_punctuation")
788+
if err != nil {
789+
log.Fatal(err)
790+
}
791+
if s != expected {
792+
t.Fatalf("Unexpected value '%s'\n", s)
793+
}
794+
795+
var iface interface{}
796+
err = conform.Field(context.Background(), &iface, "strip_punctuation")
797+
if err != nil {
798+
log.Fatal(err)
799+
}
800+
if iface != nil {
801+
t.Fatalf("Unexpected value '%v'\n", nil)
802+
}
803+
804+
iface = s
805+
err = conform.Field(context.Background(), &iface, "strip_punctuation")
806+
if err != nil {
807+
log.Fatal(err)
808+
}
809+
if iface != expected {
810+
t.Fatalf("Unexpected value '%v'\n", iface)
811+
}
812+
}
813+
768814
func TestCamelCase(t *testing.T) {
769815
conform := New()
770816

0 commit comments

Comments
 (0)