toSnakeCase function
Converts input to snake_case, splitting on camelCase boundaries and any
run of non-alphanumeric characters.
Implementation
String toSnakeCase(String input) {
var value = input.trim();
if (value.isEmpty) {
return '';
}
value = value.replaceAllMapped(
RegExp(r'([a-z0-9])([A-Z])'),
(match) => '${match[1]}_${match[2]}',
);
value = value.replaceAll(RegExp(r'[^A-Za-z0-9]+'), '_');
value = value.replaceAll(RegExp(r'_+'), '_');
value = value.replaceAll(RegExp(r'^_+|_+$'), '');
return value.toLowerCase();
}