1
1
import 'helpers.dart' ;
2
2
3
3
RegExp _email = RegExp (
4
- r"^((([a-z]|\d|[!#\$%&'*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$" );
4
+ r"^((([a-z]|\d|[!#\$%&'*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$" ,
5
+ );
5
6
6
7
RegExp _ipv4Maybe = RegExp (r'^(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)$' );
7
8
RegExp _ipv6 =
8
9
RegExp (r'^::|^::1|^([a-fA-F0-9]{1,4}::?){1,7}([a-fA-F0-9]{1,4})$' );
9
10
10
11
RegExp _creditCard = RegExp (
11
- r'^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$' );
12
+ r'^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6(?:011|5[0-9][0-9])[0-9]{12}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11})$' ,
13
+ );
12
14
13
15
int _maxUrlLength = 2083 ;
14
16
@@ -25,22 +27,31 @@ bool isEmail(String str) {
25
27
/// * [allowUnderscore] sets if underscores are allowed
26
28
/// * [hostWhitelist] sets the list of allowed hosts
27
29
/// * [hostBlacklist] sets the list of disallowed hosts
28
- bool isURL (String ? str,
29
- {List <String ?> protocols = const ['http' , 'https' , 'ftp' ],
30
- bool requireTld = true ,
31
- bool requireProtocol = false ,
32
- bool allowUnderscore = false ,
33
- List <String > hostWhitelist = const [],
34
- List <String > hostBlacklist = const []}) {
30
+ bool isURL (
31
+ String ? str, {
32
+ List <String ?> protocols = const ['http' , 'https' , 'ftp' ],
33
+ bool requireTld = true ,
34
+ bool requireProtocol = false ,
35
+ bool allowUnderscore = false ,
36
+ List <String > hostWhitelist = const [],
37
+ List <String > hostBlacklist = const [],
38
+ }) {
35
39
if (str == null ||
36
40
str.isEmpty ||
37
41
str.length > _maxUrlLength ||
38
42
str.startsWith ('mailto:' )) {
39
43
return false ;
40
44
}
41
45
int port;
42
- String ? protocol, auth, user;
43
- String host, hostname, portStr, path, query, hash;
46
+ String ? protocol;
47
+ String ? auth;
48
+ String ? user;
49
+ String host;
50
+ String hostname;
51
+ String portStr;
52
+ String path;
53
+ String query;
54
+ String hash;
44
55
45
56
// check protocol
46
57
var split = str.split ('://' );
@@ -83,7 +94,7 @@ bool isURL(String? str,
83
94
if (split.length > 1 ) {
84
95
auth = shift (split);
85
96
if (auth? .contains (':' ) ?? false ) {
86
- user = shift (auth! .split (':' ))! ;
97
+ user = shift (auth! .split (':' ));
87
98
if (! RegExp (r'^\S+$' ).hasMatch (user)) {
88
99
return false ;
89
100
}
@@ -110,8 +121,11 @@ bool isURL(String? str,
110
121
}
111
122
112
123
if (! isIP (host, null ) &&
113
- ! isFQDN (host,
114
- requireTld: requireTld, allowUnderscores: allowUnderscore) &&
124
+ ! isFQDN (
125
+ host,
126
+ requireTld: requireTld,
127
+ allowUnderscores: allowUnderscore,
128
+ ) &&
115
129
host != 'localhost' ) {
116
130
return false ;
117
131
}
@@ -137,7 +151,7 @@ bool isIP(String? str, int? version) {
137
151
if (! _ipv4Maybe.hasMatch (str! )) {
138
152
return false ;
139
153
}
140
- var parts = str.split ('.' );
154
+ final parts = str.split ('.' );
141
155
parts.sort ((a, b) => int .parse (a) - int .parse (b));
142
156
return int .parse (parts[3 ]) <= 255 ;
143
157
}
@@ -148,17 +162,20 @@ bool isIP(String? str, int? version) {
148
162
///
149
163
/// * [requireTld] sets if TLD is required
150
164
/// * [allowUnderscore] sets if underscores are allowed
151
- bool isFQDN (String str,
152
- {bool requireTld = true , bool allowUnderscores = false }) {
153
- var parts = str.split ('.' );
165
+ bool isFQDN (
166
+ String str, {
167
+ bool requireTld = true ,
168
+ bool allowUnderscores = false ,
169
+ }) {
170
+ final parts = str.split ('.' );
154
171
if (requireTld) {
155
- var tld = parts.removeLast ();
172
+ final tld = parts.removeLast ();
156
173
if (parts.isEmpty || ! RegExp (r'^[a-z]{2,}$' ).hasMatch (tld)) {
157
174
return false ;
158
175
}
159
176
}
160
177
161
- for (var part in parts) {
178
+ for (final part in parts) {
162
179
if (allowUnderscores) {
163
180
if (part.contains ('__' )) {
164
181
return false ;
@@ -178,7 +195,7 @@ bool isFQDN(String str,
178
195
179
196
/// check if the string is a credit card
180
197
bool isCreditCard (String str) {
181
- var sanitized = str.replaceAll (RegExp (r '[^0-9]+' ), '' );
198
+ final sanitized = str.replaceAll (RegExp ('[^0-9]+' ), '' );
182
199
if (! _creditCard.hasMatch (sanitized)) {
183
200
return false ;
184
201
}
@@ -189,13 +206,13 @@ bool isCreditCard(String str) {
189
206
var shouldDouble = false ;
190
207
191
208
for (var i = sanitized.length - 1 ; i >= 0 ; i-- ) {
192
- digit = sanitized.substring (i, ( i + 1 ) );
209
+ digit = sanitized.substring (i, i + 1 );
193
210
var tmpNum = int .parse (digit);
194
211
195
212
if (shouldDouble == true ) {
196
213
tmpNum *= 2 ;
197
214
if (tmpNum >= 10 ) {
198
- sum += (( tmpNum % 10 ) + 1 ) ;
215
+ sum += (tmpNum % 10 ) + 1 ;
199
216
} else {
200
217
sum += tmpNum;
201
218
}
0 commit comments