-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisAlpham.go
211 lines (193 loc) · 7.22 KB
/
isAlpham.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package validatorgo
import (
"regexp"
"strings"
)
var (
isAlphaOptsDefaultIgnore string = ""
isAlphaOptsDefaultLocale string = "en-US"
)
// IsAlphaOpts is used to configure IsAlpha
type IsAlphaOpts struct {
Ignore string // string to be ignored
Locale string // a locale
}
// regexEscapes is the set of special regex characters escaped
var regexEscapes = map[string]string{
".": `\.`,
"*": `\*`,
"+": `\+`,
"?": `\?`,
"^": `\^`,
"$": `\$`,
"(": `\(`,
")": `\)`,
"[": `\[`,
"]": `\]`,
"{": `\{`,
"}": `\}`,
"|": `\|`,
"\\": `\\`,
"-": `\-`,
"<": `\<`,
">": `\>`,
`"`: `\"`,
`'`: `\'`,
}
var (
// writingSystemAlphaRegex is the set of common writing systems and their validating alphabetical regex
writingSystemAlphaRegex = map[string]string{
"arabic": "^[\u0600-\u06FF\u0750-\u077F]+$", // Arabic
"latin": "^[A-Za-z]+$", // Latin
"latin_czech": "^[A-Za-zÁČĎÉĚÍŇÓŘŠŤÚŮÝŽáčďéěíňóřšťúůýž]+$", // Latin with Czech-specific characters
"latin_danish": "^[A-Za-zÆØÅæøå]+$", // Latin with Danish-specific characters
"latin_german": "^[A-Za-zÄÖÜßäöü]+$", // Latin with German-specific characters
"latin_spanish": "^[A-Za-zÁÉÍÑÓÚÜáéíñóúü]+$", // Latin with Spanish-specific characters
"latin_french": "^[A-Za-zÀÂÇÉÈÊËÎÏÔŒÙÛÜŸàâçéèêëîïôœùûüÿ]+$", // Latin with French-specific characters
"latin_esperanto": "^[A-Za-zĈĜĤĴŜŬĉĝĥĵŝŭ]+$", // Latin with Esperanto-specific characters
"cyrillic": "^[А-Яа-яЁё]+$", // Cyrillic
"greek": "^[Α-Ωα-ω]+$", // Greek
"armenian": "^[\u0530-\u058F]+$", // Armenian
"bengali": "^[\u0980-\u09FF]+$", // Bengali
"tibetan": "^[\u0F00-\u0FFF]+$", // Tibetan
"myanmar": "^[\u1000-\u109F]+$", // Myanmar
"chinese": "^[\u4E00-\u9FFF]+$", // Chinese
"hebrew": "^[\u0590-\u05FF]+$", // Hebrew
"devanagari": "^[\u0900-\u097F]+$", // Devanagari
"hangul": "^[\uAC00-\uD7AF\u1100-\u11FF\u3130-\u318F]+$", // Hangul
"japanese": "^[\u4E00-\u9FFF\u3040-\u309F\u30A0-\u30FF]+$", // Kanji, Hiragana, Katakana
"thai": "^[\u0E00-\u0E7F]+$", // Thai
"ukrainian": "^[А-Яа-яЁёІЇЄҐіїєґ]+$", // Cyrillic with Ukrainian-specific characters
}
// localeWritingSystems is the set of locales and their writing systems
localeWritingSystems = map[string]string{
"ar": "arabic",
"ar-AE": "arabic",
"ar-BH": "arabic",
"ar-DZ": "arabic",
"ar-EG": "arabic",
"ar-IQ": "arabic",
"ar-JO": "arabic",
"ar-KW": "arabic",
"ar-LB": "arabic",
"ar-LY": "arabic",
"ar-MA": "arabic",
"ar-QA": "arabic",
"ar-QM": "arabic",
"ar-SA": "arabic",
"ar-SD": "arabic",
"ar-SY": "arabic",
"ar-TN": "arabic",
"ar-YE": "arabic",
"bg-BG": "cyrillic",
"bn": "bengali",
"cs-CZ": "latin_czech",
"da-DK": "latin_danish",
"de-DE": "latin_german",
"el-GR": "greek",
"en-AU": "latin",
"en-GB": "latin",
"en-HK": "latin",
"en-IN": "latin",
"en-NZ": "latin",
"en-US": "latin",
"en-ZA": "latin",
"en-ZM": "latin",
"eo": "latin_esperanto",
"es-ES": "latin_spanish",
"fa-IR": "arabic",
"fi-FI": "latin_finnish",
"fr-CA": "latin_french",
"fr-FR": "latin_french",
"he": "hebrew",
"hi-IN": "devanagari",
"hu-HU": "latin_hungarian",
"it-IT": "latin_italian",
"kk-KZ": "cyrillic",
"ko-KR": "hangul",
"ja-JP": "japanese",
"ku-IQ": "arabic",
"nb-NO": "latin_norwegian",
"nl-NL": "latin_dutch",
"nn-NO": "latin_norwegian",
"pl-PL": "latin_polish",
"pt-BR": "latin_portuguese",
"pt-PT": "latin_portuguese",
"ru-RU": "cyrillic",
"si-LK": "sinhala",
"sl-SI": "latin_slovenian",
"sk-SK": "latin_slovak",
"sr-RS": "cyrillic",
"sr-RS@latin": "latin_serbian",
"sv-SE": "latin_swedish",
"th-TH": "thai",
"tr-TR": "latin_turkish",
"uk-UA": "ukrainian",
}
)
// escapeRegexChars returns escaped regex special characters
func escapeRegexChars(str string) string {
var escIgnChars strings.Builder
for _, char := range str {
charStr := string(char)
escChar, ok := regexEscapes[charStr]
if ok {
escIgnChars.WriteString(escChar)
} else {
escIgnChars.WriteString(charStr)
}
}
return escIgnChars.String()
}
// A validator that checks if the string contains only letters (a-zA-Z).
//
// IsAlphaOpts is an optional struct that can be supplied with the following key(s):
//
// Ignore: is the string to be ignored e.g. " -" will ignore spaces and -'s.
//
// Locale: one of ("ar", "ar-AE", "ar-BH", "ar-DZ", "ar-EG", "ar-IQ", "ar-JO", "ar-KW", "ar-LB", "ar-LY", "ar-MA", "ar-QA", "ar-QM", "ar-SA", "ar-SD", "ar-SY", "ar-TN", "ar-YE", "bg-BG", "bn", "cs-CZ", "da-DK", "de-DE", "el-GR", "en-AU", "en-GB", "en-HK", "en-IN", "en-NZ", "en-US", "en-ZA", "en-ZM", "eo", "es-ES", "fa-IR", "fi-FI", "fr-CA", "fr-FR", "he", "hi-IN", "hu-HU", "it-IT", "kk-KZ", "ko-KR", "ja-JP", "ku-IQ", "nb-NO", "nl-NL", "nn-NO", "pl-PL", "pt-BR", "pt-PT", "ru-RU", "si-LK", "sl-SI", "sk-SK", "sr-RS", "sr-RS@latin", "sv-SE", "th-TH", "tr-TR", "uk-UA") and defaults to en-US if none is provided.
//
// isAlpha := govalidator.IsAlpha("hello", &govalidator.IsAlphaOpts{})
// fmt.Println(isAlpha) // true
// isAlpha := govalidator.IsAlpha("hello123", &govalidator.IsAlphaOpts{})
// fmt.Println(isAlpha) // false
func IsAlpha(str string, opts *IsAlphaOpts) bool {
if opts == nil {
opts = setIsAlphaOptsToDefault()
}
var (
re *regexp.Regexp
lenClsCharFromEnd = 3
)
if opts.Ignore == "" && opts.Locale == "" {
re = regexp.MustCompile(`^[a-zA-z]+$`)
}
if opts.Ignore == "" && opts.Locale != "" {
wrtSys, ok := localeWritingSystems[opts.Locale]
if !ok {
return false
}
re = regexp.MustCompile(writingSystemAlphaRegex[wrtSys])
}
if opts.Ignore != "" && opts.Locale == "" {
charsToIgn := escapeRegexChars(opts.Ignore)
rec := regexp.MustCompile(`^[a-zA-z` + charsToIgn + `]+$`)
re = rec
}
if opts.Ignore != "" && opts.Locale != "" {
charsToIgn := escapeRegexChars(opts.Ignore)
wrtSys := localeWritingSystems[opts.Locale]
wrtSysRe := writingSystemAlphaRegex[wrtSys]
divLen := len(wrtSysRe) - lenClsCharFromEnd
fstPrtRe, secPrtRe := wrtSysRe[:divLen], wrtSysRe[divLen:]
rec := regexp.MustCompile(fstPrtRe + charsToIgn + secPrtRe)
re = rec
}
return re.MatchString(str)
}
func setIsAlphaOptsToDefault() *IsAlphaOpts {
return &IsAlphaOpts{
Ignore: isAlphaOptsDefaultIgnore,
Locale: isAlphaOptsDefaultLocale,
}
}