Add upper and lower normalizers and tests to v2. (#146)
# Describe Request Add upper and lower normalizers and tests to v2. # Change Type New code.
This commit is contained in:
parent
e24eb02431
commit
36a112c2c1
5 changed files with 161 additions and 1 deletions
32
v2/lower.go
Normal file
32
v2/lower.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// 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"
|
||||
)
|
||||
|
||||
const (
|
||||
// nameLower is the name of the lower normalizer.
|
||||
nameLower = "lower"
|
||||
)
|
||||
|
||||
// Lower maps all Unicode letters in the given value to their lower case.
|
||||
func Lower(value string) (string, error) {
|
||||
return strings.ToLower(value), nil
|
||||
}
|
||||
|
||||
// reflectLower maps all Unicode letters in the given value to their lower case.
|
||||
func reflectLower(value reflect.Value) (reflect.Value, error) {
|
||||
newValue, err := Lower(value.Interface().(string))
|
||||
return reflect.ValueOf(newValue), err
|
||||
}
|
||||
|
||||
// makeLower returns the lower normalizer function.
|
||||
func makeLower(_ string) CheckFunc[reflect.Value] {
|
||||
return reflectLower
|
||||
}
|
||||
47
v2/lower_test.go
Normal file
47
v2/lower_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 TestLower(t *testing.T) {
|
||||
input := "CHECKER"
|
||||
expected := "checker"
|
||||
|
||||
actual, err := v2.Lower(input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if actual != expected {
|
||||
t.Fatalf("actual %s expected %s", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReflectLower(t *testing.T) {
|
||||
type Person struct {
|
||||
Name string `checkers:"lower"`
|
||||
}
|
||||
|
||||
person := &Person{
|
||||
Name: "CHECKER",
|
||||
}
|
||||
|
||||
expected := "checker"
|
||||
|
||||
errs, ok := v2.CheckStruct(person)
|
||||
if !ok {
|
||||
t.Fatalf("got unexpected errors %v", errs)
|
||||
}
|
||||
|
||||
if person.Name != expected {
|
||||
t.Fatalf("actual %s expected %s", person.Name, expected)
|
||||
}
|
||||
}
|
||||
|
|
@ -22,13 +22,14 @@ var makers = map[string]MakeCheckFunc{
|
|||
nameCreditCard: makeCreditCard,
|
||||
nameDigits: makeDigits,
|
||||
nameEmail: makeEmail,
|
||||
nameFQDN: makeFQDN,
|
||||
nameHTMLEscape: makeHTMLEscape,
|
||||
nameHTMLUnescape: makeHTMLUnescape,
|
||||
nameFQDN: makeFQDN,
|
||||
nameIP: makeIP,
|
||||
nameIPv4: makeIPv4,
|
||||
nameIPv6: makeIPv6,
|
||||
nameISBN: makeISBN,
|
||||
nameLower: makeLower,
|
||||
nameLUHN: makeLUHN,
|
||||
nameMAC: makeMAC,
|
||||
nameMaxLen: makeMaxLen,
|
||||
|
|
@ -37,6 +38,7 @@ var makers = map[string]MakeCheckFunc{
|
|||
nameTrimLeft: makeTrimLeft,
|
||||
nameTrimRight: makeTrimRight,
|
||||
nameTrimSpace: makeTrimSpace,
|
||||
nameUpper: makeUpper,
|
||||
nameURL: makeURL,
|
||||
nameURLEscape: makeURLEscape,
|
||||
nameURLUnescape: makeURLUnescape,
|
||||
|
|
|
|||
32
v2/upper.go
Normal file
32
v2/upper.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// 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"
|
||||
)
|
||||
|
||||
const (
|
||||
// nameUpper is the name of the upper normalizer.
|
||||
nameUpper = "upper"
|
||||
)
|
||||
|
||||
// Upper maps all Unicode letters in the given value to their upper case.
|
||||
func Upper(value string) (string, error) {
|
||||
return strings.ToUpper(value), nil
|
||||
}
|
||||
|
||||
// reflectUpper maps all Unicode letters in the given value to their upper case.
|
||||
func reflectUpper(value reflect.Value) (reflect.Value, error) {
|
||||
newValue, err := Upper(value.Interface().(string))
|
||||
return reflect.ValueOf(newValue), err
|
||||
}
|
||||
|
||||
// makeUpper returns the upper normalizer function.
|
||||
func makeUpper(_ string) CheckFunc[reflect.Value] {
|
||||
return reflectUpper
|
||||
}
|
||||
47
v2/upper_test.go
Normal file
47
v2/upper_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 TestUpper(t *testing.T) {
|
||||
input := "checker"
|
||||
expected := "CHECKER"
|
||||
|
||||
actual, err := v2.Upper(input)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if actual != expected {
|
||||
t.Fatalf("actual %s expected %s", actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReflectUpper(t *testing.T) {
|
||||
type Person struct {
|
||||
Name string `checkers:"upper"`
|
||||
}
|
||||
|
||||
person := &Person{
|
||||
Name: "checker",
|
||||
}
|
||||
|
||||
expected := "CHECKER"
|
||||
|
||||
errs, ok := v2.CheckStruct(person)
|
||||
if !ok {
|
||||
t.Fatalf("got unexpected errors %v", errs)
|
||||
}
|
||||
|
||||
if person.Name != expected {
|
||||
t.Fatalf("actual %s expected %s", person.Name, expected)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue