camelCaseToWords function Null safety

String camelCaseToWords(
  1. String subject,
  2. [Pattern customPattern = defaultPattern]
)

Implementation

String camelCaseToWords(String subject,
    [Pattern customPattern = defaultPattern]) {
  if (subject is! String || subject.isEmpty) {
    return '';
  }

  late RegExp pattern;

  if (customPattern is String) {
    pattern = RegExp(customPattern);
  } else if (customPattern is RegExp) {
    pattern = customPattern;
  }

  final words = pattern.allMatches(subject).map((m) => m.group(0)).toList();

  words[0] = words[0]![0].toUpperCase() + words[0]!.substring(1);

  for (var i = 1; i < words.length; i++) {
    words[i] = words[i]!.toLowerCase();
  }

  return words.join(' ');
}