routesUnder function

List<RouteEntry> routesUnder(
  1. Iterable<RouteEntry> routes,
  2. String prefix
)

The routes under prefix (/api), or all of them when none are.

Implementation

List<RouteEntry> routesUnder(Iterable<RouteEntry> routes, String prefix) {
  final base = prefix.endsWith('/') && prefix.length > 1
      ? prefix.substring(0, prefix.length - 1)
      : prefix;
  if (base == '/' || base.isEmpty) {
    return routes.toList();
  }
  final under = [
    for (final route in routes)
      if (route.path == base || route.path.startsWith('$base/')) route,
  ];
  return under.isEmpty ? routes.toList() : under;
}