toSnakeCase property

String get toSnakeCase

Converts a string to snake_case.

Example: 'Hello World' becomes 'hello_world'

Implementation

String get toSnakeCase {
  if (isEmpty) return '';
  final result = RegExp(r'(?<=[a-z])[A-Z]').allMatches(this).fold(this,
      (String result, Match m) {
    return result.replaceRange(m.start, m.end, '_${m.group(0)}');
  });
  return result.toLowerCase().replaceAll(RegExp(r'[\s-]+'), '_');
}