Skip to content

Commit

Permalink
Fix URL matching rule devxoul#111
Browse files Browse the repository at this point in the history
  • Loading branch information
Paldom committed Apr 25, 2019
1 parent 63db5eb commit 9b2c06a
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion Sources/URLMatcher/URLMatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,39 @@ open class URLMatcher {
let scheme = url.urlValue?.scheme
let stringPathComponents = self.stringPathComponents(from :url)

var results = [URLMatchResult]()

for candidate in candidates {
guard scheme == candidate.urlValue?.scheme else { continue }
if let result = self.match(stringPathComponents, with: candidate) {
return result
}
}

return nil
if results.count > 1 {

let firstPlacholderClosure = { (urlPattern: URLPattern) -> Int in
var count = 0
for pathComponent in self.pathComponents(from: urlPattern) {
switch pathComponent {
case .plain(_):
count += 1
case .placeholder(_, _):
return count
}
}
return count
}

return results
.sorted(by: { return firstPlacholderClosure($0.pattern) < firstPlacholderClosure($1.pattern) })
.last

} else if results.count == 1 {
return results[0]
} else {
return nil
}
}

func match(_ stringPathComponents: [String], with candidate: URLPattern) -> URLMatchResult? {
Expand Down

0 comments on commit 9b2c06a

Please sign in to comment.