camelCase method

String camelCase({
  1. bool capitalize = true,
})

Convert a String to camelcase.

Implementation

String camelCase({bool capitalize = true}) {
  var hasUnderscore = false;

  final characters = split("");

  var firstCharacter = characters.removeAt(0).toLowerCase();

  if (capitalize) {
    firstCharacter = firstCharacter.toUpperCase();
  }

  final buffer = StringBuffer()..write(firstCharacter);

  for (final char in characters) {
    if (char == "_") {
      hasUnderscore = true;
      continue;
    }

    if (hasUnderscore) {
      hasUnderscore = false;
      buffer.write(char.toUpperCase());
      continue;
    }

    buffer.write(char);
  }

  return buffer.toString();
}