endpointNorm function

String endpointNorm(
  1. List<String> paths, {
  2. bool normSlashs = true,
  3. dynamic endWithSlash = true,
  4. dynamic startWithSlash = true,
})

Normalizes endpoint paths for URL routing with customizable formatting options. This function takes a list of path segments and combines them into a normalized endpoint path suitable for web routing. It provides fine-grained control over the formatting including slash handling and path structure. normSlashs If true, converts backslashes to forward slashes (default: true) endWithSlash If true, ensures the path ends with a forward slash (default: true) startWithSlash If true, ensures the path starts with a forward slash (default: true) The function also handles double slashes by replacing them with single slashes Example usage:

// Result: '/api/users/profile/'
String apiPath = endpointNorm(['v1', 'data'], endWithSlash: false);
// Result: '/v1/data'

Implementation

String endpointNorm(
  List<String> paths, {
  bool normSlashs = true,
  endWithSlash = true,
  startWithSlash = true,
}) {
  var path = joinPaths(paths);
  if (endWithSlash && !path.endsWith('/')) {
    path += '/';
  }

  if (startWithSlash && !path.startsWith('/')) {
    path = '/$path';
  }

  if (normSlashs) {
    path = path.replaceAll('\\', '/');
  }
  path = path.replaceAll('//', '/');
  return path;
}