LCOV - code coverage report
Current view: top level - lib/util - folly_utils.dart (source / functions) Hit Total Coverage
Test: Folly Fields Lines: 88 88 100.0 %
Date: 2023-05-18 02:05:34 Functions: 0 0 -

          Line data    Source code
       1             : // ignore_for_file: prefer-first
       2             : 
       3             : import 'package:flutter/material.dart';
       4             : import 'package:folly_fields/util/folly_date_time_extension.dart';
       5             : 
       6             : ///
       7             : ///
       8             : ///
       9             : class FollyUtils {
      10             :   ///
      11             :   ///
      12             :   ///
      13           1 :   static String? validDate(String value) {
      14           1 :     if (value.isEmpty) {
      15             :       return 'Informe uma data.';
      16             :     }
      17             : 
      18           1 :     List<String> parts = value.split('/');
      19             : 
      20           2 :     if (parts.length != 3) {
      21             :       return 'Data inválida.';
      22             :     }
      23             : 
      24           3 :     if (parts[2].length != 4) {
      25             :       return 'Ano inválido.';
      26             :     }
      27             : 
      28           2 :     int? year = int.tryParse(parts[2]);
      29             :     if (year == null) {
      30             :       return 'Ano inválido.';
      31             :     }
      32             : 
      33           2 :     int? month = int.tryParse(parts[1]);
      34           2 :     if (month == null || month < 1 || month > 12) {
      35             :       return 'Mês inválido.';
      36             :     }
      37             : 
      38           2 :     int? day = int.tryParse(parts[0]);
      39           4 :     if (day == null || day < 1 || day > DateTime(year, month).daysInMonth) {
      40             :       return 'Dia inválido.';
      41             :     }
      42             : 
      43             :     return null;
      44             :   }
      45             : 
      46             :   ///
      47             :   ///
      48             :   ///
      49           1 :   static String? validTime(String value) {
      50           2 :     if (value.length != 5) {
      51             :       return 'Informe uma hora.';
      52             :     }
      53             : 
      54           1 :     List<String> parts = value.split(':');
      55             : 
      56           2 :     if (parts.length != 2) {
      57             :       return 'Hora inválida.';
      58             :     }
      59             : 
      60           2 :     int? hour = int.tryParse(parts[0]);
      61             : 
      62           2 :     if (hour == null || hour < 0 || hour > 23) {
      63             :       return 'Horas inválidas.';
      64             :     }
      65             : 
      66           2 :     int? minute = int.tryParse(parts[1]);
      67             : 
      68           2 :     if (minute == null || minute < 0 || minute > 59) {
      69             :       return 'Minutos inválidos.';
      70             :     }
      71             : 
      72             :     return null;
      73             :   }
      74             : 
      75             :   ///
      76             :   ///
      77             :   ///
      78           1 :   static bool isPascalCase(String value) =>
      79           1 :       !(value.isEmpty ||
      80           2 :           value.contains(RegExp('[^a-zA-Z0-9]+')) ||
      81           2 :           value.startsWith(RegExp('[0-9]+'))) &&
      82           4 :       value[0].toUpperCase() == value[0];
      83             : 
      84             :   ///
      85             :   ///
      86             :   ///
      87           1 :   static bool isCamelCase(String value) =>
      88           1 :       !(value.isEmpty ||
      89           2 :           value.contains(RegExp('[^a-zA-Z0-9]+')) ||
      90           2 :           value.startsWith(RegExp('[0-9]+'))) &&
      91           4 :       value[0].toLowerCase() == value[0];
      92             : 
      93             :   ///
      94             :   ///
      95             :   ///
      96           2 :   static bool isSnakeCase(String value) => !(value.isEmpty ||
      97           2 :       value.contains(RegExp('[^_a-z0-9]+')) ||
      98           2 :       value.startsWith(RegExp('[0-9]+')));
      99             : 
     100             :   ///
     101             :   ///
     102             :   ///
     103           1 :   static String camel2Snake(String camel, {bool internal = false}) =>
     104           1 :       internal || isCamelCase(camel)
     105           1 :           ? camel.splitMapJoin(
     106           1 :               RegExp('[A-Z]'),
     107           4 :               onMatch: (Match m) => '_${m.group(0)!.toLowerCase()}',
     108           1 :               onNonMatch: (String n) => n,
     109             :             )
     110             :           : '';
     111             : 
     112             :   ///
     113             :   ///
     114             :   ///
     115           2 :   static String snake2Camel(String snake) => isSnakeCase(snake)
     116           2 :       ? pascal2Camel(snake2Pascal(snake, internal: true), internal: true)
     117             :       : '';
     118             : 
     119             :   ///
     120             :   ///
     121             :   ///
     122           1 :   static String pascal2Camel(String pascal, {bool internal = false}) =>
     123           1 :       internal || isPascalCase(pascal)
     124           4 :           ? pascal[0].toLowerCase() + pascal.substring(1)
     125             :           : '';
     126             : 
     127             :   ///
     128             :   ///
     129             :   ///
     130           1 :   static String camel2Pascal(String camel) =>
     131           5 :       isCamelCase(camel) ? camel[0].toUpperCase() + camel.substring(1) : '';
     132             : 
     133             :   ///
     134             :   ///
     135             :   ///
     136           2 :   static String pascal2Snake(String pascal) => isPascalCase(pascal)
     137           2 :       ? camel2Snake(pascal, internal: true).substring(1)
     138             :       : '';
     139             : 
     140             :   ///
     141             :   ///
     142             :   ///
     143           1 :   static String snake2Pascal(String snake, {bool internal = false}) =>
     144           1 :       internal || isSnakeCase(snake)
     145           2 :           ? snake.toLowerCase().splitMapJoin(
     146           1 :                 RegExp('_'),
     147           1 :                 onMatch: (Match m) => '',
     148           1 :                 onNonMatch: (String n) =>
     149           4 :                     n.substring(0, 1).toUpperCase() + n.substring(1),
     150             :               )
     151             :           : '';
     152             : 
     153             :   ///
     154             :   ///
     155             :   ///
     156           1 :   static Color textColorByLuminance(
     157             :     Color color, {
     158             :     Color darkColor = Colors.black,
     159             :     Color lightColor = Colors.white,
     160             :     double redFactor = 0.299,
     161             :     double greenFactor = 0.587,
     162             :     double blueFactor = 0.114,
     163             :     double threshold = 186,
     164             :   }) =>
     165           3 :       color.red * redFactor +
     166           3 :                   color.green * greenFactor +
     167           3 :                   color.blue * blueFactor >
     168             :               threshold
     169             :           ? darkColor
     170             :           : lightColor;
     171             : 
     172             :   ///
     173             :   ///
     174             :   ///
     175           2 :   static Color? colorParse(String? text, [int? defaultColor]) {
     176           2 :     if (text == null || text.isEmpty) {
     177           1 :       return defaultColor == null ? null : Color(defaultColor);
     178             :     } else {
     179             :       try {
     180           2 :         if (!text.startsWith('0x')) {
     181           6 :           text = text.replaceAll('#', '').trim().toUpperCase();
     182             : 
     183           4 :           if (text.length < 3) {
     184           2 :             throw Exception('Length less than 3.');
     185             :           }
     186             : 
     187           4 :           if (text.length == 3) {
     188          22 :             text = text[0] + text[0] + text[1] + text[1] + text[2] + text[2];
     189             :           }
     190             : 
     191           4 :           if (text.length == 4) {
     192           2 :             text = text[0] +
     193           2 :                 text[0] +
     194           2 :                 text[1] +
     195           2 :                 text[1] +
     196           2 :                 text[2] +
     197           2 :                 text[2] +
     198           2 :                 text[3] +
     199           1 :                 text[3];
     200             :           }
     201             : 
     202           4 :           if (text.length == 6) {
     203           2 :             text = 'FF$text';
     204             :           }
     205             : 
     206           4 :           if (text.length > 8) {
     207           1 :             text = text.substring(0, 8);
     208             :           }
     209             : 
     210           2 :           text = '0x$text';
     211             :         }
     212             : 
     213           4 :         return Color(int.parse(text));
     214           2 :       } on Exception catch (_) {
     215           1 :         return defaultColor == null ? null : Color(defaultColor);
     216             :       }
     217             :     }
     218             :   }
     219             : 
     220             :   ///
     221             :   ///
     222             :   ///
     223           1 :   static MaterialColor? createMaterialColor({
     224             :     int? intColor,
     225             :     Color? color,
     226             :   }) {
     227             :     if (intColor != null) {
     228           1 :       color = Color(intColor);
     229             :     }
     230             : 
     231             :     if (color == null) {
     232             :       return null;
     233             :     }
     234             : 
     235           1 :     return MaterialColor(
     236           1 :       color.value,
     237           1 :       <int, Color>{
     238             :         50: color,
     239             :         100: color,
     240             :         200: color,
     241             :         300: color,
     242             :         400: color,
     243             :         500: color,
     244             :         600: color,
     245             :         700: color,
     246             :         800: color,
     247             :         900: color,
     248             :       },
     249             :     );
     250             :   }
     251             : }

Generated by: LCOV version 1.14