fix(getUtils): replace unsupported switch types and refactor code style
Showing
1 changed file
with
247 additions
and
155 deletions
| 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,28 @@ class GetUtils { | @@ -13,20 +65,28 @@ 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 | + /// FIXME: null checking here is.. pointless (? see isNull above) |
| 74 | + if(value is String) { | ||
| 75 | + return value.toString() == 'null' || value.toString().trim().isEmpty; | ||
| 23 | } | 76 | } |
| 77 | + | ||
| 78 | + // Pretty sure that isNullOrBlank should't be validating | ||
| 79 | + // iterables... but I'm going to keep this for compatibility. | ||
| 80 | + return _hasIsEmpty(value) ? value.isEmpty as bool : true; | ||
| 24 | } | 81 | } |
| 25 | 82 | ||
| 26 | /// Checks if string is int or double. | 83 | /// 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; | 84 | + static bool isNum(String value) { |
| 85 | + if (isNull(value)) { | ||
| 86 | + return false; | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + return num.tryParse(value) is num; | ||
| 30 | } | 90 | } |
| 31 | 91 | ||
| 32 | /// Checks if string consist only numeric. | 92 | /// Checks if string consist only numeric. |
| @@ -37,9 +97,12 @@ class GetUtils { | @@ -37,9 +97,12 @@ class GetUtils { | ||
| 37 | static bool isAlphabetOnly(String s) => hasMatch(s, r'^[a-zA-Z]+$'); | 97 | static bool isAlphabetOnly(String s) => hasMatch(s, r'^[a-zA-Z]+$'); |
| 38 | 98 | ||
| 39 | /// Checks if string is boolean. | 99 | /// Checks if string is boolean. |
| 40 | - static bool isBool(String s) { | ||
| 41 | - if (isNull(s)) return false; | ||
| 42 | - return (s == 'true' || s == 'false'); | 100 | + static bool isBool(String value) { |
| 101 | + if (isNull(value)) { | ||
| 102 | + return false; | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + return (value == 'true' || value == 'false'); | ||
| 43 | } | 106 | } |
| 44 | 107 | ||
| 45 | /// Checks if string is an video file. | 108 | /// Checks if string is an video file. |
| @@ -80,18 +143,21 @@ class GetUtils { | @@ -80,18 +143,21 @@ class GetUtils { | ||
| 80 | /// Checks if string is an powerpoint file. | 143 | /// Checks if string is an powerpoint file. |
| 81 | static bool isPPT(String filePath) { | 144 | static bool isPPT(String filePath) { |
| 82 | final ext = filePath.toLowerCase(); | 145 | final ext = filePath.toLowerCase(); |
| 146 | + | ||
| 83 | return ext.endsWith(".ppt") || ext.endsWith(".pptx"); | 147 | return ext.endsWith(".ppt") || ext.endsWith(".pptx"); |
| 84 | } | 148 | } |
| 85 | 149 | ||
| 86 | /// Checks if string is an word file. | 150 | /// Checks if string is an word file. |
| 87 | static bool isWord(String filePath) { | 151 | static bool isWord(String filePath) { |
| 88 | final ext = filePath.toLowerCase(); | 152 | final ext = filePath.toLowerCase(); |
| 153 | + | ||
| 89 | return ext.endsWith(".doc") || ext.endsWith(".docx"); | 154 | return ext.endsWith(".doc") || ext.endsWith(".docx"); |
| 90 | } | 155 | } |
| 91 | 156 | ||
| 92 | /// Checks if string is an excel file. | 157 | /// Checks if string is an excel file. |
| 93 | static bool isExcel(String filePath) { | 158 | static bool isExcel(String filePath) { |
| 94 | final ext = filePath.toLowerCase(); | 159 | final ext = filePath.toLowerCase(); |
| 160 | + | ||
| 95 | return ext.endsWith(".xls") || ext.endsWith(".xlsx"); | 161 | return ext.endsWith(".xls") || ext.endsWith(".xlsx"); |
| 96 | } | 162 | } |
| 97 | 163 | ||
| @@ -177,42 +243,50 @@ class GetUtils { | @@ -177,42 +243,50 @@ class GetUtils { | ||
| 177 | hasMatch(s, r'^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$'); | 243 | hasMatch(s, r'^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$'); |
| 178 | 244 | ||
| 179 | /// Checks if string is Palindrom. | 245 | /// Checks if string is Palindrom. |
| 180 | - static bool isPalindrom(String s) { | ||
| 181 | - final cleanString = s | 246 | + static bool isPalindrom(String string) { |
| 247 | + final cleanString = string | ||
| 182 | .toLowerCase() | 248 | .toLowerCase() |
| 183 | .replaceAll(RegExp(r"\s+"), '') | 249 | .replaceAll(RegExp(r"\s+"), '') |
| 184 | .replaceAll(RegExp(r"[^0-9a-zA-Z]+"), ""); | 250 | .replaceAll(RegExp(r"[^0-9a-zA-Z]+"), ""); |
| 185 | - var isPalindrom = true; | 251 | + |
| 186 | for (var i = 0; i < cleanString.length; i++) { | 252 | for (var i = 0; i < cleanString.length; i++) { |
| 187 | if (cleanString[i] != cleanString[cleanString.length - i - 1]) { | 253 | if (cleanString[i] != cleanString[cleanString.length - i - 1]) { |
| 188 | - isPalindrom = false; | 254 | + return false; |
| 189 | } | 255 | } |
| 190 | } | 256 | } |
| 191 | - return isPalindrom; | 257 | + |
| 258 | + return true; | ||
| 192 | } | 259 | } |
| 193 | 260 | ||
| 194 | /// Checks if all data have same value. | 261 | /// Checks if all data have same value. |
| 195 | /// Example: 111111 -> true, wwwww -> true, [1,1,1,1] -> true | 262 | /// 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; | 263 | + static bool isOneAKind(dynamic value) { |
| 264 | + if ((value is String || value is List) && !isNullOrBlank(value)) { | ||
| 265 | + final first = value[0]; | ||
| 266 | + final len = value.length as num; | ||
| 267 | + | ||
| 201 | for (var i = 0; i < len; i++) { | 268 | for (var i = 0; i < len; i++) { |
| 202 | - if (s[i] != first) isOneAKind = false; | 269 | + if (value[i] != first) { |
| 270 | + return false; | ||
| 271 | + } | ||
| 203 | } | 272 | } |
| 204 | - return isOneAKind; | 273 | + |
| 274 | + return true; | ||
| 205 | } | 275 | } |
| 206 | 276 | ||
| 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; | 277 | + if (value is int) { |
| 278 | + final stringValue = value.toString(); | ||
| 279 | + final first = stringValue[0]; | ||
| 280 | + | ||
| 281 | + for (var i = 0; i < stringValue.length; i++) { | ||
| 282 | + if (stringValue[i] != first) { | ||
| 283 | + return false; | ||
| 284 | + } | ||
| 213 | } | 285 | } |
| 214 | - return isOneAKind; | 286 | + |
| 287 | + return true; | ||
| 215 | } | 288 | } |
| 289 | + | ||
| 216 | return false; | 290 | return false; |
| 217 | } | 291 | } |
| 218 | 292 | ||
| @@ -224,124 +298,96 @@ class GetUtils { | @@ -224,124 +298,96 @@ class GetUtils { | ||
| 224 | static bool isCurrency(String s) => hasMatch(s, | 298 | 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))?$'); | 299 | 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 | 300 | ||
| 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: | ||
| 242 | - return false; | 301 | + /// Checks if length of data is GREATER than maxLength. |
| 302 | + static bool isLengthGreaterThan(dynamic value, int maxLength) { | ||
| 303 | + final length = _obtainDynamicLength(value); | ||
| 304 | + | ||
| 305 | + if (length == null) { | ||
| 306 | + return false; | ||
| 243 | } | 307 | } |
| 308 | + | ||
| 309 | + return length > maxLength; | ||
| 244 | } | 310 | } |
| 245 | 311 | ||
| 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: | ||
| 261 | - return false; | 312 | + /// Checks if length of data is GREATER OR EQUAL to maxLength. |
| 313 | + static bool isLengthGreaterOrEqual(dynamic value, int maxLength) { | ||
| 314 | + final length = _obtainDynamicLength(value); | ||
| 315 | + | ||
| 316 | + if (length == null) { | ||
| 317 | + return false; | ||
| 262 | } | 318 | } |
| 319 | + | ||
| 320 | + return length >= maxLength; | ||
| 263 | } | 321 | } |
| 264 | 322 | ||
| 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: | ||
| 283 | - return false; | 323 | + /// Checks if length of data is LOWER than maxLength. |
| 324 | + /// | ||
| 325 | + /// This method is deprecated, use [isLengthLessThan] instead | ||
| 326 | + @deprecated | ||
| 327 | + static bool isLengthLowerThan(dynamic value, int maxLength) => | ||
| 328 | + isLengthLessThan(value, maxLength); | ||
| 329 | + | ||
| 330 | + /// Checks if length of data is LESS than maxLength. | ||
| 331 | + static bool isLengthLessThan(dynamic value, int maxLength) { | ||
| 332 | + final length = _obtainDynamicLength(value); | ||
| 333 | + if (length == null) { | ||
| 334 | + return false; | ||
| 284 | } | 335 | } |
| 336 | + | ||
| 337 | + return length < maxLength; | ||
| 285 | } | 338 | } |
| 286 | 339 | ||
| 287 | /// Checks if length of data is LOWER OR EQUAL to maxLength. | 340 | /// 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: | ||
| 302 | - return false; | 341 | + /// |
| 342 | + /// This method is deprecated, use [isLengthLessOrEqual] instead | ||
| 343 | + @deprecated | ||
| 344 | + static bool isLengthLowerOrEqual(dynamic value, int maxLength) => | ||
| 345 | + isLengthLessOrEqual(value, maxLength); | ||
| 346 | + | ||
| 347 | + /// Checks if length of data is LESS OR EQUAL to maxLength. | ||
| 348 | + static bool isLengthLessOrEqual(dynamic value, int maxLength) { | ||
| 349 | + final length = _obtainDynamicLength(value); | ||
| 350 | + | ||
| 351 | + if (length == null) { | ||
| 352 | + return false; | ||
| 303 | } | 353 | } |
| 354 | + | ||
| 355 | + return length <= maxLength; | ||
| 304 | } | 356 | } |
| 305 | 357 | ||
| 306 | /// Checks if length of data is EQUAL to maxLength. | 358 | /// 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: | ||
| 324 | - return false; | 359 | + static bool isLengthEqualTo(dynamic value, int otherLength) { |
| 360 | + final length = _obtainDynamicLength(value); | ||
| 361 | + | ||
| 362 | + if (length == null) { | ||
| 363 | + return false; | ||
| 325 | } | 364 | } |
| 365 | + | ||
| 366 | + return length == otherLength; | ||
| 326 | } | 367 | } |
| 327 | 368 | ||
| 328 | /// Checks if length of data is BETWEEN minLength to maxLength. | 369 | /// 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); | 370 | + static bool isLengthBetween(dynamic value, int minLength, int maxLength) { |
| 371 | + if (isNull(value)) { | ||
| 372 | + return false; | ||
| 373 | + } | ||
| 374 | + | ||
| 375 | + return isLengthGreaterOrEqual(value, minLength) && | ||
| 376 | + isLengthLowerOrEqual(value, maxLength); | ||
| 333 | } | 377 | } |
| 334 | 378 | ||
| 335 | /// Checks if a contains b (Treating or interpreting upper- and lowercase | 379 | /// Checks if a contains b (Treating or interpreting upper- and lowercase |
| 336 | /// letters as being the same). | 380 | /// letters as being the same). |
| 337 | - static bool isCaseInsensitiveContains(String a, String b) => | ||
| 338 | - a.toLowerCase().contains(b.toLowerCase()); | 381 | + static bool isCaseInsensitiveContains(String a, String b) { |
| 382 | + return a.toLowerCase().contains(b.toLowerCase()); | ||
| 383 | + } | ||
| 339 | 384 | ||
| 340 | /// Checks if a contains b or b contains a (Treating or | 385 | /// Checks if a contains b or b contains a (Treating or |
| 341 | /// interpreting upper- and lowercase letters as being the same). | 386 | /// interpreting upper- and lowercase letters as being the same). |
| 342 | static bool isCaseInsensitiveContainsAny(String a, String b) { | 387 | static bool isCaseInsensitiveContainsAny(String a, String b) { |
| 343 | final lowA = a.toLowerCase(); | 388 | final lowA = a.toLowerCase(); |
| 344 | final lowB = b.toLowerCase(); | 389 | final lowB = b.toLowerCase(); |
| 390 | + | ||
| 345 | return lowA.contains(lowB) || lowB.contains(lowA); | 391 | return lowA.contains(lowB) || lowB.contains(lowA); |
| 346 | } | 392 | } |
| 347 | 393 | ||
| @@ -356,19 +402,25 @@ class GetUtils { | @@ -356,19 +402,25 @@ class GetUtils { | ||
| 356 | 402 | ||
| 357 | //Check if num is a cnpj | 403 | //Check if num is a cnpj |
| 358 | static bool isCnpj(String cnpj) { | 404 | static bool isCnpj(String cnpj) { |
| 359 | - if (cnpj == null) return false; | 405 | + if (cnpj == null) { |
| 406 | + return false; | ||
| 407 | + } | ||
| 360 | 408 | ||
| 361 | // Obter somente os números do CNPJ | 409 | // Obter somente os números do CNPJ |
| 362 | - var numbers = cnpj.replaceAll(RegExp(r'[^0-9]'), ''); | 410 | + final numbers = cnpj.replaceAll(RegExp(r'[^0-9]'), ''); |
| 363 | 411 | ||
| 364 | // Testar se o CNPJ possui 14 dígitos | 412 | // Testar se o CNPJ possui 14 dígitos |
| 365 | - if (numbers.length != 14) return false; | 413 | + if (numbers.length != 14) { |
| 414 | + return false; | ||
| 415 | + } | ||
| 366 | 416 | ||
| 367 | // Testar se todos os dígitos do CNPJ são iguais | 417 | // Testar se todos os dígitos do CNPJ são iguais |
| 368 | - if (RegExp(r'^(\d)\1*$').hasMatch(numbers)) return false; | 418 | + if (RegExp(r'^(\d)\1*$').hasMatch(numbers)) { |
| 419 | + return false; | ||
| 420 | + } | ||
| 369 | 421 | ||
| 370 | // Dividir dígitos | 422 | // Dividir dígitos |
| 371 | - var digits = numbers.split('').map(int.parse).toList(); | 423 | + final digits = numbers.split('').map(int.parse).toList(); |
| 372 | 424 | ||
| 373 | // Calcular o primeiro dígito verificador | 425 | // Calcular o primeiro dígito verificador |
| 374 | var calcDv1 = 0; | 426 | var calcDv1 = 0; |
| @@ -377,10 +429,12 @@ class GetUtils { | @@ -377,10 +429,12 @@ class GetUtils { | ||
| 377 | calcDv1 += digits[j++] * i; | 429 | calcDv1 += digits[j++] * i; |
| 378 | } | 430 | } |
| 379 | calcDv1 %= 11; | 431 | calcDv1 %= 11; |
| 380 | - var dv1 = calcDv1 < 2 ? 0 : 11 - calcDv1; | 432 | + final dv1 = calcDv1 < 2 ? 0 : 11 - calcDv1; |
| 381 | 433 | ||
| 382 | // Testar o primeiro dígito verificado | 434 | // Testar o primeiro dígito verificado |
| 383 | - if (digits[12] != dv1) return false; | 435 | + if (digits[12] != dv1) { |
| 436 | + return false; | ||
| 437 | + } | ||
| 384 | 438 | ||
| 385 | // Calcular o segundo dígito verificador | 439 | // Calcular o segundo dígito verificador |
| 386 | var calcDv2 = 0; | 440 | var calcDv2 = 0; |
| @@ -389,27 +443,35 @@ class GetUtils { | @@ -389,27 +443,35 @@ class GetUtils { | ||
| 389 | calcDv2 += digits[j++] * i; | 443 | calcDv2 += digits[j++] * i; |
| 390 | } | 444 | } |
| 391 | calcDv2 %= 11; | 445 | calcDv2 %= 11; |
| 392 | - var dv2 = calcDv2 < 2 ? 0 : 11 - calcDv2; | 446 | + final dv2 = calcDv2 < 2 ? 0 : 11 - calcDv2; |
| 393 | 447 | ||
| 394 | // Testar o segundo dígito verificador | 448 | // Testar o segundo dígito verificador |
| 395 | - if (digits[13] != dv2) return false; | 449 | + if (digits[13] != dv2) { |
| 450 | + return false; | ||
| 451 | + } | ||
| 396 | 452 | ||
| 397 | return true; | 453 | return true; |
| 398 | } | 454 | } |
| 399 | 455 | ||
| 400 | /// Checks if the cpf is valid. | 456 | /// Checks if the cpf is valid. |
| 401 | static bool isCpf(String cpf) { | 457 | static bool isCpf(String cpf) { |
| 402 | - if (cpf == null) return false; | 458 | + if (cpf == null) { |
| 459 | + return false; | ||
| 460 | + } | ||
| 403 | 461 | ||
| 404 | // get only the numbers | 462 | // get only the numbers |
| 405 | - var numbers = cpf.replaceAll(RegExp(r'[^0-9]'), ''); | 463 | + final numbers = cpf.replaceAll(RegExp(r'[^0-9]'), ''); |
| 406 | // Test if the CPF has 11 digits | 464 | // Test if the CPF has 11 digits |
| 407 | - if (numbers.length != 11) return false; | 465 | + if (numbers.length != 11) { |
| 466 | + return false; | ||
| 467 | + } | ||
| 408 | // Test if all CPF digits are the same | 468 | // Test if all CPF digits are the same |
| 409 | - if (RegExp(r'^(\d)\1*$').hasMatch(numbers)) return false; | 469 | + if (RegExp(r'^(\d)\1*$').hasMatch(numbers)) { |
| 470 | + return false; | ||
| 471 | + } | ||
| 410 | 472 | ||
| 411 | // split the digits | 473 | // split the digits |
| 412 | - var digits = numbers.split('').map(int.parse).toList(); | 474 | + final digits = numbers.split('').map(int.parse).toList(); |
| 413 | 475 | ||
| 414 | // Calculate the first verifier digit | 476 | // Calculate the first verifier digit |
| 415 | var calcDv1 = 0; | 477 | var calcDv1 = 0; |
| @@ -417,10 +479,13 @@ class GetUtils { | @@ -417,10 +479,13 @@ class GetUtils { | ||
| 417 | calcDv1 += digits[10 - i] * i; | 479 | calcDv1 += digits[10 - i] * i; |
| 418 | } | 480 | } |
| 419 | calcDv1 %= 11; | 481 | calcDv1 %= 11; |
| 420 | - var dv1 = calcDv1 < 2 ? 0 : 11 - calcDv1; | 482 | + |
| 483 | + final dv1 = calcDv1 < 2 ? 0 : 11 - calcDv1; | ||
| 421 | 484 | ||
| 422 | // Tests the first verifier digit | 485 | // Tests the first verifier digit |
| 423 | - if (digits[9] != dv1) return false; | 486 | + if (digits[9] != dv1) { |
| 487 | + return false; | ||
| 488 | + } | ||
| 424 | 489 | ||
| 425 | // Calculate the second verifier digit | 490 | // Calculate the second verifier digit |
| 426 | var calcDv2 = 0; | 491 | var calcDv2 = 0; |
| @@ -429,48 +494,64 @@ class GetUtils { | @@ -429,48 +494,64 @@ class GetUtils { | ||
| 429 | } | 494 | } |
| 430 | calcDv2 %= 11; | 495 | calcDv2 %= 11; |
| 431 | 496 | ||
| 432 | - var dv2 = calcDv2 < 2 ? 0 : 11 - calcDv2; | 497 | + final dv2 = calcDv2 < 2 ? 0 : 11 - calcDv2; |
| 433 | 498 | ||
| 434 | // Test the second verifier digit | 499 | // Test the second verifier digit |
| 435 | - if (digits[10] != dv2) return false; | 500 | + if (digits[10] != dv2) { |
| 501 | + return false; | ||
| 502 | + } | ||
| 436 | 503 | ||
| 437 | return true; | 504 | return true; |
| 438 | } | 505 | } |
| 439 | 506 | ||
| 440 | /// Capitalize each word inside string | 507 | /// Capitalize each word inside string |
| 441 | /// Example: your name => Your Name, your name => Your name | 508 | /// Example: your name => Your Name, your name => Your name |
| 442 | - static String capitalize(String s) { | ||
| 443 | - if (isNullOrBlank(s)) return null; | 509 | + static String capitalize(String value) { |
| 510 | + if (isNullOrBlank(value)) { | ||
| 511 | + return null; | ||
| 512 | + } | ||
| 444 | 513 | ||
| 445 | - var separatedWords = s.split(' '); | 514 | + final separatedWords = value.split(' '); |
| 446 | var result = ''; | 515 | var result = ''; |
| 516 | + | ||
| 447 | for (var word in separatedWords) { | 517 | for (var word in separatedWords) { |
| 448 | result += capitalizeFirst(word); | 518 | result += capitalizeFirst(word); |
| 449 | result += ' '; | 519 | result += ' '; |
| 450 | } | 520 | } |
| 521 | + | ||
| 451 | return result.trim(); | 522 | return result.trim(); |
| 452 | } | 523 | } |
| 453 | 524 | ||
| 454 | /// Uppercase first letter inside string and let the others lowercase | 525 | /// Uppercase first letter inside string and let the others lowercase |
| 455 | /// Example: your name => Your name | 526 | /// Example: your name => Your name |
| 456 | static String capitalizeFirst(String s) { | 527 | static String capitalizeFirst(String s) { |
| 457 | - if (isNullOrBlank(s)) return null; | 528 | + if (isNullOrBlank(s)) { |
| 529 | + return null; | ||
| 530 | + } | ||
| 531 | + | ||
| 458 | return s[0].toUpperCase() + s.substring(1).toLowerCase(); | 532 | return s[0].toUpperCase() + s.substring(1).toLowerCase(); |
| 459 | } | 533 | } |
| 460 | 534 | ||
| 461 | /// Remove all whitespace inside string | 535 | /// Remove all whitespace inside string |
| 462 | /// Example: your name => yourname | 536 | /// Example: your name => yourname |
| 463 | - static String removeAllWhitespace(String s) { | ||
| 464 | - if (isNullOrBlank(s)) return null; | ||
| 465 | - return s.replaceAll(' ', ''); | 537 | + static String removeAllWhitespace(String value) { |
| 538 | + if (isNullOrBlank(value)) { | ||
| 539 | + return null; | ||
| 540 | + } | ||
| 541 | + | ||
| 542 | + return value.replaceAll(' ', ''); | ||
| 466 | } | 543 | } |
| 467 | 544 | ||
| 468 | /// Camelcase string | 545 | /// Camelcase string |
| 469 | /// Example: your name => yourName | 546 | /// Example: your name => yourName |
| 470 | - static String camelCase(String s) { | ||
| 471 | - if (isNullOrBlank(s)) return null; | ||
| 472 | - var separatedWords = s.split(' '); | 547 | + static String camelCase(String value) { |
| 548 | + if (isNullOrBlank(value)) { | ||
| 549 | + return null; | ||
| 550 | + } | ||
| 551 | + | ||
| 552 | + final separatedWords = value.split(' '); | ||
| 473 | var newString = ''; | 553 | var newString = ''; |
| 554 | + | ||
| 474 | for (final word in separatedWords) { | 555 | for (final word in separatedWords) { |
| 475 | newString += word[0].toUpperCase() + word.substring(1).toLowerCase(); | 556 | newString += word[0].toUpperCase() + word.substring(1).toLowerCase(); |
| 476 | } | 557 | } |
| @@ -484,10 +565,16 @@ class GetUtils { | @@ -484,10 +565,16 @@ class GetUtils { | ||
| 484 | /// (first found numeric word) | 565 | /// (first found numeric word) |
| 485 | static String numericOnly(String s, {bool firstWordOnly = false}) { | 566 | static String numericOnly(String s, {bool firstWordOnly = false}) { |
| 486 | var numericOnlyStr = ''; | 567 | var numericOnlyStr = ''; |
| 568 | + | ||
| 487 | for (var i = 0; i < s.length; i++) { | 569 | for (var i = 0; i < s.length; i++) { |
| 488 | - if (isNumericOnly(s[i])) numericOnlyStr += s[i]; | ||
| 489 | - if (firstWordOnly && numericOnlyStr.isNotEmpty && s[i] == " ") break; | 570 | + if (isNumericOnly(s[i])) { |
| 571 | + numericOnlyStr += s[i]; | ||
| 572 | + } | ||
| 573 | + if (firstWordOnly && numericOnlyStr.isNotEmpty && s[i] == " ") { | ||
| 574 | + break; | ||
| 575 | + } | ||
| 490 | } | 576 | } |
| 577 | + | ||
| 491 | return numericOnlyStr; | 578 | return numericOnlyStr; |
| 492 | } | 579 | } |
| 493 | 580 | ||
| @@ -495,7 +582,12 @@ class GetUtils { | @@ -495,7 +582,12 @@ class GetUtils { | ||
| 495 | return (value == null) ? false : RegExp(pattern).hasMatch(value); | 582 | return (value == null) ? false : RegExp(pattern).hasMatch(value); |
| 496 | } | 583 | } |
| 497 | 584 | ||
| 498 | - static void printFunction(String prefix, dynamic value, String info, | ||
| 499 | - {bool isError = false}) => | ||
| 500 | - GetConfig.log('$prefix $value $info'.trim(), isError: isError); | 585 | + static void printFunction( |
| 586 | + String prefix, | ||
| 587 | + dynamic value, | ||
| 588 | + String info, { | ||
| 589 | + bool isError = false, | ||
| 590 | + }) { | ||
| 591 | + GetConfig.log('$prefix $value $info'.trim(), isError: isError); | ||
| 592 | + } | ||
| 501 | } | 593 | } |
-
Please register or login to post a comment