checker/title.go
Onur Cinar bdb9c8ea9b
Version v2. (#158)
# Describe Request

Version v2.

# Change Type

New version.
2024-12-29 05:12:21 -08:00

51 lines
1.1 KiB
Go

// 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
}