Skip to content

Commit

Permalink
iss11
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mattwelke authored Jul 13, 2021
1 parent 5fa1ff3 commit 469f31e
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 53 deletions.
26 changes: 0 additions & 26 deletions .circleci/config.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENCE
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

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

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ s := sanitizer.New(sanitizer.OptionDateFormat{
1. **trim** - Remove trailing spaces left and right
1. **lower** - Lowercase all characters in the string
1. **upper** - Uppercase all characters in the string
1. **title** - First character of every word is changed to uppercase, the rest to lowercase
1. **title** - First character of every word is changed to uppercase, the rest to lowercase. Uses Go's built in `strings.Title()` function.
1. **cap** - Only the first letter of the string will be changed to uppercase, the rest to lowercase
1. **def=`<n>`** (only available for pointers) - Sets a default `<n>` value in case the pointer is `nil`
1. **xss** - Will remove brackets such as <>[](){} and the characters !=? from the string
Expand Down
9 changes: 4 additions & 5 deletions sanitize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,11 @@ func Test_Sanitize(t *testing.T) {
},
},
SliPtrSubPtr1: &[]*TestStructMixedRecursiveSub{
&TestStructMixedRecursiveSub{
{
StrField: " subtest1 ",
StrPtrField: &arg71,
},
&TestStructMixedRecursiveSub{
{
StrField: " subtest2 ",
StrPtrField: &arg72,
},
Expand Down Expand Up @@ -487,11 +487,11 @@ func Test_Sanitize(t *testing.T) {
},
},
SliPtrSubPtr1: &[]*TestStructMixedRecursiveSub{
&TestStructMixedRecursiveSub{
{
StrField: "su",
StrPtrField: &res71,
},
&TestStructMixedRecursiveSub{
{
StrField: "su",
StrPtrField: &res72,
},
Expand Down Expand Up @@ -831,4 +831,3 @@ func Test_SliceSanitize(t *testing.T) {
})
}
}

18 changes: 1 addition & 17 deletions string.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,7 @@ func sanitizeStrField(s Sanitizer, structValue reflect.Value, idx int) error {
}

func toTitle(s string) string {
b := make([]byte, len(s))
casediff := byte('a' - 'A')
inWord := false
for i := 0; i < len(s); i++ {
b[i] = s[i]
c := b[i]
isLower := c >= 'a' && c <= 'z'
isUpper := c >= 'A' && c <= 'Z'
if !inWord && isLower { // Not inside a word and it's lower case
b[i] -= casediff
}
if inWord && isUpper { // Inside a word and it's upper case
b[i] += casediff
}
inWord = isLower || isUpper
}
return string(b)
return strings.Title(strings.ToLower((s)))
}

func toCap(s string) string {
Expand Down
18 changes: 15 additions & 3 deletions string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ func Test_sanitizeStrField(t *testing.T) {
type TestStrStructPtrCap struct {
Field *string `san:"cap"`
}
type TestStrStructPtrDef struct {
Field *string `san:"def=et"`
}
type TestStrStructPtrTruncTrimLowerDef struct {
Field *string `san:"max=2,trim,lower,def=et"`
}
Expand All @@ -86,6 +83,8 @@ func Test_sanitizeStrField(t *testing.T) {
resString7 := " Test Test Test Test "
argString8 := " tEst TeSt test TEST "
resString8 := " Test test test test "
argString9 := " hernández "
resString9 := " Hernández "

type args struct {
v interface{}
Expand Down Expand Up @@ -331,6 +330,19 @@ func Test_sanitizeStrField(t *testing.T) {
},
wantErr: false,
},
{
name: "Title cases a *string field on a struct with the tag when the string has a character with a punctuation mark.",
args: args{
v: &TestStrStructPtrTitle{
Field: &argString9,
},
idx: 0,
},
want: &TestStrStructPtrTitle{
Field: &resString9,
},
wantErr: false,
},
{
name: "Puts a default value for a *string field that was nil on a struct with the tag.",
args: args{
Expand Down

0 comments on commit 469f31e

Please sign in to comment.