cleanApiServiceName function

String cleanApiServiceName(
  1. String rawName
)

Strips a trailing API service suffix from rawName and returns the resource name in snake_case.

The suffix is matched case-insensitively so acronym spellings such as APIService (which snakeCase would split into a_p_i_service) are handled correctly and don't leave the class name doubled (e.g. APIServiceApiService). When the input is nothing but the suffix (e.g. APIService), it falls back to api so a non-empty name is always produced.

Implementation

String cleanApiServiceName(String rawName) {
  String stripped = rawName.replaceFirst(
    RegExp(r'[-_ ]?api[-_ ]?service$', caseSensitive: false),
    "",
  );
  if (stripped.trim().isEmpty) {
    return "api";
  }
  return ReCase(stripped).snakeCase;
}