-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
10 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,6 @@ user := &User{ | |
} | ||
|
||
Check(user) | ||
|
||
fmt.Println(user.Username) // checker | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Title Case Normalizer | ||
|
||
The ```title``` normalizer maps the first letter of each word to their upper case. It can be mixed with checkers and other normalizers when defining the validation steps for user data. | ||
|
||
```golang | ||
type Book struct { | ||
Chapter string `checkers:"title"` | ||
} | ||
|
||
book := &Book{ | ||
Chapter: "THE Checker", | ||
} | ||
|
||
Check(book) | ||
|
||
fmt.Println(book.Chapter) // The Checker | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,6 @@ user := &User{ | |
} | ||
|
||
Check(user) | ||
|
||
fmt.Println(user.Username) // CHECKER | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,6 @@ user := &User{ | |
} | ||
|
||
Check(user) | ||
|
||
fmt.Println(user.Username) // normalizer | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,6 @@ user := &User{ | |
} | ||
|
||
Check(user) | ||
|
||
fmt.Println(user.Username) // CHECKER | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,5 @@ user := &User{ | |
Username: "chECker", | ||
} | ||
|
||
Check(user) | ||
fmt.Println(user.Username) // CHECKER | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package checker | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
"unicode" | ||
) | ||
|
||
// NormalizerTitle is the name of the normalizer. | ||
const NormalizerTitle = "title" | ||
|
||
// makeTitle makes a normalizer function for the title normalizer. | ||
func makeTitle(_ string) CheckFunc { | ||
return normalizeTitle | ||
} | ||
|
||
// normalizeTitle maps the first letter of each word to their upper case. | ||
func normalizeTitle(value, _ reflect.Value) Result { | ||
if value.Kind() != reflect.String { | ||
panic("string expected") | ||
} | ||
|
||
var sb strings.Builder | ||
begin := true | ||
|
||
for _, c := range value.String() { | ||
if unicode.IsLetter(c) { | ||
if begin { | ||
c = unicode.ToUpper(c) | ||
begin = false | ||
} else { | ||
c = unicode.ToLower(c) | ||
} | ||
} else { | ||
begin = true | ||
} | ||
|
||
sb.WriteRune(c) | ||
} | ||
|
||
value.SetString(sb.String()) | ||
|
||
return ResultValid | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package checker | ||
|
||
import "testing" | ||
|
||
func TestNormalizeTitleNonString(t *testing.T) { | ||
defer FailIfNoPanic(t) | ||
|
||
type Book struct { | ||
Chapter int `checkers:"title"` | ||
} | ||
|
||
book := &Book{} | ||
|
||
Check(book) | ||
} | ||
|
||
func TestNormalizeTitleResultValid(t *testing.T) { | ||
type Book struct { | ||
Chapter string `checkers:"title"` | ||
} | ||
|
||
book := &Book{ | ||
Chapter: "THE Checker", | ||
} | ||
|
||
_, valid := Check(book) | ||
if !valid { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestNormalizeTitle(t *testing.T) { | ||
type Book struct { | ||
Chapter string `checkers:"title"` | ||
} | ||
|
||
book := &Book{ | ||
Chapter: "THE Checker", | ||
} | ||
|
||
Check(book) | ||
|
||
if book.Chapter != "The Checker" { | ||
t.Fail() | ||
} | ||
} |