-
Notifications
You must be signed in to change notification settings - Fork 270
/
case.go
executable file
·77 lines (69 loc) · 1.5 KB
/
case.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package misspell
import (
"strings"
"unicode"
)
// WordCase is an enum of various word casing styles
type WordCase int
// Various WordCase types.. likely to be not correct
const (
AllLower WordCase = iota
AllUpper
Title
Mixed
Camel
)
// CaseStyle returns what case style a word is in
func CaseStyle(word string) WordCase {
hasTitle := false
upperCount := 0
lowerCount := 0
runeCount := 0
// this iterates over RUNES not BYTES
for _, r := range word {
// ASCII apostrophe doesn't count
// want words like "don't" to have
// upper case forms when adding to dictionary
if r == 0x0027 {
continue
}
runeCount++
if unicode.IsLower(r) {
lowerCount++
continue
}
if unicode.IsUpper(r) {
if runeCount == 1 {
hasTitle = true
}
upperCount++
continue
}
//???
}
switch {
case runeCount == lowerCount:
return AllLower
case runeCount == upperCount:
return AllUpper
case hasTitle && runeCount-1 == lowerCount:
return Title
default:
return Mixed
}
}
// CaseVariations returns
// If AllUpper or First-Letter-Only is upcased: add the all upper case version
// If AllLower, add the original, the title and upcase forms
// If Mixed, return the original, and the all upcase form
//
func CaseVariations(word string, style WordCase) []string {
switch style {
case AllLower:
return []string{word, strings.ToUpper(word[0:1]) + word[1:], strings.ToUpper(word)}
case AllUpper:
return []string{strings.ToUpper(word)}
default:
return []string{word, strings.ToUpper(word)}
}
}