LCOV - code coverage report
Current view: top level - utils - case_utils.dart (source / functions) Coverage Total Hit
Test: lcov.info Lines: 60.4 % 48 29
Test Date: 2024-07-24 10:28:19 Functions: - 0 0

            Line data    Source code
       1              : class CaseUtil {
       2              :   final RegExp _upperAlphaRegex = RegExp(r'[A-Z]');
       3              :   final RegExp _symbolRegex = RegExp(r'[ ./_\-:]');
       4              : 
       5              :   late String originalText;
       6              :   late List<String> _words;
       7              : 
       8            1 :   CaseUtil(String text,
       9              :       {String? alternativeText, List<String> removeSuffixes = const []}) {
      10            1 :     for (final removeSuffix in removeSuffixes) {
      11            0 :       if (text.toLowerCase().endsWith(removeSuffix.toLowerCase())) {
      12            0 :         text = text.substring(0, text.length - removeSuffix.length);
      13              :       }
      14              :       if (alternativeText != null &&
      15            0 :           alternativeText.toLowerCase().endsWith(removeSuffix.toLowerCase())) {
      16            0 :         alternativeText = alternativeText.substring(
      17            0 :             0, alternativeText.length - removeSuffix.length);
      18              :       }
      19              :     }
      20            1 :     originalText = text;
      21            2 :     _words = _groupIntoWords(text);
      22            2 :     if (_words.isEmpty) {
      23            1 :       originalText = alternativeText ?? '';
      24            2 :       _words = _groupIntoWords(alternativeText ?? '');
      25              :     }
      26              :   }
      27              : 
      28            1 :   List<String> _groupIntoWords(String text) {
      29            1 :     final sb = StringBuffer();
      30            1 :     final words = <String>[];
      31            2 :     final isAllCaps = !text.contains(RegExp('[a-z]'));
      32              : 
      33            3 :     for (var i = 0; i < text.length; i++) {
      34            2 :       final char = String.fromCharCode(text.codeUnitAt(i));
      35            3 :       final nextChar = i + 1 == text.length
      36              :           ? null
      37            3 :           : String.fromCharCode(text.codeUnitAt(i + 1));
      38              : 
      39            2 :       if (_symbolRegex.hasMatch(char)) {
      40              :         continue;
      41              :       }
      42              : 
      43            1 :       sb.write(char);
      44              : 
      45              :       final isEndOfWord = nextChar == null ||
      46            2 :           (_upperAlphaRegex.hasMatch(nextChar) && !isAllCaps) ||
      47            2 :           _symbolRegex.hasMatch(nextChar);
      48              : 
      49              :       if (isEndOfWord) {
      50            2 :         words.add(sb.toString());
      51            1 :         sb.clear();
      52              :       }
      53              :     }
      54              : 
      55              :     return words;
      56              :   }
      57              : 
      58            0 :   String get snakeCase => _getSnakeCase();
      59              : 
      60            0 :   String get kebabCase => _getSnakeCase(separator: '-');
      61              : 
      62            2 :   String get camelCase => _getCamelCase();
      63              : 
      64            0 :   String get textWithoutSuffix => _getTextWithoutSuffix();
      65              : 
      66            0 :   String get upperCamelCase => _uppserCamelCase();
      67              : 
      68            0 :   String _getSnakeCase({String separator = '_'}) =>
      69            0 :       _words.map((word) => word.toLowerCase()).toList().join(separator);
      70              : 
      71            1 :   String _getCamelCase({String separator = ''}) {
      72            3 :     final words = _words.map(_upperCaseFirstLetter).toList();
      73            4 :     if (words.isNotEmpty) words[0] = words[0].toLowerCase();
      74            1 :     return words.join(separator);
      75              :   }
      76              : 
      77            0 :   String _uppserCamelCase({String separator = ''}) {
      78            0 :     final words = _words.map(_upperCaseFirstLetter).toList();
      79            0 :     return words.join(separator);
      80              :   }
      81              : 
      82            0 :   String _getTextWithoutSuffix() {
      83            0 :     final delimiters = [' ', '-', '_', '.', '/', ':'];
      84            0 :     if (delimiters.any(originalText.endsWith)) {
      85            0 :       return originalText.substring(0, originalText.length - 1);
      86              :     } else {
      87            0 :       return originalText;
      88              :     }
      89              :   }
      90              : 
      91            1 :   static String _upperCaseFirstLetter(String word) {
      92            1 :     if (word.isEmpty) return word;
      93            5 :     return '${word.substring(0, 1).toUpperCase()}${word.substring(1).toLowerCase()}';
      94              :   }
      95              : }
        

Generated by: LCOV version 2.0-1