extractLowercaseComponents method

List<String> extractLowercaseComponents({
  1. Set<String> special = const {'.'},
})

Extracts and returns a list of lowercase components from the string.

This method identifies components based on transitions between lowercase and uppercase letters, between letters and digits, within sequences of uppercase letters (including special characters), and at any non-alphanumeric characters. Each identified component is converted to lowercase.

The method is useful for parsing strings formatted in camelCase, PascalCase, snake_case, kebab-case, or other mixed-case styles into a list of lowercase words or segments.

Example:

var example = 'HelloWorld123.456';
var components = example.extractLowercaseComponents(special = const {'.'});
print(components); // Output: ['hello', 'world', '123+456']

Implementation

List<String> extractLowercaseComponents({
  Set<String> special = const {'.'},
}) {
  if (this.isEmpty) return [this];
  final words = <String>[];
  var currentWord = StringBuffer();
  String? a;
  for (var n = 0; n < this.length; n++) {
    final b = this[n];
    final bIsLetter = b.isLetter || special.contains(b);
    if (bIsLetter || b.isDigit) {
      if (a != null) {
        final aIsLetter = a.isLetter || special.contains(a);
        if ((a.isLowerCase && b.isUpperCase) ||
            (a.isDigit && bIsLetter) ||
            (aIsLetter && b.isDigit) ||
            (a.isUpperCase &&
                b.isUpperCase &&
                (n + 1 < this.length && this[n + 1].isLowerCase))) {
          words.add(currentWord.toString().toLowerCase());
          currentWord = StringBuffer();
        }
      }
      currentWord.write(b);
    } else if (currentWord.isNotEmpty) {
      words.add(currentWord.toString().toLowerCase());
      currentWord = StringBuffer();
    }
    a = b;
  }
  if (currentWord.isNotEmpty) {
    words.add(currentWord.toString().toLowerCase());
  }
  return words;
}