Skip to content

Commit 469f31e

Browse files
authored
iss11
* update readme, remove redundant types * delete CircleCI config, remove unused struct * fix mattwelke GitHub username in licence * changed title casing functionality for strings to lowercase strings then apply std lib strings.Title function
1 parent 5fa1ff3 commit 469f31e

File tree

6 files changed

+22
-53
lines changed

6 files changed

+22
-53
lines changed

.circleci/config.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

LICENCE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@
185185
same "printed page" as the copyright notice for easier
186186
identification within third-party archives.
187187

188-
Copyright 2019 Matt Welke (https://github.com/welkie), Henrique S. Coelho (https://github.com/hscasn)
188+
Copyright 2021 Matt Welke (https://github.com/mattwelke), Henrique S. Coelho (https://github.com/hscasn)
189189

190190
Licensed under the Apache License, Version 2.0 (the "License");
191191
you may not use this file except in compliance with the License.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ s := sanitizer.New(sanitizer.OptionDateFormat{
9191
1. **trim** - Remove trailing spaces left and right
9292
1. **lower** - Lowercase all characters in the string
9393
1. **upper** - Uppercase all characters in the string
94-
1. **title** - First character of every word is changed to uppercase, the rest to lowercase
94+
1. **title** - First character of every word is changed to uppercase, the rest to lowercase. Uses Go's built in `strings.Title()` function.
9595
1. **cap** - Only the first letter of the string will be changed to uppercase, the rest to lowercase
9696
1. **def=`<n>`** (only available for pointers) - Sets a default `<n>` value in case the pointer is `nil`
9797
1. **xss** - Will remove brackets such as <>[](){} and the characters !=? from the string

sanitize_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,11 @@ func Test_Sanitize(t *testing.T) {
372372
},
373373
},
374374
SliPtrSubPtr1: &[]*TestStructMixedRecursiveSub{
375-
&TestStructMixedRecursiveSub{
375+
{
376376
StrField: " subtest1 ",
377377
StrPtrField: &arg71,
378378
},
379-
&TestStructMixedRecursiveSub{
379+
{
380380
StrField: " subtest2 ",
381381
StrPtrField: &arg72,
382382
},
@@ -487,11 +487,11 @@ func Test_Sanitize(t *testing.T) {
487487
},
488488
},
489489
SliPtrSubPtr1: &[]*TestStructMixedRecursiveSub{
490-
&TestStructMixedRecursiveSub{
490+
{
491491
StrField: "su",
492492
StrPtrField: &res71,
493493
},
494-
&TestStructMixedRecursiveSub{
494+
{
495495
StrField: "su",
496496
StrPtrField: &res72,
497497
},
@@ -831,4 +831,3 @@ func Test_SliceSanitize(t *testing.T) {
831831
})
832832
}
833833
}
834-

string.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,7 @@ func sanitizeStrField(s Sanitizer, structValue reflect.Value, idx int) error {
100100
}
101101

102102
func toTitle(s string) string {
103-
b := make([]byte, len(s))
104-
casediff := byte('a' - 'A')
105-
inWord := false
106-
for i := 0; i < len(s); i++ {
107-
b[i] = s[i]
108-
c := b[i]
109-
isLower := c >= 'a' && c <= 'z'
110-
isUpper := c >= 'A' && c <= 'Z'
111-
if !inWord && isLower { // Not inside a word and it's lower case
112-
b[i] -= casediff
113-
}
114-
if inWord && isUpper { // Inside a word and it's upper case
115-
b[i] += casediff
116-
}
117-
inWord = isLower || isUpper
118-
}
119-
return string(b)
103+
return strings.Title(strings.ToLower((s)))
120104
}
121105

122106
func toCap(s string) string {

string_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ func Test_sanitizeStrField(t *testing.T) {
6060
type TestStrStructPtrCap struct {
6161
Field *string `san:"cap"`
6262
}
63-
type TestStrStructPtrDef struct {
64-
Field *string `san:"def=et"`
65-
}
6663
type TestStrStructPtrTruncTrimLowerDef struct {
6764
Field *string `san:"max=2,trim,lower,def=et"`
6865
}
@@ -86,6 +83,8 @@ func Test_sanitizeStrField(t *testing.T) {
8683
resString7 := " Test Test Test Test "
8784
argString8 := " tEst TeSt test TEST "
8885
resString8 := " Test test test test "
86+
argString9 := " hernández "
87+
resString9 := " Hernández "
8988

9089
type args struct {
9190
v interface{}
@@ -331,6 +330,19 @@ func Test_sanitizeStrField(t *testing.T) {
331330
},
332331
wantErr: false,
333332
},
333+
{
334+
name: "Title cases a *string field on a struct with the tag when the string has a character with a punctuation mark.",
335+
args: args{
336+
v: &TestStrStructPtrTitle{
337+
Field: &argString9,
338+
},
339+
idx: 0,
340+
},
341+
want: &TestStrStructPtrTitle{
342+
Field: &resString9,
343+
},
344+
wantErr: false,
345+
},
334346
{
335347
name: "Puts a default value for a *string field that was nil on a struct with the tag.",
336348
args: args{

0 commit comments

Comments
 (0)