groupIntoWords method

List<String> groupIntoWords()

Splits this into words along word boundaries.

Implementation

List<String> groupIntoWords() {
  var result = this;

  // all non-alphanumeric characters: "acm-success"-> "acm success"
  result = result.replaceAll(_nonAlphaNumericChars, ' ');

  // if a number has a standalone v in front of it, separate it out
  result = result
      // TESTv4 -> "TEST v4"
      .replaceAllMapped(
        _standaloneVLower,
        (m) => '${m.group(1)} v${m.group(2)}',
      )

      // TestV4 -> "Test V4"
      .replaceAllMapped(
        _standaloneVUpper,
        (m) => '${m.group(1)} V${m.group(2)}',
      );

  // add a space between camelCased words: "AcmSuccess" -> "Acm Success"
  // Workaround for lack of support for lookbehinds in Safari:
  // https://caniuse.com/js-regexp-lookbehind
  var start = 0;
  result = () sync* {
    yield* _camelCasedWords.allMatches(result).map((match) {
      final end = match.start + 1;
      final substr = result.substring(start, end);
      start = end;
      return substr;
    });
    yield result.substring(start);
  }()
      .join(' ');

  // add a space after acronyms: "ACMSuccess" -> "ACM Success"
  result = result.replaceAllMapped(
    _acronyms,
    (m) => '${m.group(1)} ${m.group(2)}',
  );

  // add space after a number in the middle of a word: "s3ec2" -> "s3 ec2"
  result = result.replaceAllMapped(
    _numInMiddleOrEnd,
    (m) => '${m.group(1)} ${m.group(2)}',
  );

  // remove extra spaces - multiple consecutive ones or those and the
  // beginning/end of words
  result = result.replaceAll(RegExp(r'\s+'), ' ').trim();

  return result.split(' ');
}