pathIs method

bool pathIs(
  1. String pattern
)

True when the path matches pattern. * matches any text. The path and pattern are cleaned like the router does: // is one /, and a / at the start or the end does not matter. So /admin/ and /admin// both match admin, like they reach the /admin route. (is is a Dart keyword.)

req.pathIs('admin/*');

Implementation

bool pathIs(String pattern) {
  RegExp regex = _pathWildcard(pattern);
  String raw = uri.path;
  String clean = sanitizeRoutePath(raw);
  try {
    if (regex.hasMatch(Uri.decodeComponent(clean.substring(1)))) return true;
    // the path as it came still counts, so `/admin/` keeps matching
    // `admin/*` like before
    return clean != raw &&
        regex.hasMatch(Uri.decodeComponent(_trimSlash(raw)));
  } on FormatException {
    // a bad escape, like `/%ff`, matches no pattern
    return false;
  } on ArgumentError {
    // a bare `%` or a bad code like `%zz` matches no pattern
    return false;
  }
}