validName function

String validName(
  1. String input, {
  2. bool firstCapital = false,
})

Converts lower case names with underscore to UpperCamelCase (for classes) or lowerCamelCase (for class fields).

Implementation

String validName(String input, { bool firstCapital = false }) {
    RegExp re = firstCapital ? RegExp(r'^[a-z]|[^a-zA-Z][a-z]') : RegExp(r'[^a-zA-Z\?][a-z]');
    String result = input.replaceAllMapped(re, (Match match) => match.group(0)!.toUpperCase());
    re = RegExp(r'^\??\d+|[^a-zA-Z0-9]');
    result = result.replaceAll(re, '');
    return result;
}