TextSplitWords method

List<String> TextSplitWords(
  1. String text
)

Splits on _, -, and camel/Pascal-case boundaries.

Implementation

List<String> TextSplitWords(String text) {
  final result = <String>[];
  final buffer = StringBuffer();
  for (var i = 0; i < text.length; i++) {
    final c = text[i];
    if (c == '_' || c == '-' || c == ' ') {
      if (buffer.isNotEmpty) {
        result.add(buffer.toString());
        buffer.clear();
      }
    } else if (buffer.isNotEmpty && c != c.toLowerCase() && c == c.toUpperCase()) {
      result.add(buffer.toString());
      buffer.clear();
      buffer.write(c);
    } else {
      buffer.write(c);
    }
  }
  if (buffer.isNotEmpty) result.add(buffer.toString());
  return result;
}