Add title normalizer and tests to v2. (#147)
# Describe Request Add title normalizer and tests to v2. # Change Type New code.
This commit is contained in:
parent
36a112c2c1
commit
08498b7929
3 changed files with 99 additions and 0 deletions
|
|
@ -35,6 +35,7 @@ var makers = map[string]MakeCheckFunc{
|
|||
nameMaxLen: makeMaxLen,
|
||||
nameMinLen: makeMinLen,
|
||||
nameRequired: makeRequired,
|
||||
nameTitle: makeTitle,
|
||||
nameTrimLeft: makeTrimLeft,
|
||||
nameTrimRight: makeTrimRight,
|
||||
nameTrimSpace: makeTrimSpace,
|
||||
|
|
|
|||
51
v2/title.go
Normal file
51
v2/title.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
// Copyright (c) 2023-2024 Onur Cinar.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
// https://github.com/cinar/checker
|
||||
|
||||
package v2
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
const (
|
||||
// nameTitle is the name of the title normalizer.
|
||||
nameTitle = "title"
|
||||
)
|
||||
|
||||
// Title returns the value of the string with the first letter of each word in upper case.
|
||||
func Title(value string) (string, error) {
|
||||
var sb strings.Builder
|
||||
begin := true
|
||||
|
||||
for _, c := range value {
|
||||
if unicode.IsLetter(c) {
|
||||
if begin {
|
||||
c = unicode.ToUpper(c)
|
||||
begin = false
|
||||
} else {
|
||||
c = unicode.ToLower(c)
|
||||
}
|
||||
} else {
|
||||
begin = true
|
||||
}
|
||||
|
||||
sb.WriteRune(c)
|
||||
}
|
||||
|
||||
return sb.String(), nil
|
||||
}
|
||||
|
||||
// reflectTitle returns the value of the string with the first letter of each word in upper case.
|
||||
func reflectTitle(value reflect.Value) (reflect.Value, error) {
|
||||
newValue, err := Title(value.Interface().(string))
|
||||
return reflect.ValueOf(newValue), err
|
||||
}
|
||||
|
||||
// makeTitle returns the title normalizer function.
|
||||
func makeTitle(_ string) CheckFunc[reflect.Value] {
|
||||
return reflectTitle
|
||||
}
|
||||
47
v2/title_test.go
Normal file
47
v2/title_test.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// Copyright (c) 2023-2024 Onur Cinar.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
// https://github.com/cinar/checker
|
||||
|
||||
package v2_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
v2 "github.com/cinar/checker/v2"
|
||||
)
|
||||
|
||||
func TestTitle(t *testing.T) {
|
||||
input := "the checker"
|
||||
expected := "The Checker"
|
||||
|
||||
actual, err := v2.Title(input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if actual != expected {
|
||||
t.Fatalf("actual %s expected %s", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReflectTitle(t *testing.T) {
|
||||
type Book struct {
|
||||
Chapter string `checkers:"title"`
|
||||
}
|
||||
|
||||
book := &Book{
|
||||
Chapter: "the checker",
|
||||
}
|
||||
|
||||
expected := "The Checker"
|
||||
|
||||
errs, ok := v2.CheckStruct(book)
|
||||
if !ok {
|
||||
t.Fatalf("got unexpected errors %v", errs)
|
||||
}
|
||||
|
||||
if book.Chapter != expected {
|
||||
t.Fatalf("actual %s expected %s", book.Chapter, expected)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue