Nipodemos
Committed by GitHub

Merge pull request #623 from Grohden/fix/switch-types

Fix for switch types usages in GetUtils
1 import '../../../get.dart'; 1 import '../../../get.dart';
2 2
  3 +/// Returns whether a dynamic value PROBABLY
  4 +/// has the isEmpty getter/method by checking
  5 +/// standard dart types that contains it.
  6 +///
  7 +/// This is here to for the 'DRY'
  8 +bool _hasIsEmpty(dynamic value) {
  9 + return value is Iterable || value is String || value is Map;
  10 +}
  11 +
  12 +/// Returns whether a dynamic value PROBABLY
  13 +/// has the length getter/method by checking
  14 +/// standard dart types that contains it.
  15 +///
  16 +/// This is here to for the 'DRY'
  17 +bool _hasLength(dynamic value) {
  18 + return value is Iterable || value is String || value is Map;
  19 +}
  20 +
  21 +/// Obtains a length of a dynamic value
  22 +/// by previously validating it's type
  23 +///
  24 +/// Note: if [value] is double/int
  25 +/// it will be taking the .toString
  26 +/// length of the given value.
  27 +///
  28 +/// Note 2: **this may return null!**
  29 +///
  30 +/// Note 3: null [value] returns null.
  31 +int _obtainDynamicLength(dynamic value) {
  32 + if (value == null) {
  33 + // ignore: avoid_returning_null
  34 + return null;
  35 + }
  36 +
  37 + if (_hasLength(value)) {
  38 + return value.length as int;
  39 + }
  40 +
  41 + if (value is int) {
  42 + return value.toString().length;
  43 + }
  44 +
  45 + if (value is double) {
  46 + return value.toString().replaceAll('.', '').length;
  47 + }
  48 +
  49 + // ignore: avoid_returning_null
  50 + return null;
  51 +}
  52 +
