snakeCase property

String get snakeCase

Convert a String to snake_case

Implementation

String get snakeCase {
  final characters = split("");

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

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

  for (final char in characters) {
    if (_isAlphabeticRegex.firstMatch(char) != null &&
        char.toUpperCase() == char) {
      buffer
        ..write("_")
        ..write(char.toLowerCase());
    } else {
      buffer.write(char);
    }
  }

  return buffer.toString();
}