Skip to content

Commit

Permalink
Title normalizer is added. Fixes #45 (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
cinar authored Jun 18, 2023
1 parent ef73947 commit 3d03341
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ This package currently provides the following normalizers. They can be mixed wit

- [lower](doc/normalizers/lower.md) maps all Unicode letters in the given value to their lower case.
- [upper](doc/normalizers/upper.md) maps all Unicode letters in the given value to their upper case.
- [title](doc/normalizers/title.md) maps the first letter of each word to their upper case.
- [trim](doc/normalizers/trim.md) removes the whitespaces at the beginning and at the end of the given value.
- [trim-left](doc/normalizers/trim_left.md) removes the whitespaces at the beginning of the given value.
- [trim-right](doc/normalizers/trim_right.md) removes the whitespaces at the end of the given value.
Expand Down
1 change: 1 addition & 0 deletions checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var makers = map[string]MakeFunc{
CheckerSame: makeSame,
NormalizerLower: makeLower,
NormalizerUpper: makeUpper,
NormalizerTitle: makeTitle,
NormalizerTrim: makeTrim,
NormalizerTrimLeft: makeTrimLeft,
NormalizerTrimRight: makeTrimRight,
Expand Down
2 changes: 2 additions & 0 deletions doc/normalizers/lower.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ user := &User{
}

Check(user)

fmt.Println(user.Username) // checker
```
17 changes: 17 additions & 0 deletions doc/normalizers/title.md
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
```
2 changes: 2 additions & 0 deletions doc/normalizers/trim.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ user := &User{
}

Check(user)

fmt.Println(user.Username) // CHECKER
```
2 changes: 2 additions & 0 deletions doc/normalizers/trim_left.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ user := &User{
}

Check(user)

fmt.Println(user.Username) // normalizer
```
2 changes: 2 additions & 0 deletions doc/normalizers/trim_right.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ user := &User{
}

Check(user)

fmt.Println(user.Username) // CHECKER
```
2 changes: 1 addition & 1 deletion doc/normalizers/upper.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ user := &User{
Username: "chECker",
}

Check(user)
fmt.Println(user.Username) // CHECKER
```
44 changes: 44 additions & 0 deletions title.go
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
}
46 changes: 46 additions & 0 deletions title_test.go
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()
}
}

0 comments on commit 3d03341

Please sign in to comment.