normalizeCamelCase method

String normalizeCamelCase()

Transforms a string in camel case to normal case.

Example

'helloWorld' -> 'hello world'.

Implementation

String normalizeCamelCase() {
  String normalized = this;

  for (int i = 0; i < normalized.length; i += 1) {
    // Cannot access to [i - 1] when `i == 0`.
    //
    // ...foo bar...
    //       ^^
    // It's the beginning of a new word.
    if (i == 0 || normalized[i - 1] == ' ') {
      continue;
    }

    // ...foobar...
    //       ^
    // Not camel case.
    var c = normalized[i].removeDiacritics().toLowerCase().codeUnitAt(0);
    bool isInAlphabet = c > 96 && c < 123;

    if (!isInAlphabet || normalized[i].toUpperCase() != normalized[i]) {
      continue;
    }

    // ...fooBar...
    //       ^
    // Camel case found !
    normalized = normalized.replaceRange(i, i + 1, ' ${normalized[i].toLowerCase()}');
    // ...foo bar...
    // Result !
  }

  return normalized;
}