-
Notifications
You must be signed in to change notification settings - Fork 7
/
transform.go
144 lines (122 loc) · 4.85 KB
/
transform.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package handy
import (
"strings"
"unicode/utf8"
)
const (
// TransformNone No transformations are ordered. Only constraints maximum length
// TransformNone turns all other flags OFF.
TransformNone = 1
// TransformFlagTrim Trim spaces before and after process the input
// TransformFlagTrim Trims the string, removing leading and trailing spaces
TransformFlagTrim = 2
// TransformFlagLowerCase Makes the string lowercase
// If case transformation flags are combined, the last one remains, considering the following order: TransformFlagTitleCase, TransformFlagLowerCase and TransformFlagUpperCase.
TransformFlagLowerCase = 4
// TransformFlagUpperCase Makes the string uppercase
// If case transformation flags are combined, the last one remains, considering the following order: TransformFlagTitleCase, TransformFlagLowerCase and TransformFlagUpperCase.
TransformFlagUpperCase = 8
// TransformFlagOnlyDigits Removes all non-numeric characters
TransformFlagOnlyDigits = 16
// TransformFlagOnlyLetters Removes all non-letter characters
TransformFlagOnlyLetters = 32
// TransformFlagOnlyLettersAndDigits Leaves only letters and numbers
TransformFlagOnlyLettersAndDigits = 64
// TransformFlagHash After process all other flags, applies SHA256 hashing on string for output
// The routine applies handy.StringHash() on given string
TransformFlagHash = 128
// TransformFlagTitleCase Makes the string uppercase
// If case transformation flags are combined, the last one remains, considering the following order: TransformFlagTitleCase, TransformFlagLowerCase and TransformFlagUpperCase.
TransformFlagTitleCase = 256
// TransformFlagRemoveDigits Removes all digit characters, without to touch on any other
// If combined with TransformFlagOnlyLettersAndDigits, TransformFlagOnlyDigits or TransformFlagOnlyLetters, it's ineffective
TransformFlagRemoveDigits = 512
)
// Transform handles a string according given flags/parametrization, as follows:
// The transformations are made in arbitrary order, what can result in unexpected output. It the input matters, use TransformSerially instead.
// If maxLen==0, truncation is skipped
// The last operations are, by order, truncation and trimming.
func Transform(s string, maxLen int, transformFlags uint) string {
if s == "" {
return s
}
if transformFlags&TransformNone == TransformNone {
if maxLen > 0 && utf8.RuneCountInString(s) > maxLen {
s = string([]rune(s)[:maxLen])
}
return s
}
if (transformFlags & TransformFlagOnlyLettersAndDigits) == TransformFlagOnlyLettersAndDigits {
s = OnlyLettersAndNumbers(s)
}
if (transformFlags & TransformFlagOnlyDigits) == TransformFlagOnlyDigits {
s = OnlyDigits(s)
}
if (transformFlags & TransformFlagOnlyLetters) == TransformFlagOnlyLetters {
s = OnlyLetters(s)
}
if (transformFlags & TransformFlagRemoveDigits) == TransformFlagRemoveDigits {
s = RemoveDigits(s)
}
// Have to trim before and after, to avoid issues with string truncation and new leading/trailing spaces
if (transformFlags & TransformFlagTrim) == TransformFlagTrim {
s = strings.TrimSpace(s)
}
if (transformFlags & TransformFlagTitleCase) == TransformFlagTitleCase {
s = strings.Title(strings.ToLower(s))
}
if (transformFlags & TransformFlagLowerCase) == TransformFlagLowerCase {
s = strings.ToLower(s)
}
if (transformFlags & TransformFlagUpperCase) == TransformFlagUpperCase {
s = strings.ToUpper(s)
}
if (transformFlags & TransformFlagHash) == TransformFlagHash {
s = StringHash(s)
}
if s == "" {
return s
}
if maxLen > 0 && utf8.RuneCountInString(s) > maxLen {
s = string([]rune(s)[:maxLen])
}
// Have to trim before and after, to avoid issues with string truncation and new leading/trailing spaces
if (transformFlags & TransformFlagTrim) == TransformFlagTrim {
s = strings.TrimSpace(s)
}
return s
}
// TransformSerially reformat given string according parameters, in the order these params were sent
// Example: TransformSerially("uh lalah 123", 4, TransformFlagOnlyDigits,TransformFlagHash,TransformFlagUpperCase)
// First remove non-digits, then hashes string and after make it all uppercase.
// If maxLen==0, truncation is skipped
// Truncation is the last operation
func TransformSerially(s string, maxLen int, transformFlags ...uint) string {
if s == "" {
return s
}
for _, flag := range transformFlags {
switch flag {
case TransformFlagOnlyLettersAndDigits:
s = OnlyLettersAndNumbers(s)
case TransformFlagOnlyDigits:
s = OnlyDigits(s)
case TransformFlagOnlyLetters:
s = OnlyLetters(s)
case TransformFlagTrim:
s = strings.TrimSpace(s)
case TransformFlagTitleCase:
s = strings.ToTitle(s)
case TransformFlagLowerCase:
s = strings.ToLower(s)
case TransformFlagUpperCase:
s = strings.ToUpper(s)
case TransformFlagHash:
s = StringHash(s)
}
}
if maxLen > 0 && utf8.RuneCountInString(s) > maxLen {
s = string([]rune(s)[:maxLen])
}
return s
}