get_utils.dart
11.8 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import 'package:get/src/regex/regex.dart';
class GetUtils {
/// Checks if data is null.
static bool isNull(dynamic s) => s == null;
/// Checks if data is null or blank (empty or only contains whitespace).
static bool isNullOrBlank(dynamic s) {
if (isNull(s)) return true;
switch (s.runtimeType) {
case String:
case List:
case Map:
case Set:
case Iterable:
return s.isEmpty;
break;
default:
return s.toString() == 'null' || s.toString().trim().isEmpty;
}
}
/// Checks if string is int or double.
static bool isNum(String s) {
if (isNull(s)) return false;
return num.tryParse(s) is num ?? false;
}
/// Checks if string consist only numeric.
/// Numeric only doesnt accepting "." which double data type have
static bool isNumericOnly(String s) =>
RegexValidation.hasMatch(s, regex.numericOnly);
/// Checks if string consist only Alphabet. (No Whitespace)
static bool isAlphabetOnly(String s) =>
RegexValidation.hasMatch(s, regex.alphabetOnly);
/// Checks if string is boolean.
static bool isBool(String s) {
if (isNull(s)) return false;
return (s == 'true' || s == 'false');
}
/// Checks if string is an vector file.
static bool isVector(String s) => RegexValidation.hasMatch(s, regex.vector);
/// Checks if string is an image file.
static bool isImage(String s) => RegexValidation.hasMatch(s, regex.image);
/// Checks if string is an audio file.
static bool isAudio(String s) => RegexValidation.hasMatch(s, regex.audio);
/// Checks if string is an video file.
static bool isVideo(String s) => RegexValidation.hasMatch(s, regex.video);
/// Checks if string is an txt file.
static bool isTxt(String s) => RegexValidation.hasMatch(s, regex.txt);
/// Checks if string is an Doc file.
static bool isDocument(String s) => RegexValidation.hasMatch(s, regex.doc);
/// Checks if string is an Excel file.
static bool isExcel(String s) => RegexValidation.hasMatch(s, regex.excel);
/// Checks if string is an PPT file.
static bool isPPT(String s) => RegexValidation.hasMatch(s, regex.ppt);
/// Checks if string is an APK file.
static bool isAPK(String s) => RegexValidation.hasMatch(s, regex.apk);
/// Checks if string is an video file.
static bool isPDF(String s) => RegexValidation.hasMatch(s, regex.pdf);
/// Checks if string is an HTML file.
static bool isHTML(String s) => RegexValidation.hasMatch(s, regex.html);
/// Checks if string is URL.
static bool isURL(String s) => RegexValidation.hasMatch(s, regex.url);
/// Checks if string is email.
static bool isEmail(String s) => RegexValidation.hasMatch(s, regex.email);
/// Checks if string is phone number.
static bool isPhoneNumber(String s) =>
RegexValidation.hasMatch(s, regex.phone);
/// Checks if string is DateTime (UTC or Iso8601).
static bool isDateTime(String s) =>
RegexValidation.hasMatch(s, regex.basicDateTime);
/// Checks if string is MD5 hash.
static bool isMD5(String s) => RegexValidation.hasMatch(s, regex.md5);
/// Checks if string is SHA1 hash.
static bool isSHA1(String s) => RegexValidation.hasMatch(s, regex.sha1);
/// Checks if string is SHA256 hash.
static bool isSHA256(String s) => RegexValidation.hasMatch(s, regex.sha256);
/// Checks if string is ISBN 10 or 13.
static bool isISBN(String s) => RegexValidation.hasMatch(s, regex.isbn);
/// Checks if string is SSN (Social Security Number).
static bool isSSN(String s) => RegexValidation.hasMatch(s, regex.ssn);
/// Checks if string is binary.
static bool isBinary(String s) => RegexValidation.hasMatch(s, regex.binary);
/// Checks if string is IPv4.
static bool isIPv4(String s) => RegexValidation.hasMatch(s, regex.ipv4);
/// Checks if string is IPv6.
static bool isIPv6(String s) => RegexValidation.hasMatch(s, regex.ipv6);
/// Checks if string is hexadecimal.
/// Example: HexColor => #12F
static bool isHexadecimal(String s) =>
RegexValidation.hasMatch(s, regex.hexadecimal);
/// Checks if string is Palindrom.
static bool isPalindrom(String s) {
bool isPalindrom = true;
for (var i = 0; i < s.length; i++) {
if (s[i] != s[s.length - i - 1]) isPalindrom = false;
}
return isPalindrom;
}
/// Checks if all data have same value.
/// Example: 111111 -> true, wwwww -> true, [1,1,1,1] -> true
static bool isOneAKind(dynamic s) {
if ((s is String || s is List) && !isNullOrBlank(s)) {
var first = s[0];
var isOneAKind = true;
for (var i = 0; i < s.length; i++) {
if (s[i] != first) isOneAKind = false;
}
return isOneAKind;
}
if (s is int) {
String value = s.toString();
var first = value[0];
var isOneAKind = true;
for (var i = 0; i < value.length; i++) {
if (value[i] != first) isOneAKind = false;
}
return isOneAKind;
}
return false;
}
/// Checks if string is Passport No.
static bool isPassport(String s) =>
RegexValidation.hasMatch(s, regex.passport);
/// Checks if string is Currency.
static bool isCurrency(String s) =>
RegexValidation.hasMatch(s, regex.currency);
/// Checks if length of data is LOWER than maxLength.
static bool isLengthLowerThan(dynamic s, int maxLength) {
if (isNull(s)) return (maxLength <= 0) ? true : false;
switch (s.runtimeType) {
case String:
case List:
case Map:
case Set:
case Iterable:
return s.length < maxLength;
break;
case int:
return s.toString().length < maxLength;
break;
case double:
return s.toString().replaceAll('.', '').length < maxLength;
break;
default:
return false;
}
}
/// Checks if length of data is GREATER than maxLength.
static bool isLengthGreaterThan(dynamic s, int maxLength) {
if (isNull(s)) return false;
switch (s.runtimeType) {
case String:
case List:
case Map:
case Set:
case Iterable:
return s.length > maxLength;
break;
case int:
return s.toString().length > maxLength;
break;
case double:
return s.toString().replaceAll('.', '').length > maxLength;
break;
default:
return false;
}
}
/// Checks if length of data is GREATER OR EQUAL to maxLength.
static bool isLengthGreaterOrEqual(dynamic s, int maxLength) {
if (isNull(s)) return false;
switch (s.runtimeType) {
case String:
case List:
case Map:
case Set:
case Iterable:
return s.length >= maxLength;
break;
case int:
return s.toString().length >= maxLength;
break;
case double:
return s.toString().replaceAll('.', '').length >= maxLength;
break;
default:
return false;
}
}
/// Checks if length of data is LOWER OR EQUAL to maxLength.
static bool isLengthLowerOrEqual(dynamic s, int maxLength) {
if (isNull(s)) return false;
switch (s.runtimeType) {
case String:
case List:
case Map:
case Set:
case Iterable:
return s.length <= maxLength;
break;
case int:
return s.toString().length <= maxLength;
break;
case double:
return s.toString().replaceAll('.', '').length <= maxLength;
default:
return false;
}
}
/// Checks if length of data is EQUAL to maxLength.
static bool isLengthEqualTo(dynamic s, int maxLength) {
if (isNull(s)) return false;
switch (s.runtimeType) {
case String:
case List:
case Map:
case Set:
case Iterable:
return s.length == maxLength;
break;
case int:
return s.toString().length == maxLength;
break;
case double:
return s.toString().replaceAll('.', '').length == maxLength;
break;
default:
return false;
}
}
/// Checks if length of data is BETWEEN minLength to maxLength.
static bool isLengthBetween(dynamic s, int minLength, int maxLength) {
if (isNull(s)) return false;
return isLengthGreaterOrEqual(s, minLength) &&
isLengthLowerOrEqual(s, maxLength);
}
/// Checks if a contains b (Treating or interpreting upper- and lowercase letters as being the same).
static bool isCaseInsensitiveContains(String a, String b) =>
a.toLowerCase().contains(b.toLowerCase());
/// Checks if a contains b or b contains a (Treating or interpreting upper- and lowercase letters as being the same).
static bool isCaseInsensitiveContainsAny(String a, String b) {
String lowA = a.toLowerCase();
String lowB = b.toLowerCase();
return lowA.contains(lowB) || lowB.contains(lowA);
}
/// Checks if num a LOWER than num b.
static bool isLowerThan(num a, num b) => a < b;
/// Checks if num a GREATER than num b.
static bool isGreaterThan(num a, num b) => a > b;
/// Checks if num a EQUAL than num b.
static bool isEqual(num a, num b) => a == b;
/// Checks if the cpf is valid.
static bool isCpf(String cpf) {
if (cpf == null) return false;
// get only the numbers
var numbers = cpf.replaceAll(RegExp(r'[^0-9]'), '');
// Test if the CPF has 11 digits
if (numbers.length != 11) return false;
// Test if all CPF digits are the same
if (RegExp(r'^(\d)\1*$').hasMatch(numbers)) return false;
// split the digits
List<int> digits =
numbers.split('').map((String d) => int.parse(d)).toList();
// Calculate the first verifier digit
var calcDv1 = 0;
for (var i in Iterable<int>.generate(9, (i) => 10 - i)) {
calcDv1 += digits[10 - i] * i;
}
calcDv1 %= 11;
var dv1 = calcDv1 < 2 ? 0 : 11 - calcDv1;
// Tests the first verifier digit
if (digits[9] != dv1) return false;
// Calculate the second verifier digit
var calcDv2 = 0;
for (var i in Iterable<int>.generate(10, (i) => 11 - i)) {
calcDv2 += digits[11 - i] * i;
}
calcDv2 %= 11;
var dv2 = calcDv2 < 2 ? 0 : 11 - calcDv2;
// Test the second verifier digit
if (digits[10] != dv2) return false;
return true;
}
/// Capitalize each word inside string
/// Example: your name => Your Name, your name => Your name
///
/// If First Only is `true`, the only letter get uppercase is the first letter
static String capitalize(String s, {bool firstOnly = false}) {
if (isNullOrBlank(s)) return null;
if (firstOnly) return capitalizeFirst(s);
List lst = s.split(' ');
String newStr = '';
for (var s in lst) newStr += capitalizeFirst(s);
return newStr;
}
/// Uppercase first letter inside string and let the others lowercase
/// Example: your name => Your name
static String capitalizeFirst(String s) {
if (isNullOrBlank(s)) return null;
return s[0].toUpperCase() + s.substring(1).toLowerCase();
}
/// Remove all whitespace inside string
/// Example: your name => yourname
static String removeAllWhitespace(String s) {
if (isNullOrBlank(s)) return null;
return s.replaceAll(' ', '');
}
/// Camelcase string
/// Example: your name => yourName
static String camelCase(String s) {
if (isNullOrBlank(s)) return null;
return s[0].toLowerCase() + removeAllWhitespace(capitalize(s)).substring(1);
}
/// Extract numeric value of string
/// Example: OTP 12312 27/04/2020 => 1231227042020ß
/// If firstword only is true, then the example return is "12312" (first found numeric word)
static String numericOnly(String s, {bool firstWordOnly = false}) {
String numericOnlyStr = '';
for (var i = 0; i < s.length; i++) {
if (isNumericOnly(s[i])) numericOnlyStr += s[i];
if (firstWordOnly && numericOnlyStr.isNotEmpty && s[i] == " ") break;
}
return numericOnlyStr;
}
static Regex regex = Regex();
}
class RegexValidation {
/// Returns whether the pattern has a match in the string [input].
static bool hasMatch(String s, Pattern p) =>
(s == null) ? false : RegExp(p).hasMatch(s);
}