3 class GetUtils { 53 class GetUtils {
  54 + GetUtils._();
  55 +
4 /// Checks if data is null. 56 /// Checks if data is null.
5 - static bool isNull(dynamic s) => s == null; 57 + static bool isNull(dynamic value) => value == null;
6 58
7 /// In dart2js (in flutter v1.17) a var by default is undefined. 59 /// In dart2js (in flutter v1.17) a var by default is undefined.
8 /// *Use this only if you are in version <- 1.17*. 60 /// *Use this only if you are in version <- 1.17*.
@@ -13,20 +65,27 @@ class GetUtils { @@ -13,20 +65,27 @@ class GetUtils {
13 static dynamic nil(dynamic s) => s == null ? null : s; 65 static dynamic nil(dynamic s) => s == null ? null : s;
14 66
15 /// Checks if data is null or blank (empty or only contains whitespace). 67 /// Checks if data is null or blank (empty or only contains whitespace).
16 - static bool isNullOrBlank(dynamic s) {  
17 - if (isNull(s)) return true; 68 + static bool isNullOrBlank(dynamic value) {
  69 + if (isNull(value)) {
  70 + return true;
  71 + }
18 72
19 - if (s is String || s is List || s is Map || s is Set || s is Iterable) {  
20 - return s.isEmpty as bool;  
21 - } else {  
22 - return s.toString() == 'null' || s.toString().trim().isEmpty; 73 + if(value is String) {
  74 + return value.toString().trim().isEmpty;
23 } 75 }
  76 +
  77 + // Pretty sure that isNullOrBlank should't be validating
  78 + // iterables... but I'm going to keep this for compatibility.
  79 + return _hasIsEmpty(value) ? value.isEmpty as bool : false;
24 } 80 }
25 81
26 /// Checks if string is int or double. 82 /// Checks if string is int or double.
27 - static bool isNum(String s) {  
28 - if (isNull(s)) return false;  
29 - return num.tryParse(s) is num ?? false; 83 + static bool isNum(String value) {
  84 + if (isNull(value)) {
  85 + return false;
  86 + }
  87 +
  88 + return num.tryParse(value) is num;
30 } 89 }
31 90
32 /// Checks if string consist only numeric. 91 /// Checks if string consist only numeric.
@@ -37,9 +96,12 @@ class GetUtils { @@ -37,9 +96,12 @@ class GetUtils {
37 static bool isAlphabetOnly(String s) => hasMatch(s, r'^[a-zA-Z]+$'); 96 static bool isAlphabetOnly(String s) => hasMatch(s, r'^[a-zA-Z]+$');
38 97
39 /// Checks if string is boolean. 98 /// Checks if string is boolean.
40 - static bool isBool(String s) {  
41 - if (isNull(s)) return false;  
42 - return (s == 'true' || s == 'false'); 99 + static bool isBool(String value) {
  100 + if (isNull(value)) {
  101 + return false;
  102 + }
  103 +
  104 + return (value == 'true' || value == 'false');
43 } 105 }
44 106
45 /// Checks if string is an video file. 107 /// Checks if string is an video file.
@@ -80,18 +142,21 @@ class GetUtils { @@ -80,18 +142,21 @@ class GetUtils {
80 /// Checks if string is an powerpoint file. 142 /// Checks if string is an powerpoint file.
81 static bool isPPT(String filePath) { 143 static bool isPPT(String filePath) {
82 final ext = filePath.toLowerCase(); 144 final ext = filePath.toLowerCase();
  145 +
83 return ext.endsWith(".ppt") || ext.endsWith(".pptx"); 146 return ext.endsWith(".ppt") || ext.endsWith(".pptx");
84 } 147 }
85 148
86 /// Checks if string is an word file. 149 /// Checks if string is an word file.
87 static bool isWord(String filePath) { 150 static bool isWord(String filePath) {
88 final ext = filePath.toLowerCase(); 151 final ext = filePath.toLowerCase();
  152 +
89 return ext.endsWith(".doc") || ext.endsWith(".docx"); 153 return ext.endsWith(".doc") || ext.endsWith(".docx");
90 } 154 }
91 155
92 /// Checks if string is an excel file. 156 /// Checks if string is an excel file.
93 static bool isExcel(String filePath) { 157 static bool isExcel(String filePath) {
94 final ext = filePath.toLowerCase(); 158 final ext = filePath.toLowerCase();
  159 +
95 return ext.endsWith(".xls") || ext.endsWith(".xlsx"); 160 return ext.endsWith(".xls") || ext.endsWith(".xlsx");
96 } 161 }
97 162
@@ -177,42 +242,50 @@ class GetUtils { @@ -177,42 +242,50 @@ class GetUtils {
177 hasMatch(s, r'^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$'); 242 hasMatch(s, r'^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$');
178 243
179 /// Checks if string is Palindrom. 244 /// Checks if string is Palindrom.
180 - static bool isPalindrom(String s) {  
181 - final cleanString = s 245 + static bool isPalindrom(String string) {
  246 + final cleanString = string
182 .toLowerCase() 247 .toLowerCase()
183 .replaceAll(RegExp(r"\s+"), '') 248 .replaceAll(RegExp(r"\s+"), '')
184 .replaceAll(RegExp(r"[^0-9a-zA-Z]+"), ""); 249 .replaceAll(RegExp(r"[^0-9a-zA-Z]+"), "");
185 - var isPalindrom = true; 250 +
186 for (var i = 0; i < cleanString.length; i++) { 251 for (var i = 0; i < cleanString.length; i++) {
187 if (cleanString[i] != cleanString[cleanString.length - i - 1]) { 252 if (cleanString[i] != cleanString[cleanString.length - i - 1]) {
188 - isPalindrom = false; 253 + return false;
189 } 254 }
190 } 255 }
191 - return isPalindrom; 256 +
  257 + return true;
192 } 258 }
193 259
194 /// Checks if all data have same value. 260 /// Checks if all data have same value.
195 /// Example: 111111 -> true, wwwww -> true, [1,1,1,1] -> true 261 /// Example: 111111 -> true, wwwww -> true, [1,1,1,1] -> true
196 - static bool isOneAKind(dynamic s) {  
197 - if ((s is String || s is List) && !isNullOrBlank(s)) {  
198 - var first = s[0];  
199 - var isOneAKind = true;  
200 - var len = s.length as num; 262 + static bool isOneAKind(dynamic value) {
  263 + if ((value is String || value is List) && !isNullOrBlank(value)) {
  264 + final first = value[0];
  265 + final len = value.length as num;
  266 +
201 for (var i = 0; i < len; i++) { 267 for (var i = 0; i < len; i++) {
202 - if (s[i] != first) isOneAKind = false; 268 + if (value[i] != first) {
  269 + return false;
203 } 270 }
204 - return isOneAKind;  
205 } 271 }
206 272
207 - if (s is int) {  
208 - var value = s.toString();  
209 - var first = value[0];  
210 - var isOneAKind = true;  
211 - for (var i = 0; i < value.length; i++) {  
212 - if (value[i] != first) isOneAKind = false; 273 + return true;
213 } 274 }
214 - return isOneAKind; 275 +
  276 + if (value is int) {
  277 + final stringValue = value.toString();
  278 + final first = stringValue[0];
  279 +
  280 + for (var i = 0; i < stringValue.length; i++) {
  281 + if (stringValue[i] != first) {
  282 + return false;
  283 + }
  284 + }
  285 +
  286 + return true;
215 } 287 }
  288 +
216 return false; 289 return false;
217 } 290 }
218 291
@@ -224,124 +297,96 @@ class GetUtils { @@ -224,124 +297,96 @@ class GetUtils {
224 static bool isCurrency(String s) => hasMatch(s, 297 static bool isCurrency(String s) => hasMatch(s,
225 r'^(S?\$|\₩|Rp|\¥|\€|\₹|\₽|fr|R\$|R)?[ ]?[-]?([0-9]{1,3}[,.]([0-9]{3}[,.])*[0-9]{3}|[0-9]+)([,.][0-9]{1,2})?( ?(USD?|AUD|NZD|CAD|CHF|GBP|CNY|EUR|JPY|IDR|MXN|NOK|KRW|TRY|INR|RUB|BRL|ZAR|SGD|MYR))?$'); 298 r'^(S?\$|\₩|Rp|\¥|\€|\₹|\₽|fr|R\$|R)?[ ]?[-]?([0-9]{1,3}[,.]([0-9]{3}[,.])*[0-9]{3}|[0-9]+)([,.][0-9]{1,2})?( ?(USD?|AUD|NZD|CAD|CHF|GBP|CNY|EUR|JPY|IDR|MXN|NOK|KRW|TRY|INR|RUB|BRL|ZAR|SGD|MYR))?$');
226 299
227 - /// Checks if length of data is LOWER than maxLength.  
228 - static bool isLengthLowerThan(dynamic s, int maxLength) {  
229 - if (isNull(s)) return (maxLength <= 0) ? true : false;  
230 - switch (s.runtimeType as Type) {  
231 - case String:  
232 - case List:  
233 - case Map:  
234 - case Set:  
235 - case Iterable:  
236 - return (s.length as num) < maxLength;  
237 - case int:  
238 - return s.toString().length < maxLength;  
239 - case double:  
240 - return s.toString().replaceAll('.', '').length < maxLength;  
241 - default: 300 + /// Checks if length of data is GREATER than maxLength.
  301 + static bool isLengthGreaterThan(dynamic value, int maxLength) {
  302 + final length = _obtainDynamicLength(value);
  303 +
  304 + if (length == null) {
242 return false; 305 return false;
243 } 306 }
  307 +
  308 + return length > maxLength;
244 } 309 }
245 310
246 - /// Checks if length of data is GREATER than maxLength.  
247 - static bool isLengthGreaterThan(dynamic s, int maxLength) {  
248 - if (isNull(s)) return false;  
249 - switch (s.runtimeType as Type) {  
250 - case String:  
251 - case List:  
252 - case Map:  
253 - case Set:  
254 - case Iterable:  
255 - return (s.length as num) > maxLength;  
256 - case int:  
257 - return s.toString().length > maxLength;  
258 - case double:  
259 - return s.toString().replaceAll('.', '').length > maxLength;  
260 - default: 311 + /// Checks if length of data is GREATER OR EQUAL to maxLength.
  312 + static bool isLengthGreaterOrEqual(dynamic value, int maxLength) {
  313 + final length = _obtainDynamicLength(value);
  314 +
  315 + if (length == null) {
261 return false; 316 return false;
262 } 317 }
  318 +
  319 + return length >= maxLength;
263 } 320 }
264 321
265 - /// Checks if length of data is GREATER OR EQUAL to maxLength.  
266 - static bool isLengthGreaterOrEqual(dynamic s, int maxLength) {  
267 - if (isNull(s)) return false;  
268 - switch (s.runtimeType as Type) {  
269 - case String:  
270 - case List:  
271 - case Map:  
272 - case Set:  
273 - case Iterable:  
274 - return (s.length as num) >= maxLength;  
275 - break;  
276 - case int:  
277 - return s.toString().length >= maxLength;  
278 - break;  
279 - case double:  
280 - return s.toString().replaceAll('.', '').length >= maxLength;  
281 - break;  
282 - default: 322 + /// Checks if length of data is LOWER than maxLength.
  323 + ///
  324 + /// This method is deprecated, use [isLengthLessThan] instead
  325 + @deprecated
  326 + static bool isLengthLowerThan(dynamic value, int maxLength) =>
  327 + isLengthLessThan(value, maxLength);
  328 +
  329 + /// Checks if length of data is LESS than maxLength.
  330 + static bool isLengthLessThan(dynamic value, int maxLength) {
  331 + final length = _obtainDynamicLength(value);
  332 + if (length == null) {
283 return false; 333 return false;
284 } 334 }
  335 +
  336 + return length < maxLength;
285 } 337 }
286 338
287 /// Checks if length of data is LOWER OR EQUAL to maxLength. 339 /// Checks if length of data is LOWER OR EQUAL to maxLength.
288 - static bool isLengthLowerOrEqual(dynamic s, int maxLength) {  
289 - if (isNull(s)) return false;  
290 - switch (s.runtimeType as Type) {  
291 - case String:  
292 - case List:  
293 - case Map:  
294 - case Set:  
295 - case Iterable:  
296 - return (s.length as num) <= maxLength;  
297 - case int:  
298 - return s.toString().length <= maxLength;  
299 - case double:  
300 - return s.toString().replaceAll('.', '').length <= maxLength;  
301 - default: 340 + ///
  341 + /// This method is deprecated, use [isLengthLessOrEqual] instead
  342 + @deprecated
  343 + static bool isLengthLowerOrEqual(dynamic value, int maxLength) =>
  344 + isLengthLessOrEqual(value, maxLength);
  345 +
  346 + /// Checks if length of data is LESS OR EQUAL to maxLength.
  347 + static bool isLengthLessOrEqual(dynamic value, int maxLength) {
  348 + final length = _obtainDynamicLength(value);
  349 +
  350 + if (length == null) {
302 return false; 351 return false;
303 } 352 }
  353 +
  354 + return length <= maxLength;
304 } 355 }
305 356
306 /// Checks if length of data is EQUAL to maxLength. 357 /// Checks if length of data is EQUAL to maxLength.
307 - static bool isLengthEqualTo(dynamic s, int maxLength) {  
308 - if (isNull(s)) return false;  
309 - switch (s.runtimeType as Type) {  
310 - case String:  
311 - case List:  
312 - case Map:  
313 - case Set:  
314 - case Iterable:  
315 - return s.length == maxLength;  
316 - break;  
317 - case int:  
318 - return s.toString().length == maxLength;  
319 - break;  
320 - case double:  
321 - return s.toString().replaceAll('.', '').length == maxLength;  
322 - break;  
323 - default: 358 + static bool isLengthEqualTo(dynamic value, int otherLength) {
  359 + final length = _obtainDynamicLength(value);
  360 +
  361 + if (length == null) {
324 return false; 362 return false;
325 } 363 }
  364 +
  365 + return length == otherLength;
326 } 366 }
327 367
328 /// Checks if length of data is BETWEEN minLength to maxLength. 368 /// Checks if length of data is BETWEEN minLength to maxLength.
329 - static bool isLengthBetween(dynamic s, int minLength, int maxLength) {  
330 - if (isNull(s)) return false;  
331 - return isLengthGreaterOrEqual(s, minLength) &&  
332 - isLengthLowerOrEqual(s, maxLength); 369 + static bool isLengthBetween(dynamic value, int minLength, int maxLength) {
  370 + if (isNull(value)) {
  371 + return false;
  372 + }
  373 +
  374 + return isLengthGreaterOrEqual(value, minLength) &&
  375 + isLengthLowerOrEqual(value, maxLength);
333 } 376 }
334 377
335 /// Checks if a contains b (Treating or interpreting upper- and lowercase 378 /// Checks if a contains b (Treating or interpreting upper- and lowercase
336 /// letters as being the same). 379 /// letters as being the same).
337 - static bool isCaseInsensitiveContains(String a, String b) =>  
338 - a.toLowerCase().contains(b.toLowerCase()); 380 + static bool isCaseInsensitiveContains(String a, String b) {
  381 + return a.toLowerCase().contains(b.toLowerCase());
  382 + }
339 383
340 /// Checks if a contains b or b contains a (Treating or 384 /// Checks if a contains b or b contains a (Treating or
341 /// interpreting upper- and lowercase letters as being the same). 385 /// interpreting upper- and lowercase letters as being the same).
342 static bool isCaseInsensitiveContainsAny(String a, String b) { 386 static bool isCaseInsensitiveContainsAny(String a, String b) {
343 final lowA = a.toLowerCase(); 387 final lowA = a.toLowerCase();
344 final lowB = b.toLowerCase(); 388 final lowB = b.toLowerCase();
  389 +
345 return lowA.contains(lowB) || lowB.contains(lowA); 390 return lowA.contains(lowB) || lowB.contains(lowA);
346 } 391 }
347 392
@@ -356,19 +401,25 @@ class GetUtils { @@ -356,19 +401,25 @@ class GetUtils {
356 401
357 //Check if num is a cnpj 402 //Check if num is a cnpj
358 static bool isCnpj(String cnpj) { 403 static bool isCnpj(String cnpj) {
359 - if (cnpj == null) return false; 404 + if (cnpj == null) {
  405 + return false;
  406 + }
360 407
361 // Obter somente os números do CNPJ 408 // Obter somente os números do CNPJ
362 - var numbers = cnpj.replaceAll(RegExp(r'[^0-9]'), ''); 409 + final numbers = cnpj.replaceAll(RegExp(r'[^0-9]'), '');
363 410
364 // Testar se o CNPJ possui 14 dígitos 411 // Testar se o CNPJ possui 14 dígitos
365 - if (numbers.length != 14) return false; 412 + if (numbers.length != 14) {
  413 + return false;
  414 + }
366 415
367 // Testar se todos os dígitos do CNPJ são iguais 416 // Testar se todos os dígitos do CNPJ são iguais
368 - if (RegExp(r'^(\d)\1*$').hasMatch(numbers)) return false; 417 + if (RegExp(r'^(\d)\1*$').hasMatch(numbers)) {
  418 + return false;
  419 + }
369 420
370 // Dividir dígitos 421 // Dividir dígitos
371 - var digits = numbers.split('').map(int.parse).toList(); 422 + final digits = numbers.split('').map(int.parse).toList();
372 423
373 // Calcular o primeiro dígito verificador 424 // Calcular o primeiro dígito verificador
374 var calcDv1 = 0; 425 var calcDv1 = 0;
@@ -377,10 +428,12 @@ class GetUtils { @@ -377,10 +428,12 @@ class GetUtils {
377 calcDv1 += digits[j++] * i; 428 calcDv1 += digits[j++] * i;
378 } 429 }
379 calcDv1 %= 11; 430 calcDv1 %= 11;
380 - var dv1 = calcDv1 < 2 ? 0 : 11 - calcDv1; 431 + final dv1 = calcDv1 < 2 ? 0 : 11 - calcDv1;
381 432
382 // Testar o primeiro dígito verificado 433 // Testar o primeiro dígito verificado
383 - if (digits[12] != dv1) return false; 434 + if (digits[12] != dv1) {
  435 + return false;
  436 + }
384 437
385 // Calcular o segundo dígito verificador 438 // Calcular o segundo dígito verificador
386 var calcDv2 = 0; 439 var calcDv2 = 0;
@@ -389,27 +442,35 @@ class GetUtils { @@ -389,27 +442,35 @@ class GetUtils {
389 calcDv2 += digits[j++] * i; 442 calcDv2 += digits[j++] * i;
390 } 443 }
391 calcDv2 %= 11; 444 calcDv2 %= 11;
392 - var dv2 = calcDv2 < 2 ? 0 : 11 - calcDv2; 445 + final dv2 = calcDv2 < 2 ? 0 : 11 - calcDv2;
393 446
394 // Testar o segundo dígito verificador 447 // Testar o segundo dígito verificador
395 - if (digits[13] != dv2) return false; 448 + if (digits[13] != dv2) {
  449 + return false;
  450 + }
396 451
397 return true; 452 return true;
398 } 453 }
399 454
400 /// Checks if the cpf is valid. 455 /// Checks if the cpf is valid.
401 static bool isCpf(String cpf) { 456 static bool isCpf(String cpf) {
402 - if (cpf == null) return false; 457 + if (cpf == null) {
  458 + return false;
  459 + }
403 460
404 // get only the numbers 461 // get only the numbers
405 - var numbers = cpf.replaceAll(RegExp(r'[^0-9]'), ''); 462 + final numbers = cpf.replaceAll(RegExp(r'[^0-9]'), '');
406 // Test if the CPF has 11 digits 463 // Test if the CPF has 11 digits
407 - if (numbers.length != 11) return false; 464 + if (numbers.length != 11) {
  465 + return false;
  466 + }
408 // Test if all CPF digits are the same 467 // Test if all CPF digits are the same
409 - if (RegExp(r'^(\d)\1*$').hasMatch(numbers)) return false; 468 + if (RegExp(r'^(\d)\1*$').hasMatch(numbers)) {
  469 + return false;
  470 + }
410 471
411 // split the digits 472 // split the digits
412 - var digits = numbers.split('').map(int.parse).toList(); 473 + final digits = numbers.split('').map(int.parse).toList();
413 474
414 // Calculate the first verifier digit 475 // Calculate the first verifier digit
415 var calcDv1 = 0; 476 var calcDv1 = 0;
@@ -417,10 +478,13 @@ class GetUtils { @@ -417,10 +478,13 @@ class GetUtils {
417 calcDv1 += digits[10 - i] * i; 478 calcDv1 += digits[10 - i] * i;
418 } 479 }
419 calcDv1 %= 11; 480 calcDv1 %= 11;
420 - var dv1 = calcDv1 < 2 ? 0 : 11 - calcDv1; 481 +
  482 + final dv1 = calcDv1 < 2 ? 0 : 11 - calcDv1;
421 483
422 // Tests the first verifier digit 484 // Tests the first verifier digit
423 - if (digits[9] != dv1) return false; 485 + if (digits[9] != dv1) {
  486 + return false;
  487 + }
424 488
425 // Calculate the second verifier digit 489 // Calculate the second verifier digit
426 var calcDv2 = 0; 490 var calcDv2 = 0;
@@ -429,48 +493,64 @@ class GetUtils { @@ -429,48 +493,64 @@ class GetUtils {
429 } 493 }
430 calcDv2 %= 11; 494 calcDv2 %= 11;
431 495
432 - var dv2 = calcDv2 < 2 ? 0 : 11 - calcDv2; 496 + final dv2 = calcDv2 < 2 ? 0 : 11 - calcDv2;
433 497
434 // Test the second verifier digit 498 // Test the second verifier digit
435 - if (digits[10] != dv2) return false; 499 + if (digits[10] != dv2) {
  500 + return false;
  501 + }
436 502
437 return true; 503 return true;
438 } 504 }
439 505
440 /// Capitalize each word inside string 506 /// Capitalize each word inside string
441 /// Example: your name => Your Name, your name => Your name 507 /// Example: your name => Your Name, your name => Your name
442 - static String capitalize(String s) {  
443 - if (isNullOrBlank(s)) return null; 508 + static String capitalize(String value) {
  509 + if (isNullOrBlank(value)) {
  510 + return null;
  511 + }
444 512
445 - var separatedWords = s.split(' '); 513 + final separatedWords = value.split(' ');
446 var result = ''; 514 var result = '';
  515 +
447 for (var word in separatedWords) { 516 for (var word in separatedWords) {
448 result += capitalizeFirst(word); 517 result += capitalizeFirst(word);
449 result += ' '; 518 result += ' ';
450 } 519 }
  520 +
451 return result.trim(); 521 return result.trim();
452 } 522 }
453 523
454 /// Uppercase first letter inside string and let the others lowercase 524 /// Uppercase first letter inside string and let the others lowercase
455 /// Example: your name => Your name 525 /// Example: your name => Your name
456 static String capitalizeFirst(String s) { 526 static String capitalizeFirst(String s) {
457 - if (isNullOrBlank(s)) return null; 527 + if (isNullOrBlank(s)) {
  528 + return null;
  529 + }
  530 +
458 return s[0].toUpperCase() + s.substring(1).toLowerCase(); 531 return s[0].toUpperCase() + s.substring(1).toLowerCase();
459 } 532 }
460 533
461 /// Remove all whitespace inside string 534 /// Remove all whitespace inside string
462 /// Example: your name => yourname 535 /// Example: your name => yourname
463 - static String removeAllWhitespace(String s) {  
464 - if (isNullOrBlank(s)) return null;  
465 - return s.replaceAll(' ', ''); 536 + static String removeAllWhitespace(String value) {
  537 + if (isNullOrBlank(value)) {
  538 + return null;
  539 + }
  540 +
  541 + return value.replaceAll(' ', '');
466 } 542 }
467 543
468 /// Camelcase string 544 /// Camelcase string
469 /// Example: your name => yourName 545 /// Example: your name => yourName
470 - static String camelCase(String s) {  
471 - if (isNullOrBlank(s)) return null;  
472 - var separatedWords = s.split(' '); 546 + static String camelCase(String value) {
  547 + if (isNullOrBlank(value)) {
  548 + return null;
  549 + }
  550 +
  551 + final separatedWords = value.split(' ');
473 var newString = ''; 552 var newString = '';
  553 +
474 for (final word in separatedWords) { 554 for (final word in separatedWords) {
475 newString += word[0].toUpperCase() + word.substring(1).toLowerCase(); 555 newString += word[0].toUpperCase() + word.substring(1).toLowerCase();
476 } 556 }
@@ -484,10 +564,16 @@ class GetUtils { @@ -484,10 +564,16 @@ class GetUtils {
484 /// (first found numeric word) 564 /// (first found numeric word)
485 static String numericOnly(String s, {bool firstWordOnly = false}) { 565 static String numericOnly(String s, {bool firstWordOnly = false}) {
486 var numericOnlyStr = ''; 566 var numericOnlyStr = '';
  567 +
487 for (var i = 0; i < s.length; i++) { 568 for (var i = 0; i < s.length; i++) {
488 - if (isNumericOnly(s[i])) numericOnlyStr += s[i];  
489 - if (firstWordOnly && numericOnlyStr.isNotEmpty && s[i] == " ") break; 569 + if (isNumericOnly(s[i])) {
  570 + numericOnlyStr += s[i];
  571 + }
  572 + if (firstWordOnly && numericOnlyStr.isNotEmpty && s[i] == " ") {
  573 + break;
490 } 574 }
  575 + }
  576 +
491 return numericOnlyStr; 577 return numericOnlyStr;
492 } 578 }
493 579
@@ -495,7 +581,12 @@ class GetUtils { @@ -495,7 +581,12 @@ class GetUtils {
495 return (value == null) ? false : RegExp(pattern).hasMatch(value); 581 return (value == null) ? false : RegExp(pattern).hasMatch(value);
496 } 582 }
497 583
498 - static void printFunction(String prefix, dynamic value, String info,  
499 - {bool isError = false}) => 584 + static void printFunction(
  585 + String prefix,
  586 + dynamic value,
  587 + String info, {
  588 + bool isError = false,
  589 + }) {
500 GetConfig.log('$prefix $value $info'.trim(), isError: isError); 590 GetConfig.log('$prefix $value $info'.trim(), isError: isError);
  591 + }
501 } 592 }
@@ -8,60 +8,63 @@ class TestClass { @@ -8,60 +8,63 @@ class TestClass {
8 class EmptyClass {} 8 class EmptyClass {}
9 9
10 void main() { 10 void main() {
11 - group('dynamic extensions', () {  
12 - test('var.isNullOrBlank on a string ', () {  
13 - var string = '';  
14 - expect('foo'.isNullOrBlank, equals(false));  
15 - expect(string.isNullOrBlank, equals(true));  
16 - });  
17 - test('var.isNullOrBlank on a int ', () {  
18 - expect(5.isNullOrBlank, equals(false));  
19 - expect(0.isNullOrBlank, equals(false)); 11 + group('isNullOrBlank on dynamic', () {
  12 + // Identity util to convert to iterables
  13 + dynamic _id(dynamic e) => e;
  14 +
  15 + test('null isNullOrBlank should be true for null', () {
  16 + expect((null).isNullOrBlank, true);
20 }); 17 });
21 18
22 - test('var.isNullOrBlank on a double ', () { 19 + test('isNullOrBlank should be false for unsupported types', () {
  20 + expect(5.isNullOrBlank, false);
  21 + expect(0.isNullOrBlank, false);
  22 +
23 expect(5.0.isNullOrBlank, equals(false)); 23 expect(5.0.isNullOrBlank, equals(false));
24 expect(0.0.isNullOrBlank, equals(false)); 24 expect(0.0.isNullOrBlank, equals(false));
25 - });  
26 25
27 - test('var.isNullOrBlank on a list ', () {  
28 - var list = ['foo'];  
29 - expect([].isNullOrBlank, equals(true));  
30 - expect(['oi', 'foo'].isNullOrBlank, equals(false));  
31 - expect([{}, {}].isNullOrBlank, equals(false));  
32 - expect(list[0].isNullOrBlank, equals(false)); 26 + TestClass testClass;
  27 + expect(testClass.isNullOrBlank, equals(true));
  28 + expect(TestClass().isNullOrBlank, equals(false));
  29 + expect(EmptyClass().isNullOrBlank, equals(false));
33 }); 30 });
34 31
35 - test('var.isNullOrBlank on a set ', () {  
36 - var halogens = {'fluorine', 'chlorine', 'bromine', 'iodine', 'astatine'};  
37 - expect(<String>{}.isNullOrBlank, equals(true));  
38 - expect({'foo', 'bar'}.isNullOrBlank, equals(false));  
39 - expect(halogens.isNullOrBlank, equals(false)); 32 + test('isNullOrBlank should validate strings', () {
  33 + expect("".isNullOrBlank, true);
  34 + expect(" ".isNullOrBlank, true);
  35 +
  36 + expect("foo".isNullOrBlank, false);
  37 + expect(" foo ".isNullOrBlank, false);
  38 +
  39 + expect("null".isNullOrBlank, false);
40 }); 40 });
41 41
42 - test('var.isNullOrBlank on a map ', () {  
43 - var map = {"foo": 'bar', "one": "um"};  
44 - expect({}.isNullOrBlank, equals(true));  
45 - expect({"other": "thing"}.isNullOrBlank, equals(false));  
46 - expect({'first': [], 'second': []}.isNullOrBlank, equals(false));  
47 - expect(map["foo"].isNullOrBlank, equals(false));  
48 - expect(map["other"].isNullOrBlank, equals(true)); 42 + test('isNullOrBlank should validate iterables', () {
  43 + expect([].map(_id).isNullOrBlank, true);
  44 + expect([1].map(_id).isNullOrBlank, false);
49 }); 45 });
50 46
51 - test('var.isNullOrBlank on a function ', () {  
52 - someFunction({String string, int integer}) {  
53 - expect(string.isNullOrBlank, equals(false));  
54 - expect(integer.isNullOrBlank, equals(true));  
55 - } 47 + test('isNullOrBlank should validate lists', () {
  48 + expect([].isNullOrBlank, true);
  49 + expect(['oi', 'foo'].isNullOrBlank, false);
  50 + expect([{}, {}].isNullOrBlank, false);
  51 + expect(['foo'][0].isNullOrBlank, false);
  52 + });
56 53
57 - someFunction(string: 'some value'); 54 + test('isNullOrBlank should validate sets', () {
  55 + expect((<dynamic>{}).isNullOrBlank, true);
  56 + expect(({1}).isNullOrBlank, false);
  57 + expect({'fluorine', 'chlorine', 'bromine'}.isNullOrBlank, false);
58 }); 58 });
59 59
60 - test('var.isNullOrBlank on a class ', () {  
61 - TestClass testClass;  
62 - expect(TestClass().isNullOrBlank, equals(false));  
63 - expect(testClass.isNullOrBlank, equals(true));  
64 - expect(EmptyClass().isNullOrBlank, equals(false)); 60 + test('isNullOrBlank should validate maps', () {
  61 + expect(({}).isNullOrBlank, true);
  62 + expect(({1: 1}).isNullOrBlank, false);
  63 + expect({"other": "thing"}.isNullOrBlank, false);
  64 +
  65 + final map = {"foo": 'bar', "one": "um"};
  66 + expect(map["foo"].isNullOrBlank, false);
  67 + expect(map["other"].isNullOrBlank, true);
65 }); 68 });
66 }); 69 });
67 70
  1 +import 'package:flutter_test/flutter_test.dart';
  2 +import 'package:get/get.dart';
  3 +
  4 +// Identity util to convert to iterables
  5 +dynamic _id(dynamic e) => e;
  6 +
  7 +void main() {
  8 + // Tests for GetUtils.isNullOrBlank are located at dynamic extensions
  9 + group('GetUtils.isLength* functions', () {
  10 + test('isLengthEqualTo should validate iterable lengths', () {
  11 + // iterables should cover list and set
  12 + expect(GetUtils.isLengthEqualTo([].map(_id), 0), true);
  13 + expect(GetUtils.isLengthEqualTo([1, 2].map(_id), 2), true);
  14 +
  15 + expect(GetUtils.isLengthEqualTo({}, 0), true);
  16 + expect(GetUtils.isLengthEqualTo({1: 1, 2: 1}, 2), true);
  17 + expect(GetUtils.isLengthEqualTo({}, 2), false);
  18 +
  19 + expect(GetUtils.isLengthEqualTo("", 0), true);
  20 + expect(GetUtils.isLengthEqualTo("a", 0), false);
  21 + expect(GetUtils.isLengthEqualTo("a", 1), true);
  22 + });
  23 +
  24 + test('isLengthGreaterOrEqual should validate lengths', () {
  25 + // iterables should cover list and set
  26 + expect(GetUtils.isLengthGreaterOrEqual([].map(_id), 0), true);
  27 + expect(GetUtils.isLengthGreaterOrEqual([1, 2].map(_id), 2), true);
  28 + expect(GetUtils.isLengthGreaterOrEqual([1, 2].map(_id), 1), true);
  29 +
  30 + expect(GetUtils.isLengthGreaterOrEqual({}, 0), true);
  31 + expect(GetUtils.isLengthGreaterOrEqual({1: 1, 2: 1}, 1), true);
  32 + expect(GetUtils.isLengthGreaterOrEqual({1: 1, 2: 1}, 2), true);
  33 + expect(GetUtils.isLengthGreaterOrEqual({}, 2), false);
  34 +
  35 + expect(GetUtils.isLengthGreaterOrEqual("", 0), true);
  36 + expect(GetUtils.isLengthGreaterOrEqual("a", 0), true);
  37 + expect(GetUtils.isLengthGreaterOrEqual("", 1), false);
  38 + });
  39 +
  40 + test('isLengthLessOrEqual should validate lengths', () {
  41 + // iterables should cover list and set
  42 + expect(GetUtils.isLengthLessOrEqual([].map(_id), 0), true);
  43 + expect(GetUtils.isLengthLessOrEqual([1, 2].map(_id), 2), true);
  44 + expect(GetUtils.isLengthLessOrEqual([1, 2].map(_id), 1), false);
  45 +
  46 + expect(GetUtils.isLengthLessOrEqual({}, 0), true);
  47 + expect(GetUtils.isLengthLessOrEqual({1: 1, 2: 1}, 1), false);
  48 + expect(GetUtils.isLengthLessOrEqual({1: 1, 2: 1}, 3), true);
  49 + expect(GetUtils.isLengthLessOrEqual({}, 2), true);
  50 +
  51 + expect(GetUtils.isLengthLessOrEqual("", 0), true);
  52 + expect(GetUtils.isLengthLessOrEqual("a", 2), true);
  53 + expect(GetUtils.isLengthLessOrEqual("a", 0), false);
  54 + });
  55 + });
  56 +}