-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtitle.go
51 lines (42 loc) · 1.09 KB
/
title.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
}