Skip to content

Commit 76abb47

Browse files
committed
Update code with linting
1 parent 160142b commit 76abb47

File tree

1 file changed

+66
-41
lines changed

1 file changed

+66
-41
lines changed

lib/email_validator.dart

Lines changed: 66 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@ enum type { None, Alphabetic, Numeric, AlphaNumeric }
66

77
class EmailValidator {
88
static int _index = 0;
9-
static final _atomCharacters = "!#\$%&'*+-/=?^_`{|}~";
9+
static const String _atomCharacters = "!#\$%&'*+-/=?^_`{|}~";
1010
static type _domainType = type.None;
1111

12-
static bool _isDigit(c) {
13-
return (c.codeUnitAt(0) >= 48 && c.codeUnitAt(0) <= 57);
12+
static bool _isDigit(String c) {
13+
return c.codeUnitAt(0) >= 48 && c.codeUnitAt(0) <= 57;
1414
}
1515

16-
static bool _isLetter(c) {
16+
static bool _isLetter(String c) {
1717
return (c.codeUnitAt(0) >= 65 && c.codeUnitAt(0) <= 90) ||
1818
(c.codeUnitAt(0) >= 97 && c.codeUnitAt(0) <= 122);
1919
}
2020

21-
static bool _isLetterOrDigit(c) {
21+
static bool _isLetterOrDigit(String c) {
2222
return _isLetter(c) || _isDigit(c);
2323
}
2424

25-
static bool _isAtom(c, bool allowInternational) {
25+
static bool _isAtom(String c, bool allowInternational) {
2626
return c.codeUnitAt(0) < 128
27-
? _isLetterOrDigit(c) || _atomCharacters.indexOf(c) != -1
27+
? _isLetterOrDigit(c) || _atomCharacters.contains(c)
2828
: allowInternational;
2929
}
3030

31-
static bool _isDomain(c, bool allowInternational) {
31+
static bool _isDomain(String c, bool allowInternational) {
3232
if (c.codeUnitAt(0) < 128) {
3333
if (_isLetter(c) || c == '-') {
3434
_domainType = type.Alphabetic;
@@ -51,7 +51,7 @@ class EmailValidator {
5151
return false;
5252
}
5353

54-
static bool _isDomainStart(c, bool allowInternational) {
54+
static bool _isDomainStart(String c, bool allowInternational) {
5555
if (c.codeUnitAt(0) < 128) {
5656
if (_isLetter(c)) {
5757
_domainType = type.Alphabetic;
@@ -79,7 +79,7 @@ class EmailValidator {
7979
}
8080

8181
static bool _skipAtom(String text, bool allowInternational) {
82-
int startIndex = _index;
82+
final int startIndex = _index;
8383

8484
while (_index < text.length && _isAtom(text[_index], allowInternational))
8585
_index++;
@@ -88,7 +88,7 @@ class EmailValidator {
8888
}
8989

9090
static bool _skipSubDomain(String text, bool allowInternational) {
91-
int startIndex = _index;
91+
final int startIndex = _index;
9292

9393
if (!_isDomainStart(text[_index], allowInternational)) return false;
9494

@@ -102,22 +102,26 @@ class EmailValidator {
102102

103103
static bool _skipDomain(
104104
String text, bool allowTopLevelDomains, bool allowInternational) {
105-
if (!_skipSubDomain(text, allowInternational)) return false;
105+
if (!_skipSubDomain(text, allowInternational))
106+
return false;
106107

107108
if (_index < text.length && text[_index] == '.') {
108109
do {
109110
_index++;
110111

111-
if (_index == text.length) return false;
112+
if (_index == text.length)
113+
return false;
112114

113-
if (!_skipSubDomain(text, allowInternational)) return false;
115+
if (!_skipSubDomain(text, allowInternational))
116+
return false;
114117
} while (_index < text.length && text[_index] == '.');
115118
} else if (!allowTopLevelDomains) {
116119
return false;
117120
}
118121

119122
// Note: by allowing AlphaNumeric, we get away with not having to support punycode.
120-
if (_domainType == type.Numeric) return false;
123+
if (_domainType == type.Numeric)
124+
return false;
121125

122126
return true;
123127
}
@@ -135,15 +139,17 @@ class EmailValidator {
135139
if (text[_index] == '\\') {
136140
escaped = !escaped;
137141
} else if (!escaped) {
138-
if (text[_index] == '"') break;
142+
if (text[_index] == '"')
143+
break;
139144
} else {
140145
escaped = false;
141146
}
142147

143148
_index++;
144149
}
145150

146-
if (_index >= text.length || text[_index] != '"') return false;
151+
if (_index >= text.length || text[_index] != '"')
152+
return false;
147153

148154
_index++;
149155

@@ -154,7 +160,7 @@ class EmailValidator {
154160
int groups = 0;
155161

156162
while (_index < text.length && groups < 4) {
157-
int startIndex = _index;
163+
final int startIndex = _index;
158164
int value = 0;
159165

160166
while (_index < text.length &&
@@ -175,8 +181,8 @@ class EmailValidator {
175181
return groups == 4;
176182
}
177183

178-
static bool _isHexDigit(str) {
179-
var c = str.codeUnitAt(0);
184+
static bool _isHexDigit(String str) {
185+
final int c = str.codeUnitAt(0);
180186
return (c >= 65 && c <= 70) ||
181187
(c >= 97 && c <= 102) ||
182188
(c >= 48 && c <= 57);
@@ -204,32 +210,40 @@ class EmailValidator {
204210
while (_index < text.length) {
205211
int startIndex = _index;
206212

207-
while (_index < text.length && _isHexDigit(text[_index])) _index++;
213+
while (_index < text.length && _isHexDigit(text[_index]))
214+
_index++;
208215

209-
if (_index >= text.length) break;
216+
if (_index >= text.length)
217+
break;
210218

211219
if (_index > startIndex && colons > 2 && text[_index] == '.') {
212220
// IPv6v4
213221
_index = startIndex;
214222

215-
if (!_skipIPv4Literal(text)) return false;
223+
if (!_skipIPv4Literal(text))
224+
return false;
216225

217226
return compact ? colons < 6 : colons == 6;
218227
}
219228

220229
int count = _index - startIndex;
221-
if (count > 4) return false;
230+
if (count > 4)
231+
return false;
222232

223-
if (text[_index] != ':') break;
233+
if (text[_index] != ':')
234+
break;
224235

225236
startIndex = _index;
226-
while (_index < text.length && text[_index] == ':') _index++;
237+
while (_index < text.length && text[_index] == ':')
238+
_index++;
227239

228240
count = _index - startIndex;
229-
if (count > 2) return false;
241+
if (count > 2)
242+
return false;
230243

231244
if (count == 2) {
232-
if (compact) return false;
245+
if (compact)
246+
return false;
233247

234248
compact = true;
235249
colons += 2;
@@ -238,7 +252,8 @@ class EmailValidator {
238252
}
239253
}
240254

241-
if (colons < 2) return false;
255+
if (colons < 2)
256+
return false;
242257

243258
return compact ? colons < 7 : colons == 7;
244259
}
@@ -248,9 +263,11 @@ class EmailValidator {
248263
[bool allowTopLevelDomains = false, bool allowInternational = false]) {
249264
_index = 0;
250265

251-
if (email == null) throw new ArgumentError("email");
266+
if (email == null)
267+
throw new ArgumentError('email');
252268

253-
if (email.length == 0 || email.length >= 255) return false;
269+
if (email.isEmpty || email.length >= 255)
270+
return false;
254271

255272
// Local-part = Dot-string / Quoted-string
256273
// ; MAY be case-sensitive
@@ -268,11 +285,14 @@ class EmailValidator {
268285
while (email[_index] == '.') {
269286
_index++;
270287

271-
if (_index >= email.length) return false;
288+
if (_index >= email.length)
289+
return false;
272290

273-
if (!_skipAtom(email, allowInternational)) return false;
291+
if (!_skipAtom(email, allowInternational))
292+
return false;
274293

275-
if (_index >= email.length) return false;
294+
if (_index >= email.length)
295+
return false;
276296
}
277297
}
278298

@@ -291,17 +311,22 @@ class EmailValidator {
291311
_index++;
292312

293313
// we need at least 8 more characters
294-
if (_index + 8 >= email.length) return false;
314+
if (_index + 8 >= email.length)
315+
return false;
316+
317+
final String ipv6 = email.substring(_index - 1).toLowerCase();
295318

296-
var ipv6 = email.substring(_index - 1).toLowerCase();
297-
if (ipv6.contains("ipv6:")) {
298-
_index += "IPv6:".length;
299-
if (!_skipIPv6Literal(email)) return false;
319+
if (ipv6.contains('ipv6:')) {
320+
_index += 'IPv6:'.length;
321+
if (!_skipIPv6Literal(email))
322+
return false;
300323
} else {
301-
if (!_skipIPv4Literal(email)) return false;
324+
if (!_skipIPv4Literal(email))
325+
return false;
302326
}
303327

304-
if (_index >= email.length || email[_index++] != ']') return false;
328+
if (_index >= email.length || email[_index++] != ']')
329+
return false;
305330

306331
return _index == email.length;
307332
}

0 commit comments

Comments
 (0)