-
Notifications
You must be signed in to change notification settings - Fork 0
/
title.go
49 lines (39 loc) · 939 Bytes
/
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
// 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 checker
import (
"reflect"
"strings"
"unicode"
)
// tagTitle is the tag of the normalizer.
const tagTitle = "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) error {
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 nil
}