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 10, 2019
1 parent eec853c commit a26314a
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions Sources/URLMatcher/URLMatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,46 @@ open class URLMatcher {
///
/// - returns: A `URLMatchComponents` struct that holds the URL pattern string, a dictionary of
/// the URL placeholder values.
open func match(_ url: URLConvertible, from candidates: [URLPattern]) -> URLMatchResult? {
let url = self.normalizeURL(url)
let scheme = url.urlValue?.scheme
let stringPathComponents = self.stringPathComponents(from :url)

for candidate in candidates {
guard scheme == candidate.urlValue?.scheme else { continue }
if let result = self.match(stringPathComponents, with: candidate) {
return result
}
open func match(_ url: URLConvertible, from candidates: [URLPattern]) -> URLMatchResult? {
let url = self.normalizeURL(url)
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) {
results.append(result)
}
}

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
}
}

return nil
}

func match(_ stringPathComponents: [String], with candidate: URLPattern) -> URLMatchResult? {
let normalizedCandidate = self.normalizeURL(candidate).urlStringValue
let candidatePathComponents = self.pathComponents(from: normalizedCandidate)
Expand Down

0 comments on commit a26314a

Please sign in to comment.