getMostSpecificMatch method

  1. @visibleForTesting
Routed? getMostSpecificMatch(
  1. String location,
  2. List<Routed> matches
)

Given a list of possible matches, return the most exact one.

Implementation

@visibleForTesting
Routed? getMostSpecificMatch(String location, List<Routed> matches) {
  // Return null if we have no matches
  if (matches.isEmpty) return null;
  // If we have an exact match, use that
  for (var m in matches) {
    if (m.value == location) return m;
  }
  // Next, take the first non-prefixed match we see
  for (var m in matches) {
    if (!m.prefix) return m;
  }
  // Last resort, take the longest match which we'll assume is most precise.
  // Adds special logic to make sure '*' gets sorted after something like '/'
  // TODO: This should sort by number of matched segments first, and then length?
  matches.sort((a, b) {
    if (b.value == '*') return -1;
    return a.value.length > b.value.length ? -1 : 1;
  });
  return matches[0];
}