match method

Route? match(
  1. String method,
  2. String path
)

Match a route for given method and path

Implementation

Route? match(String method, String path) {
  if (!_routes.containsKey(method)) {
    return null;
  }

  List<String> parts = path.split('/').where((p) => p.isNotEmpty).toList();
  int length = parts.length - 1;
  List<int> filteredParams = _params.where((i) => i <= length).toList();

  for (List<int> sample in combinations<int>(filteredParams)) {
    sample = sample.where((i) => i <= length).toList();
    String match = parts
        .asMap()
        .entries
        .map(
          (entry) =>
              sample.contains(entry.key) ? placeholderToken : entry.value,
        )
        .join('/');

    if (_routes[method]!.containsKey(match)) {
      return _routes[method]![match]!;
    }
  }

  return null;
}