getCategory function

String getCategory(
  1. String path
)

Extracts the category (module name) from a given path.

  • If the last segment contains .Shared., returns the last part.
  • Otherwise, removes /api/ and braces {}.
  • If path starts with /api/{something}/, removes both api and {something}.
  • Removes prefixes defined in ConstantsHelper.allPrefixesToRemove.
  • Returns the first remaining segment in lowercase.

If no category is found, it falls back to ConstantsHelper.generalCategory.

Implementation

String getCategory(String path) {
  final ref = path.split('/').last;
  final refParts = ref.split('.');

  if (refParts.contains('.Shared.')) {
    return refParts.last;
  }

  path = cleanPath(path);
  List<String> parts = path.split('/')
    ..removeWhere((p) => p.isEmpty); // remove empty strings from //

  if (parts.isEmpty) return ConstantsHelper.generalCategory;

  // If starts with api/{something}, remove both
  if (parts.length > 1 && parts.first.toLowerCase() == 'api') {
    parts.removeAt(0); // remove "api"
    parts.removeAt(0); // remove second part (e.g., "mobile", "warehouseapp")
  }

  // Remove defined prefixes
  for (var prefix in ConstantsHelper.allPrefixesToRemove) {
    if (parts.isNotEmpty && parts.first.toLowerCase() == prefix.toLowerCase()) {
      parts.removeAt(0);
      break;
    }
  }

  return parts.isNotEmpty
      ? parts.first.toLowerCase()
      : ConstantsHelper.generalCategory;
}