Skip to content

Commit be8352e

Browse files
hscasnmattwelke
andauthored
[feat] Allowing custom trim set characters (#15)
* [feat] Allowing custom trim set characters * Improving unit tests * Update README.md Co-authored-by: Matt Welke <[email protected]> --------- Co-authored-by: Matt Welke <[email protected]>
1 parent 9feff51 commit be8352e

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,14 @@ s := sanitizer.New(sanitizer.OptionDateFormat{
8989

9090
1. **max=`<n>`** - Maximum string length. It will truncate the string to `<n>` characters if this limit is exceeded
9191
1. **trim** - Remove trailing spaces left and right
92+
1. **trim=`<c>`** - Remove trailing characters `<c>` left and right. You can provide more than one character. Example: `trim= \n` will trim spaces and new lines
9293
1. **lower** - Lowercase all characters in the string
9394
1. **upper** - Uppercase all characters in the string
9495
1. **title** - First character of every word is changed to uppercase, the rest to lowercase. Uses Go's built in `strings.Title()` function.
9596
1. **cap** - Only the first letter of the string will be changed to uppercase, the rest to lowercase
9697
1. **def=`<n>`** (only available for pointers) - Sets a default `<n>` value in case the pointer is `nil`
9798
1. **xss** - Will remove brackets such as <>[](){} and the characters !=? from the string
98-
1. **date** - Will parse the string using the input formats provided in the options and print it using the output format provided in the options. If the string can not be parsed, it will be left empty.
99+
1. **date** - Will parse the string using the input formats provided in the options and print it using the output format provided in the options. If the string can not be parsed, it will be left empty
99100

100101
The order of precedence will be: **xss** -> **trim** -> **date** -> **max** -> **lower** -> **upper** -> **title** -> **cap**
101102

string.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ func sanitizeStrField(s Sanitizer, structValue reflect.Value, idx int) error {
5656

5757
// Trim must happen before the other tags, no matter what other
5858
// components there are.
59-
if _, ok := tags["trim"]; ok {
60-
// Ignore value of this component, we don't care *how* to trim,
61-
// we just trim.
59+
if trimset, ok := tags["trim"]; ok {
60+
if len(trimset) == 0 {
61+
trimset = " "
62+
}
6263
oldStr := field.String()
63-
field.SetString(strings.Trim(oldStr, " "))
64+
field.SetString(strings.Trim(oldStr, trimset))
6465
}
6566

6667
// Apply rest of transforms

string_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ func Test_sanitizeStrField(t *testing.T) {
2121
type TestStrStructTrim struct {
2222
Field string `san:"trim"`
2323
}
24+
type TestStrStructTrimCustom struct {
25+
Field string `san:"trim= \ns7"`
26+
}
2427
type TestStrStructLower struct {
2528
Field string `san:"lower"`
2629
}
@@ -48,6 +51,9 @@ func Test_sanitizeStrField(t *testing.T) {
4851
type TestStrStructPtrTrim struct {
4952
Field *string `san:"trim"`
5053
}
54+
type TestStrStructPtrTrimCustom struct {
55+
Field *string `san:"trim= \ns7"`
56+
}
5157
type TestStrStructPtrLower struct {
5258
Field *string `san:"lower"`
5359
}
@@ -85,6 +91,8 @@ func Test_sanitizeStrField(t *testing.T) {
8591
resString8 := " Test test test test "
8692
argString9 := " hernández "
8793
resString9 := " Hernández "
94+
argString10 := "s7\n7s tEst 7s\n7s"
95+
resString10 := "tEst"
8896

8997
type args struct {
9098
v interface{}
@@ -148,6 +156,19 @@ func Test_sanitizeStrField(t *testing.T) {
148156
},
149157
wantErr: false,
150158
},
159+
{
160+
name: "Trims a string field on a struct with the custom tag.",
161+
args: args{
162+
v: &TestStrStructTrimCustom{
163+
Field: "s7\n7s tEst 7s\n7s",
164+
},
165+
idx: 0,
166+
},
167+
want: &TestStrStructTrimCustom{
168+
Field: "tEst",
169+
},
170+
wantErr: false,
171+
},
151172
{
152173
name: "Lowercases a string field on a struct with the tag.",
153174
args: args{
@@ -278,6 +299,19 @@ func Test_sanitizeStrField(t *testing.T) {
278299
},
279300
wantErr: false,
280301
},
302+
{
303+
name: "Trims a string field on a struct with the custom tag.",
304+
args: args{
305+
v: &TestStrStructPtrTrimCustom{
306+
Field: &argString10,
307+
},
308+
idx: 0,
309+
},
310+
want: &TestStrStructPtrTrimCustom{
311+
Field: &resString10,
312+
},
313+
wantErr: false,
314+
},
281315
{
282316
name: "Lowercases a *string field on a struct with the tag.",
283317
args: args{

tags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (s Sanitizer) fieldTags(f reflect.StructTag) map[string]string {
2424
m[kv[0]] = kv[1]
2525
} else {
2626
// Use directly. Ex. 'trim' without value
27-
m[comp] = "_"
27+
m[comp] = ""
2828
}
2929
}
3030

0 commit comments

Comments
 (0)