toSnakeCase method

String toSnakeCase(
  1. String text
)

Converts a string to snake_case naming convention.

text - The input text to convert.

Converts PascalCase/camelCase to snake_case and normalizes separators. Example: 'UserProfile' → 'user_profile'

Implementation

String toSnakeCase(String text) {
  return text
      .replaceAll(RegExp(r'[A-Z]'), '_\$&')
      .replaceAll(RegExp(r'[-\s]+'), '_')
      .toLowerCase()
      .replaceAll(RegExp(r'^_'), '');
}