toSnakeCase method
Converts a string from camelCase to snake_case.
Example:
'helloWorld'.toSnakeCase( ) ; // 'hello_world'
'userProfileData'.toSnakeCase( ) ; // 'user_profile_data'
'HelloWorld'.toSnakeCase( ) ; // 'hello_world'
Implementation
String toSnakeCase({String separator = '_'}) {
return this
.replaceAll(RegExp(r'\s+'), '_')
.replaceAllMapped(
RegExp(r'[A-Z]'), (match) => '_' + match.group(0)!.toLowerCase())
.replaceAll(RegExp(r'^_'), '')
.replaceAll(RegExp(r'_+'), '_');
}